Hey everyone! I'm learning to code and decided to make a Discord bot for my gaming group. I'm using Python with the discord.py library.
Here's what I want the bot to do:
1. Send a message asking who wants to join a game
2. Keep track of users who react with a checkmark
3. Start a timer when requested
4. Notify the users who reacted when the timer ends
I've got some of the code working, but I'm stuck on how to combine the user list with the timer function. Can anyone help me figure out how to:
1. Store the users who react in a list
2. Use that list in the timer function to mention everyone when it's done
Any tips or code examples would be super helpful! Thanks in advance!
I’ve been working on a similar project for my gaming group, and I can share some insights that might help you out. One approach that worked well for me was using a combination of asyncio and a custom class to manage game sessions.
For storing users who react, I created a GameSession class that holds a list of participants and the timer status. When a user reacts, I add them to the session’s participant list.
The timer function was a bit tricky, but I found that using asyncio.create_task() to run the timer in the background worked great. Once the timer finishes, it automatically notifies all participants in the session.
Here’s a rough outline of how I structured it:
class GameSession:
def __init__(self):
self.participants = []
self.timer_task = None
async def start_timer(self, duration, channel):
await asyncio.sleep(duration)
await channel.send(f'Time's up! {' '.join(self.participants)}')
game_sessions = {}
@bot.command()
async def create_game(ctx):
game_sessions[ctx.message.id] = GameSession()
# Rest of your code here
@bot.event
async def on_reaction_add(reaction, user):
if reaction.message.id in game_sessions:
game_sessions[reaction.message.id].participants.append(user.mention)
@bot.command()
async def start_timer(ctx, duration: int):
if ctx.message.id in game_sessions:
session = game_sessions[ctx.message.id]
session.timer_task = asyncio.create_task(session.start_timer(duration, ctx.channel))
This approach has been working well for me, but it did take some trial and error to get right. Let me know if you need any clarification on how it works!
Sounds like an interesting project, John_Fast. To combine the user list with the timer function, you could create a dictionary to store game sessions. Each session would have a unique identifier, a list of participants, and the timer status.
When users react, add them to the corresponding session’s participant list. For the timer, you can use asyncio.sleep() to create a non-blocking delay. Once the timer finishes, iterate through the participant list to mention everyone.
Here’s a basic structure:
game_sessions = {}
@bot.command()
async def start_game(ctx):
session_id = ctx.message.id
game_sessions[session_id] = {‘participants’: , ‘timer_active’: False}
@bot.event
async def on_reaction_add(reaction, user):
session_id = reaction.message.id
if session_id in game_sessions:
game_sessions[session_id][‘participants’].append(user.mention)
This approach should help you organize your bot’s functionality more efficiently.
Hey John_Fast! Cool project! For storing users, use a global list and append usernames when they react. In the timer function, loop through that list to mention everyone. Something like:
users =
@bot.event
async def on_reaction_add(reaction, user):
users.append(user.mention)
Then use users in your timer function. Hope this helps!