2016-08-14 14 views
0

に参加does notの私は、これはコードで、IRCボットを作った:私のPythonのボットは、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 [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 'がtext: 'と同じことをして、読みやすく(そしてPythonでは慣用句です)。 –

答えて

1

@spectrasが気付くように、JOINを送信する前にちょっと待たなければなりません。あなたは、私はそれがPINGに応答する推測完成し、登録プロセス、前のコマンド(PRIVMSGおよび/または、JOINを)送っ意味

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

キーがサーバーから、この応答であります。

MOTDが終了した後、最初のメッセージを送信するのが通常の(そして良い)プラクティスです。 376コマンドを受信したときに発生します。


オフトピック発言:あなたの代わりにNickServにPRIVMSGを送るusing SASLを検討すべきです。これは今IRCで認証するための標準です。

+0

ok、私はJOINを送信する前に "time.sleep(1)"を追加しました。 – TAL

+0

私はちょうどそれを試みたが、私は同じ結果を得る – TAL

+0

それはない。あなたはJOINを送る前にPINGに**答え**をしなければならないので。 –