【发布时间】:2018-02-21 09:03:27
【问题描述】:
我正在尝试向我的 Python IRC 机器人添加一个功能,当我在 IRC 上键入“join #channel-name”时,机器人将加入频道。
这是我的代码:
# IRC bot written by syrius
import socket
server = "irc.freenode.net" # IRC server
channel = "#syrius-test" # Channel
botnick = "syrius-bot" # Nickname of the bot
master = "syrius_" # Nickname of the bot's master
exitcode = "bye " + botnick #Text that we will use to make the bot quit
def ircwrite(message):
global ircsock
ircsock.send(str(message).encode('latin-1', 'ignore'))
def ping():
ircwrite("PONG :pingis\n")
def sendmsg(chan , msg):
ircwrite("PRIVMSG "+ chan +" :"+ msg +"\n")
def joinchan(channel):
ircsock.send(bytes("JOIN "+ channel + "\n"))
def join():
ircsock.send(bytes("JOIN %s"))
def hello():
ircwrite("PRIVMSG "+ channel +" :Hello!\n")
def quitting():
ircwrite("PRIVMSG "+ channel +" :Okay boss, leaving now.\n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))
ircwrite("USER "+ botnick +" "+ botnick +" "+ botnick +" :IRC bot coded by syrius.\n")
ircwrite("NICK "+ botnick +"\n")
joinchan(channel)
while 1:
ircmsg = ircsock.recv(2048).decode() # receive data from the server
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
print(ircmsg) # Here we print what's coming from the server
name = ircmsg.split('!',1)[0][1:] # We split out the name
if ircmsg.find(":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
hello()
if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
ping()
if name.lower() == master.lower() and ircmsg.find(":quit " + botnick) != -1:
quitting()
ircsock.send(bytes("QUIT \n", "UTF-8"))
if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:
join()
main()
下面的代码当然是不正确的:
第 23 行:
def join():
ircsock.send(bytes("JOIN %s"))
第 56 行:
if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:
join()
我想知道我应该在那里放什么以便机器人可以加入频道。
任何帮助将不胜感激。
【问题讨论】:
-
请单独提出您的问题。 IRC 命令还需要行尾标记。你知道 b 字面量吗?另外,你要给 %s 提供什么?
-
请阅读this!
-
HI Bear Brown,感谢您的快速回复。因此,对于行尾标记,它将是: ircsock.send(bytes("JOIN %s\n")) ? %s 可能不正确,因为它是我在 IRC 上写的要加入的频道的名称。
-
凹凸。任何人 ?请帮忙!