【问题标题】:IRC bot written in Python - join channel function用 Python 编写的 IRC bot - 加入频道功能
【发布时间】: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 上写的要加入的频道的名称。
  • 凹凸。任何人 ?请帮忙!

标签: python bots irc


【解决方案1】:

我发现此解决方案存在一些问题,我的建议是您可能应该尝试使用 IRC 框架,例如 Pydle 或已经处理协议的十亿其他框架之一。

我看到的第一个问题是使用 latin-1 进行编码,通常您应该使用 utf-8,您也可以使用服务器在 RPL_ISUPPORT 回复中的 CHARSET 中宣传的任何内容,尽管这不再常见。除了编码之外,您还可以从 utf-8 解码 IRC 行,这样您就可以处理字符串而不是字节,然后在输出时重新编码。

下一个 IRC 问题将是您的行结束,IRC 消息应始终以 CR-LS(回车换行)结束,这将是 \r\n 字符,而不仅仅是 \n。

我想提到的最后一件事是您的字符串格式化,您应该使用"JOIN {}".format(channel),这是目前首选的字符串格式化方法,除非您使用的是 python 3.7,否则您将使用类似的 fstrings。

使用您现在尝试进行格式化的方式,您是通过两个连接(例如"USER" + channel)进行的,但您还尝试通过 %s 使用旧式字符串格式化。如果您想使用 %s 格式,正确的方法是 "JOIN %s" % (channel),尽管现在使用 .format 和 fstrings 是首选方法。

【讨论】:

    【解决方案2】:

    你缺少论据。 ircsock.send(bytes("JOIN %s"))。

    应该是这样的:

    ircsock.send(bytes("JOIN %s \r\n") % (channel))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2015-03-23
      • 1970-01-01
      • 2016-12-21
      • 2017-02-25
      • 2015-08-11
      • 2015-11-24
      相关资源
      最近更新 更多