在discord.py Cogs中使用' wait_for '



我正在创建一些东西,机器人会随机要求"打乱"这句话。问题,用户必须回答这个问题。现在,当我使用client.wait_for等待用户/s响应时,当我尝试使用断点时,wait_for完全停止代码。为什么会发生这种情况?我想我正在纠结如何在齿轮上使用它。这是我没有导入的齿轮代码:

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:nn`{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))

commands.Bot.wait_for()没有任何意义。您应该在实例上调用wait_for方法Bot类。它不是一个静态方法。

commands.Bot  # This is the name of a class
bot = commands.Bot(...)  # This is an instance of the commands.Bot class

要在Cog中访问它,最常见的解决方案是在创建Cog实例时将其作为参数传递给__init__()

文档中的示例也清楚地显示了这一点(使用client而不是discord.Client): https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=wait_for#discord.ext.commands.Bot.wait_for

接下来,wait_for的参数是错误的。你不能传递多个事件类型来等待。

wait_for("event", "message", ...)  # You can't do this

"event"对wait_for来说甚至都不是有效的东西,所以这两种方式都行不通。我不知道发生了什么事。甚至会触发。

最后,你创建了一个check函数,但是你传递的是check=None,所以你没有使用它…

请仔细查看文档页面(上面的链接)中的示例。

所有这些都应该给你一个错误信息,无论是在你的控制台还是在你的IDE中,虽然-所以不确定你是怎么到这里的&您可能没有正确配置logging

你在轨道上,但有一些错误。下面是你的代码片段:

def check(m):
return m.content == 'answer' and m.channel == message.channel
msg = await commands.Bot().wait_for("message")

您的检查功能对于您正在做的事情来说大多是正确的,您只需要将‘answer’替换为answer,然后变量answer将像chosen_list:answer = ‘find the answer in the txt file’

对于msg,你完全把它搞砸了。commands.Bot()是错误的,应该是bot或client,所以这里是bot。msg = await bot.wait_for('message', check=check, timeout=10)

' message '初始化bot正在等待消息,check等于我们在bot等待消息时运行的检查函数,timeout是bot在停止等待消息之前等待的时间长度。

下面是对wait_for

的文档引用

最新更新