【发布时间】:2017-02-25 19:58:57
【问题描述】:
我正在用 Python 编写一个简单的 IRC 机器人。实际上,它可以连接到特定的频道,读取消息并发送响应,但它无法区分频道消息和私人消息。
示例:
连接到同一频道的 John 向 Bot 的聊天室发送私人消息,例如“!say hello”;
机器人必须向同一个私人聊天室发送“你好”,仅发送给 John。
相反,当机器人在频道板上读取“!say hello”时,必须向频道发送“hello”。
我的代码:
ircmsg = connection.ircsock.recv(2048)
ircmsg_clean = ircmsg.strip(str.encode('\n\r'))
if ircmsg.find(str.encode("!say")) != -1:
try:
parts = ircmsg_clean.split()
content = parts[4]
connection.sendMsg(channel, content)
except IndexError:
connection.sendMsg(channel, "Invalid syntax")
连接文件:
def sendmsg(channel, msg):
ircsock.send(str.encode("PRIVMSG " + channel +" :" + msg + "\n"))
我知道如何向特定用户发送消息:
def sendPrivateMsg(username, msg):
ircsock.send(str.encode("PRIVMSG " + username + " :" + msg + "\n"))
但必须知道消息来自哪里、渠道或用户,并发送适当的响应。
对不起,我的英语不好。
【问题讨论】:
标签: python networking bots irc channel