IRC机器人乒乓球不起作用

时间:2015-08-27 19:46:17

标签: ping bots irc

我使用this page中的代码创建了bot。 当我试图到达irc.rizon.net时,一切都很好。但是,当我将服务器更改为irc.alphachat.net时,问题就出现了。

#!/usr/bin/env python3
import socket

server = 'irc.alphachat.net'
channel = '#somechannel'
NICK = 'somenick'
IDENT = 'somenick'
REALNAME = 'somenick'
port = 6667

def joinchan(chan):
    ircsock.send(bytes('JOIN %s\r\n' % chan, 'UTF-8'))


def ping(): # This is our first function! It will respond to server Pings.
    ircsock.send(bytes("QUOTE PONG \r\n", 'UTF-8'))


def send_message(chan, msg):
    ircsock.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))

ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, port)) # Here we connect to the server using the port 6667
ircsock.send(bytes("USER "+ NICK +" "+ NICK +" "+ NICK +" :This bot\n", 'UTF-8')) # user authentication
ircsock.send(bytes("NICK "+ NICK +"\n", 'UTF-8')) # here we actually assign the nick to the bot

joinchan(channel) # Join the channel using the functions we previously defined

while 1: # Be careful with these! it might send you to an infinite loop
  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
  if ircmsg.find(' PRIVMSG ')!=-1:
     nick=ircmsg.split('!')[0][1:]
  if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
    ping()
  if ircmsg.find(":Hello "+ NICK) != -1: # If we can find "Hello Mybot" it will call the function hello()
    hello()

问题在于ping命令,因为我不知道如何回答服务器:

:irc-us2.alphachat.net NOTICE * :*** Looking up your hostname...
:irc-us2.alphachat.net NOTICE * :*** Checking Ident
:irc-us2.alphachat.net NOTICE * :*** Found your hostname
:irc-us2.alphachat.net NOTICE * :*** No Ident response
PING :CE661578
:irc-us2.alphachat.net 451 * :You have not registered

2 个答案:

答案 0 :(得分:0)

使用IRC,你应该把每一行按''(空格)划分成块来处理它 - 这样的东西应该在你的打印之后起作用(未经测试)

它不起作用的原因是因为你没有正确回复PING

chunk = ircmsg.split(' ')
if chunk[0] == 'PING': # This is a ping
  ircsock.send(bytes('PONG :%s\r\n' % (chunk[1]), 'UTF-8')) # Send a pong!
if chunk[1] == 'PRIVMSG': # This is a message
  if chunk[3] == ':Hello': # Hey, someone said hello!
    send_message(chunk[2], "Hi there!") # chunk[2] is channel / private!
if chunk[1] == '001': # We've logged on
  joinchannel(channel) # Let's join!
  send_message(channel, "I've arrived! :-)") # Announce to the channel

通常命令/ numeric在第二个参数(chunk [1])中找到 - 我能想到的唯一例外是PING,它位于第一个参数(chunk [0])

另请注意,我移动了joinchannel() - 您应该在登录后才这样做。

编辑:没有意识到这篇文章的年龄。遗憾!

答案 1 :(得分:0)

我相信您只需要对您为响应ping请求而发送的字符串进行少量更改即可。

尝试使用:

ircsock.send(bytes("PONG pingis\n", "UTF-8"))

此ping响应在freenode上对我有效。