2016-08-14 38 views
0

所以我做了一個IRC bot的通道,這是代碼:我的Python bot可以連接到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') 

這是結果我得到當我執行PROGRAMM:

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 [email protected]+ 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服務器,但不加入頻道.... 什麼錯?

+0

您不應該在每次發送之間等待服務器的回答嗎? – spectras

+0

題外話:不要使用'if text.find('PING')!= -1:'。 '如果在文本中'PING':'完全一樣,並且更容易閱讀(並且在Python中是慣用的)。 –

回答

1

正如@spectras注意到的,在發送JOIN之前,您必須稍等一會兒。

的關鍵是從服務器上這樣回覆:

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

這意味着您在完成註冊過程,我想這回答了PING之前發送一個命令(PRIVMSG和/或JOIN) 。

通常的(也是好的)做法是在MOTD完成後發送初始消息,即,當您收到376命令。


題外話的話:你應該考慮using SASL,而不是發送到PRIVMSG NickServ。這是現在在IRC上進行身份驗證的標準。

+0

好吧,我發送JOIN之前添加了「time.sleep(1)」,是嗎? – TAL

+0

我只是試過,但我得到了相同的結果 – TAL

+0

不,它不是。因爲你必須在發送JOIN前**回答**。 –

相關問題