【问题标题】:yes/no confirmation after command in disord.pydisord.py 中的命令后是/否确认
【发布时间】:2021-04-06 17:22:13
【问题描述】:

我正在使用 discord.py 库在 python 中制作一个不和谐机器人。我需要知道,如何在命令后实现是/否确认。例如,考虑一个“禁止”命令,该命令禁止用户访问服务器,并且只能由主持人使用。在写完“!ban @user”之后,我希望机器人回复“Ban @user?(y/n)”,如果那个版主回复的不是“y”,那么禁令就会被取消。我该如何实施?像这样的:

@client.command()
@commands.is_owner()
async def ban(ctx, member : discord.Member, *, reason = None):
    await ctx.send("Ban @member?(y/n)")
    if get_confirmation():
        await member.ban(reason = reason)
    else:
        await ctx.send("Ban Cancelled")

【问题讨论】:

    标签: python discord.py


    【解决方案1】:
    @client.command()
    @commands.is_owner()
    async def ban(ctx, member : discord.Member, reason = None):
        await ctx.send(f"Ban {member.mention}?(yes/no)")
        @client.command()
        @commands.is_owner()
        async def yes(ctx, member : discord.member, reson=None):
            await member.ban(reason = reason)
        @client.command()
        @commands.is_owner()
        async def no(ctx):
            await ctx.send("Ban Cancelled")
    

    【讨论】:

      【解决方案2】:

      您需要使用wait_for

      以下是修改后的代码:

      @client.command()
      @commands.is_owner()
      async def ban(ctx, member : discord.Member, *, reason = None):
          await ctx.send(f"Ban {member.mention}?(y/n)")
          msg = await bot.wait_for("message", check=lambda m:m.author==ctx.author and m.channel.id==ctx.channel.id)
          if msg.content.lower in ("y", "yes"):
              await member.ban(reason = reason)
          else:
              await ctx.send("Ban Cancelled")
      

      【讨论】:

        【解决方案3】:

        好吧,经过一番挖掘,我找到了this post。希望对遇到同样问题的人有所帮助

        【讨论】:

        • 如果您的问题是重复的,您可以删除它。
        • @takendarkk 它可能只是帮助别人:)
        猜你喜欢
        • 1970-01-01
        • 2022-01-20
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 2023-02-03
        相关资源
        最近更新 更多