talkomatic.py 0.1.2
An easy-to-use Python versatile wrapper for the Talkomatic Bot interface ensuring best practices for making bots, and handling the REST API.
 
Loading...
Searching...
No Matches
vote_guard.py
Go to the documentation of this file.
1"""
2vote_guard.py
3
4This example shows making a bot keeping track of votes, and banned users.
5"""
6
7
8from talkomatic import Bot, RoomType # import the Bot class from the talkomatic package
9
10
11bot = Bot() # create a new Bot instance
12
13# with this decorator, the function will be called when the bot finishes connecting to the server
14@bot.on_connect
15async def on_connect() -> None:
16 # we find the first non-full room, and we join it
17 for room in bot.rooms:
18 if (not room.is_full) and room.room_type == RoomType.PUBLIC:
19 await bot.join_room(room)
20 return
21 print("We didn't find any non-full rooms to join. :(") # :(
22 await bot.disconnect()
23
24# this function will execute when:
25@bot.on_room_join # - when the bot joins a room
26@bot.on_user_vote # - when a user votes
27@bot.on_user_leave # - when a user leaves
28async def update_message(*args) -> None: # we use *args here since we don't care about the arguments
29 bot_message = "Votes:\n\n"
30 for voted, voters in bot.current_room.votes.items(): # we iterate over the voted user, and the voters
31 voted_user = bot.get_user_by_id(voted.id) # we try to resolve the voted user, if we can't, we just use the ID-only one
32 bot_message += f"{str(voted_user if voted_user != None else voted)}\n" # we add the name the voted user
33 for voter in voters: # for each voter that's voted for that user
34 voter_user = bot.get_user_by_id(voter.id) # we try to resolve the voter user, if we can't, we just use the ID-only one
35 bot_message += f" - {str(voter_user if voter_user != None else voter)}\n" # we add the name of the voter
36 bot_message += "\n" # we add a new line after each user's votes (for readability)
37 if len(bot.current_room.votes) == 0: # no voted users!
38 bot_message += "No users are currently voting for anyone to be banned.\n"
39
40 if (bot.current_room.banned_users != None) and (bot.current_room.banned_users != []): # there are banned users!
41 bot_message += "\nBanned users:\n"
42 for banned_user in bot.current_room.banned_users: # we display the banned users' IDs
43 bot_message += f" - User with ID: {banned_user.id}\n"
44
45 await bot.send_message(bot_message) # we send the bot's message!
46
47# we're ready to go! let's run the bot with a username and location
48bot.run("Vote Guard", "Bot")
None on_connect()
Definition vote_guard.py:15
None update_message(*args)
Definition vote_guard.py:28