質問編集履歴
3
新規エラーの発生
title
CHANGED
File without changes
|
body
CHANGED
@@ -189,4 +189,41 @@
|
|
189
189
|
|
190
190
|
クラスsocketが_send_raw属性を持たないとエラーが出ていますが、なぜクラスsocketがここで出てくるのかよくわかりません。
|
191
191
|
IRCクラス内の属性だと思ったのですが、この辺の仕組みがよく理解できていないように思います。
|
192
|
-
エラーの原因がわかりましたらご教授お願いいたします。
|
192
|
+
エラーの原因がわかりましたらご教授お願いいたします。
|
193
|
+
|
194
|
+
2020/4/17追記
|
195
|
+
```Python
|
196
|
+
/home/yuta/Desktop/work/python/PycharmProjects/TestPJ/venv/bin/python /home/yuta/Desktop/work/python/PycharmProjects/TestPJ/networking/bot.py
|
197
|
+
connecting to:irc.freenode.net
|
198
|
+
b':barjavel.freenode.net NOTICE * :*** Looking up your hostname...\r\n'
|
199
|
+
Traceback (most recent call last):
|
200
|
+
File "/home/yuta/Desktop/work/python/PycharmProjects/TestPJ/networking/bot.py", line 16, in <module>
|
201
|
+
if "PRIVMSG " in text and channel in text and "hello" in text:
|
202
|
+
TypeError: a bytes-like object is required, not 'str'
|
203
|
+
|
204
|
+
Process finished with exit code 1
|
205
|
+
```
|
206
|
+
|
207
|
+
```bot
|
208
|
+
from irc import *
|
209
|
+
import os
|
210
|
+
import random
|
211
|
+
|
212
|
+
channel = "#testit"
|
213
|
+
server = "irc.freenode.net"
|
214
|
+
botnick = "reddity"
|
215
|
+
|
216
|
+
irc = IRC()
|
217
|
+
irc.connect(server, channel, botnick)
|
218
|
+
|
219
|
+
while 1:
|
220
|
+
text = irc.get_text()
|
221
|
+
print(text)
|
222
|
+
|
223
|
+
if "PRIVMSG " in text and channel in text and "hello" in text:
|
224
|
+
irc.send(channel, "HEllo!")
|
225
|
+
```
|
226
|
+
呼び出す側のbotで型エラーがおきました。
|
227
|
+
|
228
|
+
strをbyteに直す必要がありますが、どこを指しているのか不明です。
|
229
|
+
自分では全然検討もつかなくものすごく申し訳ないですが、わかる人がいましたらご教授お願いいたします。
|
2
指摘を基にして修正を行った。
title
CHANGED
File without changes
|
body
CHANGED
@@ -137,4 +137,56 @@
|
|
137
137
|
|
138
138
|
connect属性を持っていないとエラーがでました。
|
139
139
|
irc.pyの該当属性を入れ子にしてタブをずらしたことで属性を見つけられなくなったと推測できたのですが、この場合botの呼び出しの引数をどのようにirc.pyの方へ渡せば良いのでしょうか?
|
140
|
-
わかる方がいらしたらぜひアドバイスをお願い致します。
|
140
|
+
わかる方がいらしたらぜひアドバイスをお願い致します。
|
141
|
+
|
142
|
+
2020/04/16追記
|
143
|
+
指摘を基に修正したところ新たなエラーがでました。
|
144
|
+
```irc
|
145
|
+
import socket
|
146
|
+
import sys
|
147
|
+
|
148
|
+
class IRC:
|
149
|
+
irc = socket.socket()
|
150
|
+
|
151
|
+
def __init__(self):
|
152
|
+
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
153
|
+
|
154
|
+
def send(self, chan, msg):
|
155
|
+
self.irc._send_raw("PRIVMSG " + chan + " " + msg + "\n")
|
156
|
+
|
157
|
+
def _send_raw(self, line, encoding="utf-8"):
|
158
|
+
self.irc._send_raw(line.encode(encoding) + b"\r\n")
|
159
|
+
|
160
|
+
def connect(self, server, channel, botnick):
|
161
|
+
# defines the socket
|
162
|
+
print("connecting to:" + server)
|
163
|
+
self.irc.connect((server, 6667)) # connects to the server
|
164
|
+
self.irc._send_raw("USER " + botnick + " " + botnick + " " + botnick + " :This is a fun bot!\n") # user authentication
|
165
|
+
self.irc._send_raw("NICK " + botnick + "\n")
|
166
|
+
self.irc._send_raw("JOIN " + channel + "\n") # join the channel
|
167
|
+
|
168
|
+
def get_text(self):
|
169
|
+
text=self.irc.recv(2040) # receive the text
|
170
|
+
|
171
|
+
if text.find(b'PING') != -1:
|
172
|
+
self._send_raw('PONG' + text.split()[1])
|
173
|
+
return text
|
174
|
+
```
|
175
|
+
|
176
|
+
```Python
|
177
|
+
/home/yuta/Desktop/work/python/PycharmProjects/TestPJ/venv/bin/python /home/yuta/Desktop/work/python/PycharmProjects/TestPJ/networking/bot.py
|
178
|
+
connecting to:irc.freenode.net
|
179
|
+
Traceback (most recent call last):
|
180
|
+
File "/home/yuta/Desktop/work/python/PycharmProjects/TestPJ/networking/bot.py", line 10, in <module>
|
181
|
+
irc.connect(server, channel, botnick)
|
182
|
+
File "/home/yuta/Desktop/work/python/PycharmProjects/TestPJ/networking/irc.py", line 20, in connect
|
183
|
+
self.irc._send_raw("USER " + botnick + " " + botnick + " " + botnick + " :This is a fun bot!\n") # user authentication
|
184
|
+
AttributeError: 'socket' object has no attribute '_send_raw'
|
185
|
+
|
186
|
+
Process finished with exit code 1
|
187
|
+
|
188
|
+
```
|
189
|
+
|
190
|
+
クラスsocketが_send_raw属性を持たないとエラーが出ていますが、なぜクラスsocketがここで出てくるのかよくわかりません。
|
191
|
+
IRCクラス内の属性だと思ったのですが、この辺の仕組みがよく理解できていないように思います。
|
192
|
+
エラーの原因がわかりましたらご教授お願いいたします。
|
1
回答を基に新たに修正して発生したエラーへの問い合わせ
title
CHANGED
File without changes
|
body
CHANGED
@@ -68,4 +68,73 @@
|
|
68
68
|
**__TypeError: a bytes-like object is required, not 'str'__**
|
69
69
|
↑はbyte型で指定しないといけないという意味はなんとなくわかるのですが、具体的にどこを修正すればよろしいのかイマイチ理解できておりません。
|
70
70
|
|
71
|
-
わかる人がいましたらご教授お願いいたします。
|
71
|
+
わかる人がいましたらご教授お願いいたします。
|
72
|
+
|
73
|
+
2020/4/26追記
|
74
|
+
指摘点を基に修正しましたが新たなエラーが出ました。
|
75
|
+
```irc
|
76
|
+
import socket
|
77
|
+
import sys
|
78
|
+
|
79
|
+
class IRC:
|
80
|
+
irc = socket.socket()
|
81
|
+
|
82
|
+
def __init__(self):
|
83
|
+
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
84
|
+
|
85
|
+
def send(self, chan, msg):
|
86
|
+
self.irc.send("PRIVMSG " + chan + " " + msg + "\n")
|
87
|
+
|
88
|
+
def _send_raw(self, line, encoding="utf-8"):
|
89
|
+
self.irc.send(line.encode(encoding) + b"\r\n")
|
90
|
+
|
91
|
+
def connect(self, server, channel, botnick):
|
92
|
+
# defines the socket
|
93
|
+
print("connecting to:" + server)
|
94
|
+
self.irc.connect((server, 6667)) # connects to the server
|
95
|
+
self.irc.send("USER " + botnick + " " + botnick + " " + botnick + " :This is a fun bot!\n") # user authentication
|
96
|
+
self.irc.send("NICK " + botnick + "\n")
|
97
|
+
self.irc.send("JOIN " + channel + "\n") # join the channel
|
98
|
+
|
99
|
+
def get_text(self):
|
100
|
+
text=self.irc.recv(2040) # receive the text
|
101
|
+
|
102
|
+
if text.find(b'PING') != -1:
|
103
|
+
self._send_raw('PONG' + text.split()[1])
|
104
|
+
return text
|
105
|
+
```
|
106
|
+
|
107
|
+
```bot
|
108
|
+
from irc import *
|
109
|
+
import os
|
110
|
+
import random
|
111
|
+
|
112
|
+
channel = "#testit"
|
113
|
+
server = "irc.freenode.net"
|
114
|
+
botnick = "reddity"
|
115
|
+
|
116
|
+
irc = IRC()
|
117
|
+
irc.connect(server, channel, botnick)
|
118
|
+
|
119
|
+
while 1:
|
120
|
+
text = irc.get_text()
|
121
|
+
print(text)
|
122
|
+
|
123
|
+
if "PRIVMSG " in text and channel in text and "hello" in text:
|
124
|
+
irc.send(channel, "HEllo!")
|
125
|
+
```
|
126
|
+
|
127
|
+
```Python
|
128
|
+
/home/yuta/Desktop/work/python/PycharmProjects/TestPJ/venv/bin/python /home/yuta/Desktop/work/python/PycharmProjects/TestPJ/networking/bot.py
|
129
|
+
Traceback (most recent call last):
|
130
|
+
File "/home/yuta/Desktop/work/python/PycharmProjects/TestPJ/networking/bot.py", line 10, in <module>
|
131
|
+
irc.connect(server, channel, botnick)
|
132
|
+
AttributeError: 'IRC' object has no attribute 'connect'
|
133
|
+
|
134
|
+
Process finished with exit code 1
|
135
|
+
|
136
|
+
```
|
137
|
+
|
138
|
+
connect属性を持っていないとエラーがでました。
|
139
|
+
irc.pyの該当属性を入れ子にしてタブをずらしたことで属性を見つけられなくなったと推測できたのですが、この場合botの呼び出しの引数をどのようにirc.pyの方へ渡せば良いのでしょうか?
|
140
|
+
わかる方がいらしたらぜひアドバイスをお願い致します。
|