1

友達のライブ ストリーム用に Twitch ボットを作成しようとしていますが、メッセージを送信する (またはコマンドを認識する) のに問題があります。

私のコード:

import socket,string
HOST = "irc.twitch.tv"
PORT = 6667
NICK = "Axiom"
IDENT = "Axiom"
REALNAME = "Axiom"
CHANNEL = "#itwreckz"
PASSWORD = "oauth:PASSHERE"
readbuffer = ""
print "Connecting.."
t = socket.socket()
t.connect((HOST, PORT))
print "Connected! Logging in"
t.send("PASS %s\r\n" % PASSWORD)
t.send("NICK %s\r\n" % NICK)
t.send("USER %s %s BOT :%s\r\n" % (IDENT, HOST, REALNAME))
print "Logged in. Joining Channel"
t.send("JOIN %s\r\n" % CHANNEL)
print "JOINED!"

while 2>1:
    readbuffer = readbuffer+t.recv(1024)
    taco = string.split(readbuffer, "\n")
    readbuffer = taco.pop()
    for line in taco:
        line=string.rstrip(line)
        line=string.split(line)
        if len(line) > 3:
            print line
            command = line
            if "!test" in command:
                print "If you see this, it recognizes command." #Doesn't show
                t.send("PRIVMSG %s :IT WORKS!\r\n" % CHANNEL) #This either
        if(line[0]=="PING"):
            t.send("PONG %s\r\n" % line[1])

私がどこを台無しにしたのか誰か知っていますか?

チャットで送信されたメッセージが表示されるため、if len(line) >3:機能します (メッセージが 3 文字を超える場合のみ)。だから、私はそのようなものをうまくセットアップしたと思います。メッセージなどを印刷します。

コマンドを機能させることができないようです。


編集:これは非常に古い投稿ですが、IRC ボット全体をより適切に機能するように再作成したことを指摘したいと思います。

おそらく興味がある人のために:

import socket,time
network = 'irc.example.net'
port = 6667
channels = ['#Channel1','#Channel2'] #Add as many as you want
nick = 'myIRCBot'
identify = True
password = 'superAwesomePassword'
irc = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
print "Connecting to "+network+" ..."
irc.connect ((network, port))
print "Changing nick to "+nick+"..."
irc.send ('NICK '+nick+'\r\n')
if identify == True:
        print "Verifying password..."
        irc.send("PASS %s\n" % (password))
print "Setting login data..."
irc.send ('USER '+nick+' '+nick+' '+nick+' :'+nick+' IRC\r\n')
time.sleep(1)
for channel in channels:
        print "Joining "+channel+"..."
        irc.send ('JOIN '+channel+'\r\n')
        irc.send ('PRIVMSG '+channel+' :'+nick+' Started! Type .help for more\r\n')
        time.sleep(1)
print nick+" bot started."
while True:
        data = irc.recv(4096)
        if data.find('PING') != -1:
                irc.send('PONG '+data.split()[1]+'\r\n')
        try:
                user = data.split("!",1)[0].replace(":","",1)
                vhost = data.split(" ",1)[0].split("!",1)[1]
                dType = data.split(" ",1)[1].split(" ",1)[0]
                chan = data.split(" ",1)[1].split(" ",1)[1].split(" ",1)[0]
                msg = data.split(" ",1)[1].split(" ",1)[1].split(" ",1)[1].replace(":","",1).replace("\n","",1).replace("\r","",1)
                if msg == '.help':
                        irc.send ('PRIVMSG '+chan+' :This is the only command!\r\n')
                print user+" ("+vhost+") "+dType+" to "+chan+": "+msg
        except:
                pass
4

1 に答える 1

1

line=string.rstrip(line)では、行から末尾の空白を削除しています。次の行 ではline=string.split(line)、行を単語に分割しています。その後line、メッセージ内の各単語のリストです。実際、 をチェックlen(line) > 3すると、行に 3 つ以上の単語があるかどうかがチェックされます。

IRC メッセージからコマンドを抽出したい場合、最も簡単な (完全に信頼できるわけではありませんが) 方法は、今持っている単語のリストの最初の要素を取得することですif line[0] == "PING"。この場合、「PING」がコマンドです。

ここで、IRC プロトコル コマンドではなく、他のユーザーからメッセージとして送信されたコマンドを参照している場合、それは別の話です。プライベートまたはチャネルで受信したすべてのメッセージは、次の形式に従います。

PRIVMSG <message target> :<message received>

"!test"したがって、そのmessage received部分に含まれる他のユーザーからのメッセージを探している場合。最も可能性が高いのは、あなたの部分string.split(line)の最初の単語がただの代わりにあるときです。IRC privmsg 全体のメッセージ部分だけをそのまま残したい場合に本当に必要なことは、コロンを探して、それに続くすべてのものを取得することです。つまり、message received":!test""!test"

words = line.split()
if words[0] == "PRIVMSG":
    message = line[line.find(":"):][1:]
    message_words = message.split()
    if "!test" in message_words:
        print "If you see this, it recognizes the command"

この行message = line[line.find(":"):][1:]は少しごちゃごちゃしていますが、そこで起こっているのは":"、行内で最初に出現する を見つけて、そこからコロンを切り取っているだけです。

于 2014-07-26T17:54:23.840 に答える