2017-01-12 4 views
1

私はPython 2.7を使用しています。私はtwitch用のIRC botを作成しようとしていますが、問題があります。私は「webchat.freenode.net」のような他のIRCでボットを作成し、すべてが単収縮のためok..Myコードだった:twitch irc botを作成する(ログイン認証に失敗しました)

import time 
import socket 


HOST = "irc.twitch.tv" 
PORT = 6667 
BOTNICK = "thebot" 
PASSWORD = "oauth:nph788dap10fu6ozlzv1b32fzm4r8q" 
CHAN = "#fordotis10" 




irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
irc.connect((HOST, PORT)) 
irc.setblocking(False) 
time.sleep(1) 

irc.send("PASS "+PASSWORD+"\r\n") 

time.sleep(1) 

irc.send("USER "+BOTNICK+" "+BOTNICK+" "+BOTNICK+" :I AM BOT!\r\n") 

time.sleep(1) 

irc.send("NICK "+BOTNICK+"\r\n") 
time.sleep(1) 

irc.send("JOIN "+CHAN+"\r\n") 

text= "" 
while 1: 
    try: 
     text = irc.recv(1024) 
     print text 
    except Exception: 
     pass 
    if text.find("PING")!=-1: 
     irc.send("PONG "+text.split()[1]+"\r\n") 

このコードでは、私は次のエラーを取得する:

:tmi.twitch .tv NOTICE *:ログイン認証に失敗しました

何が欠けていますか?

+0

私がやっていたことがあれば、すでに働いているものを見つけようとして、その2つを比較しようとします。おそらくこのような何かが助けになるでしょうか? https://github.com/aidanrwt/twitch-bot –

+0

あなたの答えをありがとう、私は今使用するhttp://pytwitcherapi.readthedocs.io/en/latest/と作品! – AlexDotis

+0

将来の読者のためにあなた自身の答えを書いてください(そして受け入れてください)! –

答えて

0

thisページを使用してわかりますから、HOST = "irc.twitch.tv"HOST = irc.chat.twitch.tv"です。

+0

私はそれを試しましたが何もない、同じエラー – AlexDotis

+0

私はこれをテストするための何も持っていませんが、おそらく 'USER'メッセージを削除すると動作します。また、ユーザー名とOAuthトークンの正当性を再度確認します。 – Thelmund

0

最後に、私のコードに問題が見つかりました!だから、私は2つの方法でツイッチボットを作成する手順を記述します!

最初の方法:あなたはアカウントを作成する必要があります

  1. import socket 
    import re 
    
    
    HOST = "irc.twitch.tv" 
    
    PORT = 6667 
    
    NICK = "botname" #The account Name 
    
    PASS = "The password here" #http://www.twitchapps.com/tmi/ 
    
    CHAN = "#Channel" 
    
    
    s = socket.socket() 
    
    s.connect((HOST, PORT)) 
    
    s.send("PASS {}\r\n".format(PASS)) 
    
    s.send("NICK {}\r\n".format(NICK)) 
    
    s.send("JOIN {}\r\n".format(CHAN)) 
    
    while True: 
        resp = s.recv(1024) 
        print resp 
        if resp == "PING :tmi.twitch.tv\r\n": 
         s.send("PONG :tmi.twitch.tv\r\n") 
        if resp.find("hi")!=-1: 
         s.send("PRIVMSG "+CHAN+" :HELLO\r\n") 
    
    
        resp = "" 
    

第二の方法けいれんする:

私はpytwitcherapiを見つけたが、あなたはからドキュメントを読むことができます。http://pytwitcherapi.readthedocs.io/en/latest/

import pytwitcherapi 
import time 
import webbrowser 
import threading 
import queue 

session = pytwitcherapi.TwitchSession() 

url = session.get_auth_url() 

session.start_login_server() 

webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open(url) #OAuth generator 

while not session.authorized: 
    time.sleep(1) 

time.sleep(2) 

print "Authorized" 

session.shutdown_login_server() 

channel = session.get_channel("ChannelName") 

client = pytwitcherapi.IRCClient(session,channel) 

t = threading.Thread(target = client.process_forever) 
t.start() 

print "connected" 

while True: 
    try: 
     m = client.messages.get(False) 
     if m.text == "!ping": 
      client.send_msg("pong!") 
     if m.text =="hey": 
      client.send_msg("Hey You!") 
    except queue.Empty: 
     pass 

・ホープすべてこのヘルプお!

関連する問題