我的Python Bot可以连接到IRC服务器但不加入该频道

时间:2016-08-14 16:34:37

标签: python irc

所以我制作了一个IRC机器人,这是代码:

import socket
import sys

server = "irc.esper.net"
channel = "#stencyl"
botnick = "MrGutsy"

irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "connecting to:"+server
irc.connect((server, 6667))
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This is a fun   bot!\n")
irc.send("NICK "+ botnick +"\n")
irc.send("PRIVMSG nickserv :iNOOPE\r\n")
irc.send("JOIN "+ channel +"\n")

while 1:
   text=irc.recv(2040)
   print text

   if text.find('PING') != -1:
      irc.send('PONG ' + text.split() [1] + '\r\n')
   if text.find(':!hi') !=-1:
      t = text.split(':!hi')
      to = t[1].strip()
      irc.send('PRIVMSG '+channel+' :Hello '+str(to)+'! \r\n')
   if text.find(':!water') !=-1:
      t = text.split(':!hi')
      to = t[1].strip()
      irc.send('PRIVMSG '+channel+' :*brings water '+str(to)+'! \r\n')

这是我执行程序时得到的结果:

ThinkPad-T420:~$ python test.py
connecting to:irc.esper.net
:stormlight.esper.net NOTICE * :*** Looking up your hostname...

:stormlight.esper.net NOTICE * :*** Checking Ident
:stormlight.esper.net NOTICE * :*** Found your hostname

:stormlight.esper.net NOTICE * :*** No Ident response

PING :0C882AF0
:stormlight.esper.net 451 * :You have not registered

:stormlight.esper.net 001 MrGutsy :Welcome to the EsperNet Internet Relay Chat Network MrGutsy
:stormlight.esper.net 002 MrGutsy :Your host is stormlight.esper.net[45.79.137.210/6667], running version charybdis-3.5.0-dev
:stormlight.esper.net 003 MrGutsy :This server was created Sat May 21 2016 at 23:51:37 UTC
:stormlight.esper.net 004 MrGutsy stormlight.esper.net charybdis-3.5.0-dev DQRSZagiloswz CFILPQTbcefgijklmnopqrstvz bkloveqjfI
:stormlight.esper.net 005 MrGutsy SAFELIST ELIST=CTU CHANTYPES=# EXCEPTS INVEX CHANMODES=eIbq,k,flj,CFLPQTcgimnprstz CHANLIMIT=#:50 PREFIX=(ov)@+ MAXLIST=bqeI:100 MODES=4 NETWORK=EsperNet KNOCK :are supported by this server
:stormlight.esper.net 005 MrGutsy STATUSMSG=@+ CALLERID=g CASEMAPPING=rfc1459 NICKLEN=30 MAXNICKLEN=30 CHANNELLEN=50 TOPICLEN=390 ETRACE CPRIVMSG CNOTICE DEAF=D MONITOR=100 :are supported by this server
:stormlight.esper.net 005 MrGutsy FNC TARGMAX=NAMES:1,LIST:1,KICK:1,WHOIS:1,PRIVMSG:4,NOTICE:4,ACCEPT:,MONITOR: EXTBAN=$,acjorsxz WHOX CLIENTVER=3.0 :are supported by this server
:stormlight.esper.net 251 MrGutsy :There are 9 users and 6825 invisible on 15 servers
:stormlight.esper.net 252 MrGutsy 34 :IRC Operators online
:stormlight.esper.net 253 MrGutsy 1 :unknown connection(s)
:stormlight.esper.net 254 MrGutsy 5328 :channels formed
:stormlight.esper.net 255 MrGutsy :I have 1181 clients and 1 servers
:stormlight.esper.net 265 MrGutsy 1181 1395 :Current local users 1181, max 1395
:stormlight.esper.net 266 MrGutsy 6834 7714 :Current global users 6834, max 7714
:stormlight.esper.net 250 MrGutsy :Highest connection count: 1396 (1395 clients) (91268 connections received)
:stormlight.esper.net 375 MrGutsy :- stormlight.esper.net Message of the Day - 
:stormlight.esper.net 372 MrGutsy :-            __                       .__  .__       .__     __   
:stormlight.esper.net 372 MrGutsy :-    _______/  |_  ___________  _____ |  | |__| ____ |  |___/  |_ 
:stormlight.esper.net 372 MrGutsy :-   /  ___/\   __\/  _ \_  __ \/     \|  | |  |/ ___\|  |  \   __\
:stormlight.esper.net 372 MrGutsy :-   \___ \  |  | (  <_> )  | \/  Y Y  \  |_|  / /_/  >   Y  \  |  
:stormlight.esper.net 372 MrGutsy :-  /____  > |__|  \____/|__|  |__|_|  /____/__\___  /|___|  /__|  
:stormlight.esper.net 372 MrGutsy :-       \/  stormlight.esper.net    \/       /_____/      \/  
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- Location: Newark NJ, United States
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- Ports: 5555, 6665 - 6669, 6697 (SSL), 7000.
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- Knight Radiant (Administrator): brynjar (brynjar at esper.net)
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- Shardbearers (Operators): Raiden (raiden at esper.net)
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- Terms of Service:
:stormlight.esper.net 372 MrGutsy :-   Your use of this network constitutes an agreement to abide by the
:stormlight.esper.net 372 MrGutsy :-   rules presented in the EsperNet AUP - http://esper.net/charter.php
:stormlight.esper.net 372 MrGutsy :-   as well as any applicable U.S. and International laws.
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :-   Further, your use of this network implies consent to a port scan
:stormlight.esper.net 372 MrGutsy :-   to detect open proxies and otherwise compromised systems.
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :-   Properly configured bots are permitted, but are required to follow
:stormlight.esper.net 372 MrGutsy :-   the same rules as users. FServes are strictly prohibited. 
:stormlight.esper.net 372 MrGutsy :-   See http:/www.esper.net/bots.php for more information.
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :-   We reserve the right to remove anyone at any time for any reason.
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- New to IRC? Helpful information:
:stormlight.esper.net 372 MrGutsy :-             http://www.esper.net/getting_started.php 
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- USEFUL CHANNELS
:stormlight.esper.net 372 MrGutsy :- #dragonweyr - Network staff assistance
:stormlight.esper.net 372 MrGutsy :- #coders - Help with programming
:stormlight.esper.net 372 MrGutsy :- #lobby - General chat
:stormlight.esper.net 372 MrGutsy :- #help - General IRC help
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 372 MrGutsy :- Curious about our name? Check out The Stormlight Archive by Brandon
:stormlight.esper.net 372 MrGutsy :- Sanderson!
:stormlight.esper.net 372 MrGutsy :-  
:stormlight.esper.net 376 MrGutsy :End of /MOTD command.
:MrGutsy MODE MrGutsy :+i

它似乎连接到IRC服务器,但没有加入频道.... 怎么了??

1 个答案:

答案 0 :(得分:1)

正如@spectras注意到的那样,你必须先等一下才能发送JOIN。

关键是来自服务器的回复:

:stormlight.esper.net 451 * :You have not registered

这意味着您在完成注册过程之前发送了一个命令(PRIVMSG和/或JOIN),我猜它会回复PING。

通常(和良好)的做法是在MOTD完成后发送初始消息,即。当您收到376命令时。

偏离主题的评论:您应该考虑using SASL而不是向NickServ发送PRIVMSG。它现在是在IRC上进行身份验证的标准。