Reputation: 56
I'm creating something where the bot will randomly ask a "scramble this sentence" question and the user will have to answer the question. Now when I use client.wait_for
to wait for the user/s response, the wait_for
stops the code completely when I tried using breakpoints. Why does this happen? I think I'm struggling on how to use this in a cog. This is my cog code without imports:
class firework_economy(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(self, message):
if message.author.bot:
return
else:
rand_num = randint(1, 10)
print(rand_num)
if rand_num == 5:
ss = open('scrambled sentences.txt','r')
sslist = ss.readlines()
chosen_sentence = choice(sslist)
await message.reply(f"Unscramble the following sentence for a chance to win a firework:\n\n`{chosen_sentence}`")
def check(m):
return m.content == 'answer' and m.channel == message.channel
msg = await commands.Bot().wait_for("message")
await message.channel.send(f"{msg.author.mention} was first!")
async def setup(bot):
await bot.add_cog(firework_economy(bot))
Upvotes: 0
Views: 656
Reputation: 68
You’re on track but there are a few errors. Here’s a snippet of your code.
def check(m):
return m.content == 'answer' and m.channel == message.channel
msg = await commands.Bot().wait_for("message")
Your check function is mostly correct for what you are doing, you just need to replace the ‘answer’
with answer
and then the variable answer
would be like chosen_list
: answer = ‘find the answer in the txt file’
For msg you have totally messed it up.
commands.Bot() is wrong and should be bot or client so here bot.
msg = await bot.wait_for('message', check=check, timeout=10)
‘message’ initialises that the bot is waiting for a message, check is equal to our check function that will run when the bot is waiting for a message and timeout is the length of time the bot will wait before it stops waiting for a message.
Here is the docs reference on wait_for
Upvotes: 1
Reputation: 5650
commands.Bot.wait_for()
doesn't make any sense. You're supposed to call the wait_for
method on an instance of the Bot
class. It's not a static method.
commands.Bot # This is the name of a class
bot = commands.Bot(...) # This is an instance of the commands.Bot class
To get access to it in a Cog
, the most common solution is to pass it in as an argument to the __init__()
when you create the Cog
instance.
The examples in the docs also clearly show this (using client
instead of discord.Client
): https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=wait_for#discord.ext.commands.Bot.wait_for
Next, the arguments to wait_for
are wrong. You can't pass multiple event types to wait for.
wait_for("event", "message", ...) # You can't do this
"event" isn't even a valid thing to wait_for
, so that wouldn't work either way. I wouldn't know what "event" would even be triggered by.
Lastly, you made a check
function but you're passing check=None
so you're not using it...
Take a close look at the examples in the docs page (linked above).
All of these should be giving you an error message, both in your console and in your IDE, though - so not sure how you got here & you may not have configured logging
properly.
Upvotes: 1