I’ve developed a Discord bot for our gaming events. It currently lists users who react with a thumbs-up and posts their names in a new thread when triggered by the message author adding a money bag reaction.
We want users to send screenshots in these threads and add a money bag reaction to the previous message. However, they often forget the reaction. I’d like to enhance the bot to:
- Notify when someone adds the money bag reaction
- Ping users after 20 hours if they haven’t added the reaction
My current code:
- Prevents money bag reactions without a skull reaction
- Filters messages
- Lists users who reacted with thumbs-up
- Creates a new thread
- Posts user names in the thread
I’m unsure whether to implement the new features in on_reaction_add or on_thread_create, or maybe both. Any advice? I’m new to Python and could use some guidance.
async def handle_reaction(reaction, user):
if user == reaction.message.author and reaction.emoji == '💼':
channel = bot.get_channel(PAYMENTS_CHANNEL_ID)
if not any(r.emoji == '💀' for r in reaction.message.reactions):
await reaction.remove(user)
return
content = re.sub(r'^<@&\d+> ', '', reaction.message.content)
new_msg = await channel.send(f'{content} by {user.mention}')
await new_msg.add_reaction('💼')
thumbs_up_users = [u.mention for r in reaction.message.reactions
if r.emoji == '👍'
for u in await r.users().flatten()
if u != bot.user]
thread = await channel.create_thread(
name=f'{content} by {user.display_name}',
auto_archive_duration=1440
)
await thread.send(', '.join(thumbs_up_users))
have u thought about using discord.py’s tasks? u could set up a task to check threads periodically for screenshots and reactions. maybe combine that with on_message for real-time monitoring. might be easier than messing with on_reaction_add. just an idea, hope it helps!
Considering your requirements, I’d suggest implementing a combination of event listeners and background tasks. For notifying when someone adds the money bag reaction, you can enhance your existing on_reaction_add event. As for pinging users after 20 hours, a background task using discord.ext.tasks would be ideal.
Here’s a high-level approach:
- Modify on_reaction_add to send a notification when a money bag reaction is added.
- Create a background task that checks threads every hour for missing reactions.
- In the task, maintain a dictionary of thread creation times and user lists.
- If 20 hours have passed and reactions are missing, ping the relevant users.
This setup should provide real-time notifications and periodic checks without overcomplicating your code structure. Remember to handle edge cases and potential race conditions.
As someone who’s worked on similar Discord bots, I can offer some insights. I’d recommend using a combination of event listeners and scheduled tasks for this.
For notifying about money bag reactions, extend your on_reaction_add function. When a money bag is added, send a message in the thread to acknowledge it.
For the 20-hour ping, use discord.ext.tasks to create a background task. Have it run every hour, checking threads created in the last 24 hours. If a thread is over 20 hours old and lacks a money bag reaction, ping the relevant users.
Store thread info (creation time, participants) in a database or JSON file. This helps track which threads need checking and who to ping.
Remember to handle edge cases, like deleted threads or users leaving the server. Also, consider adding a command for manually triggering checks or resetting timers.
This approach should give you real-time notifications and reliable delayed pings without overhauling your existing code.