teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

もう一つのファイルについて修正点を追記

2020/04/16 23:31

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -36,4 +36,40 @@
36
36
  `self.irc.send(line.encode(encoding))` の b'\r\n' を外しました。
37
37
 
38
38
  コードの修正箇所を少なくする為。_send_raw内のを変更しましたが、
39
- 呼び出し側で追加するか、_send_raw内で追加するか、どちらか片方で良いです。
39
+ 呼び出し側で追加するか、_send_raw内で追加するか、どちらか片方で良いです。
40
+
41
+ ----
42
+ 追記
43
+
44
+ ```python
45
+
46
+ from irc import *
47
+ import os
48
+ import random
49
+
50
+ channel = "#testit"
51
+ server = "irc.freenode.net"
52
+ botnick = "reddity"
53
+
54
+ irc = IRC()
55
+ irc.connect(server, channel, botnick)
56
+
57
+ while 1:
58
+ text = irc.get_text().decode("utf-8")
59
+ print(text)
60
+
61
+ if "PRIVMSG" in text and channel in text and "hello" in text:
62
+ irc.send(channel, "HEllo!")
63
+
64
+ ```
65
+
66
+ 変更点:
67
+ `text = irc.get_text().decode("utf-8")`
68
+
69
+ text がバイト型の場合
70
+
71
+ - b"PRIVMSG" in text
72
+ - b"hello" in text
73
+ - とできますが、 `channel in text` の channel は 文字列なので変換が必要
74
+ 但し、irc.send は文字列を要求されるので。元のtextを文字列に変換するようにしました。
75
+ エンコードは固定で utf-8 としてます。