1+ from twitchio .ext import commands
2+
3+
4+ class Bot (commands .Bot ):
5+
6+ def __init__ (self ):
7+ # Initialise our Bot with our access token, prefix and a list of channels to join on boot...
8+ # prefix can be a callable, which returns a list of strings or a string...
9+ # initial_channels can also be a callable which returns a list of strings...
10+ super ().__init__ (token = 'ACCESS_TOKEN' , prefix = '?' , initial_channels = ['...' ])
11+
12+ async def event_ready (self ):
13+ # Notify us when everything is ready!
14+ # We are logged in and ready to chat and use commands...
15+ print (f'Logged in as | { self .nick } ' )
16+ print (f'User id is | { self .user_id } ' )
17+
18+ async def event_message (self , message ):
19+ # Messages with echo set to True are messages sent by the bot...
20+ # For now we just want to ignore them...
21+ if message .echo :
22+ return
23+
24+ # Print the contents of our message to console...
25+ print (message .content )
26+
27+ # Since we have commands and are overriding the default `event_message`
28+ # We must let the bot know we want to handle and invoke our commands...
29+ await self .handle_commands (message )
30+
31+ @commands .command ()
32+ async def hello (self , ctx : commands .Context ):
33+ # Here we have a command hello, we can invoke our command with our prefix and command name
34+ # e.g ?hello
35+ # We can also give our commands aliases (different names) to invoke with.
36+
37+ # Send a hello back!
38+ # Sending a reply back to the channel is easy... Below is an example.
39+ await ctx .send (f'Hello { ctx .author .name } !' )
40+
41+
42+ bot = Bot ()
43+ bot .run ()
44+ # bot.run() is blocking and will stop execution of any below code here until stopped or closed.
0 commit comments