I have been trying to create a Twitch Plays stream for a few hours now, but I’m facing a hiccup when I run my code in Liclipse. The error message I’m getting is:
TypeError: unbound method twitch_connect() must be called with Twitch instance as first argument (got str instance instead)
Here’s my complete code:
# Import the necessary libraries
import twitch
import keypresser
import keyholder
t = twitch.Twitch
k = keypresser.Keypresser
seconds = 2
# Substitute your Twitch username and OAuth key below to connect to Twitch.
# You can generate your OAuth key at http://twitchapps.com/tmi/
username = "notgoingtoshow";
key = "notgoingtoshow";
t.twitch_connect(username, key)
# The primary loop
while True:
# Look for new messages
new_messages = t.twitch_recieve_messages();
if not new_messages:
# No new messages...
continue
else:
for message in new_messages:
# Great, we have a message. Let's gather some information from it
msg = message['message'].lower()
username = message['username'].lower()
print(username + ": " + msg);
# This is where you define which keys should be pressed and listened to.
# The following code will simulate key presses if respective inputs are received on Twitch.
# Adjust this according to your game's requirements!
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);
Can anyone help me resolve this issue?
You’re pointing to the class instead of creating an instance. When you write t = twitch.Twitch
, you’re just referencing the class definition - not an actual object. Methods like twitch_connect()
need to be called on a real instance so Python can automatically pass self
as the first argument. Since you’re calling it directly on the class, Python doesn’t know what to use for self
- that’s why you’re getting the “unbound method” error. I hit this same issue when I started with Twitch APIs. Easy fix: use t = twitch.Twitch()
with parentheses to create an actual instance. Then t.twitch_connect(username, key)
will work because Python can properly pass the instance as the first argument.
You’re encountering this issue because you’re calling methods on the class itself rather than an instance of it. When you do t = twitch.Twitch
, you’re merely assigning the class to a variable, which does not create an object. To resolve this, you need to instantiate it by adding parentheses: t = twitch.Twitch()
. The same goes for the keypresser; change k = keypresser.Keypresser
to k = keypresser.Keypresser()
. This is a common pitfall for those learning object-oriented programming in Python. The error arises because the method expects a Twitch instance (normally ‘self’) as the first argument, but you’re passing a string instead.
You’re assigning the class itself to your variables instead of creating instances. When you write t = twitch.Twitch
, you’re just pointing to the class, not making an object from it. So when Python hits t.twitch_connect(username, key)
, it’s looking for a Twitch instance as the first parameter, but there’s no self
getting passed automatically since you never actually created an instance. Python thinks your username string is supposed to be the Twitch instance. I’ve hit this same issue with socket connections for chat bots. Easy fix: change t = twitch.Twitch
to t = twitch.Twitch()
and k = keypresser.Keypresser
to k = keypresser.Keypresser()
. Those parentheses actually call the constructor and give you real objects to work with.
I get the frustration! You’re not creating instances of your classes. When you write t = twitch.Twitch
, you’re just referencing the class itself, not making an object from it. You need t = twitch.Twitch()
with the parentheses. Same thing with keypresser. That should fix it!