【问题标题】:Accept a second input based on the first one in discord.py根据 discord.py 中的第一个输入接受第二个输入
【发布时间】:2021-08-17 15:09:04
【问题描述】:

我希望我的机器人有一个删除频道中所有消息的命令。

@pip.command()
async def clear(ctx, amount):
    if amount == "all":
        await ctx.send("Are you sure you want to delete all messages in this channel?")
        async def yes(ctx):
            if ctx == "yes":
                await ctx.channel.purge(limit=100000000)

这是我的代码。

它会向我发送消息,询问我是否确定要删除所有消息,但在那之后,当我输入“pip yes”时它什么也不做(pip 是我的前缀)我该如何解决这个问题?

感谢您的帮助!

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

您应该进一步查看client.wait_for。它在不和谐的构建功能中。纯粹的!

import asyncio

@client.command()
async def clear(ctx, amount):
   if amount == "all":
      await ctx.send("Are you sure you want to delete all messages in this channel?")

      def check(message):
         if message.author == ctx.author:
            return ctx.author == message.author # Or just return True

      try:
         answer = await client.wait_for('message', timeout = 20.0, check = check)
         if answer.content == "yes":
            await ctx.channel.purge(limit=100000000)
         else:
            # Do something

      except asyncio.TimeoutError:
         await ctx.send('Timeout')

【讨论】:

  • 我尝试实现您的代码,其中大部分运行良好,但即使我输入“是”,它也会运行 else 代码而不是 if 代码。你能帮我弄清楚这是为什么吗?非常感谢您的帮助,您链接的文档对我帮助很大!
  • @pip.command() async def clear(ctx, amount): if amount == "all": await ctx.send("Are you sure you want to delete all messages in this channel?") def check(message): if message.author == ctx.author: return ctx.author == message.author try: answer = await pip.wait_for("message", timeout = 20.0, check = check) if answer == "yes": await ctx.channel.purge(limit=1000000) else: print("failed") except asyncio.TimeoutError: await ctx.send('Timeout')
  • 我的错,应该是answer.content。随意upvote并标记我的答案!纯粹的伙伴!
猜你喜欢
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多