Issue
I have this bot that sends a particular message to the channel whenever a particular user's name is mentioned (using on_message()
). And if I get tired of it and want it to stop, I just react to whatever it sent with a thumbs down within 2 minutes, and it stops.
Now, I also have another command that can take in that person's name as an argument, like .command @person_name
. My problem is that, when I call the command with that specific person's name, the bot first posts the reaction message (which is fine), and then waits for two full minutes for the reaction, before it times out and moves on to the command
body. How can I make the waitfor()
function run in the background?
Here's a generalized gist of my code:
async def on_message(message):
if "person_name" in message.content:
await message.channel.send("sample reaction message")
await fun() #check for reaction
... #other things
await bot.process_commands(message)
async def fun(message):
content = message.content
channel = message.channel
global VARIABLE #this variable decides whether the bot should react to the person's name
def check(reaction,user):
return user.id == bot.owner_id and str(reaction.emoji) == '👎'
try: reaction,user = await bot.wait_for('reaction_add',timeout = (60 * 2),check=check)
except asyncio.TimeoutError: pass
else:
if str(reaction.emoji) == '👎':
VARIABLE = False #disable the reaction message
await channel.send(" Okay, I'll stop.")
Solution
Using the on_reaction_add event will allow you to do this. I'd advise against using global variables though. The event will trigger whenever someone reacts to a cached message. You will want to use on_raw_reaction_add
if you want this to work even after a bot restart.
Add this function to your bot:
async def on_reaction_add(reaction, user):
if bot.is_owner(user) and str(reaction.emoji) == "👎":
VARIABLE = False
Answered By - Genetical
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.