Python TypeError:必须使用twitch instan调用未绑定的方法twitch\u connect()

2024-06-16 08:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

几个小时以来,我一直在乱搞代码,让Twitch在Twitch上播放流媒体。我刚到最后,试着用李唇膏,但它说。。。你知道吗

TypeError: unbound method twitch_connect() must be called with Twitch instance as first argument (got str instance instead)

以下是完整代码:

#Define the imports
import twitch
import keypresser
import keyholder
t = twitch.Twitch
k = keypresser.Keypresser
seconds = 2

#Enter your twitch username and oauth-key below, and the app connects to twitch with the details.
#Your oauth-key can be generated at http://twitchapps.com/tmi/
username = "notgoingtoshow";
key = "notgoingtoshow";
t.twitch_connect(username, key)

#The main loop
while True:
    #Check for new mesasages
    new_messages = t.twitch_recieve_messages();

    if not new_messages:
        #No new messages...
        continue
    else:
        for message in new_messages:
            #Wuhu we got a message. Let's extract some details from it
            msg = message['message'].lower()
            username = message['username'].lower()
            print(username + ": " + msg);

            #This is where you change the keys that shall be pressed and listened to.
            #The code below will simulate the key q if "q" is typed into twitch by someone
            #.. the same thing with "w"
            #Change this to make Twitch fit to your game!
            if msg == "start": k.key_press("enter");
            if msg == "b": keyholder.holdForSeconds(key, seconds);
            if msg == "a": keyholder.holdForSeconds(key, seconds);
            if msg == "up": keyholder.holdForSeconds(key, seconds);
            if msg == "down": keyholder.holdForSeconds(key, seconds);
            if msg == "left": keyholder.holdForSeconds(key, seconds);
            if msg == "right": keyholder.holdForSeconds(key, seconds);

有什么解决办法吗?你知道吗


Tags: thetokeymessagenewifusernamemsg
1条回答
网友
1楼 · 发布于 2024-06-16 08:24:01

从类对象调用twitch_connect不是实例,但是twitch_connect方法将self(或类的实例)作为第一个参数:

t = twitch.Twitch

应该使用()实例化类:

t = twitch.Twitch()
t.twitch_connect(username, key)

只能从类本身调用静态方法和类方法。但这是另一个不同的故事。你知道吗

相关问题 更多 >