I have a bot that every 1 to 12 hours sends a random message into a specific channel (the channel that the command !random is used in). A certain handful of these message are yes/no questions, and I want to make it that the bot will respond if somebody says yes/no.
The code works, the issue is I want the bot to respond ONLY if the person answered yes/no in the same channel the bot sent the original message, not just any channel (because this may disrupt other actual conversations).
The issue is, the bot seems to always think the id entered for message.channel.id is wrong. I’ve triple checked and it is indeed the correct channel where I used the command and the bot is sending the messages there, it is also where I’m replying to the bot as I would want for the “if” condition to work. Here’s the code:
@bot.event async def on_message(message): if message.author == bot.user: return global newvalue if newvalue == True: if 'no' in message.content and message.channel.id == "<#988993107368476692>": await message.channel.send("first response") newvalue = False return if 'yes' in message.content and message.channel.id == "<#988993107368476692>": await message.channel.send("second response") newvalue = False return else: print("nothing to say!") return await bot.process_commands(message)
Like I said, the code before I added message.channel.id worked fine, except that if I answered the bot in a different channel, it would answer me back in that new channel instead of not reading the message at all (which is what I want, hence this code).
But, even though I’m keeping it to the correct channel, my terminal keeps printing “nothing to say!”, meaning it’s passing over those two if statements now.
I’ve tried making it 2 separate if statements (if ‘no’: if in channel: etc) but that doesn’t work either.
tldr how do I make a bot respond only if a user’s message is sent to a specific channel?
Advertisement
Answer
Ah, turns out I’m stupid lol. I had to do what @anoor1234 suggested but make it a separate if function
if newvalue == True: if 'no' in message.content: if message.channel.id == 988993107368476692: await message.channel.send("first response") newvalue = False return if 'yes' in message.content: if message.channel.id == 988993107368476692: await message.channel.send("second response") newvalue = False return
This fixed it.