【问题标题】:Cofirmation on command (client.wait_for)确认命令 (client.wait_for)
【发布时间】:2022-01-20 13:33:14
【问题描述】:

我有点难过。我试图让机器人询问他们是否确定要从频道中删除 50 条消息,说是或否,但每当我尝试制作 2 个 client.wait_for 时,它们似乎都不起作用。这是我目前得到的:

@commands.guild_only()
@commands.cooldown(1, 15, commands.BucketType.user)
@commands.has_role("Developer")
async def purge(ctx,  amount=50):
  await ctx.reply("This will delete the most recent **50** messages from this channel, are you sure?\n`yes`, `no`")

  def check (message):
    return message.content == 'yes' and message.channel

  msglol = await client.wait_for('yes', check=check)
  await ctx.channel.purge(limit=amount.format(msglol))
  await ctx.reply("Done!", delete_after=5)

  def check (message):
     return message.content == 'no' and message.channel

  msglmao = await client.wait_for('no', check=check)
  await ctx.reply("Ok!".format(msglmao))```
     

【问题讨论】:

  • 也没有——不是我没有放“@client.command()”我忘了把它粘贴到代码中;-;

标签: discord discord.py


【解决方案1】:

正如 Chuaat 所说,client.wait_for 的正确语法是 wait_for(event, *, check=None, timeout=None) 但是,通过您对wait_for 的实现,很明显您首先不了解wait_for 的工作原理。

通过使用wait_for,您正在制作特定事件的一次性侦听器。代替事件,您可以插入所有带有特定检查的on_x(x 是通用名称)事件,只有在满足所有检查时才会触发检查。

例如,通过这样做:

def check(message: discord.Message): # typenhinting for better understand
    return message.author == ctx.author and message.channel == ctx.channel
msg = await client.wait_for('message', check=check) # msg is a discord.Message object
if msg.content.lower() == 'yes':
   return await ctx.send("oh cool, you said yes")
elif msg.content.lower() == 'no':
   return await ctx.send('oh cool, you said no')

机器人创建了一个一次性监听器,它将寻找一个“on_message”事件,其message.author == ctx.authormessage.channel == ctx.channel

从您的实施来看,您似乎认为您需要打开两个wait_for,一个代表“是”,一个代表“否”,但根本不是这样。

【讨论】:

  • 谢谢!!我想知道如何使用多个 wait_for
猜你喜欢
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多