【问题标题】:How do I make the bot tell the user that its error? discord.py如何让机器人告诉用户它的错误?不和谐.py
【发布时间】:2021-09-01 08:07:19
【问题描述】:

我自己做一个随机数命令,例子:
我:{commandprefix}随机
Bot : 请输入两个参数,一个数字和一个更大的数字。

我:{commandprefix}随机 1
Bot : 请输入两个参数,一个数字和一个更大的数字。

我:{commandprefix}随机 0.5 1.3
机器人:0.8

我:{commandprefix}随机 1 10
机器人:3

等等

就像我的例子一样,如果有任何错误(比如字符不是数字,只有 1 个参数等),机器人会自动告诉我们有错误。

这是我的代码:)

@client.command()
async def random(ctx, arg1, arg2):
    import random
    try:
        import random
        arg = random.randrange(float(arg1), float(arg2))
    else:
        embed = discord.Embed(title = "Random Number",
        description = f"Here is your random number : {arg}",
        color = discord.Color.red())

        now = datetime.now()
        current_time = now.strftime("%H:%M")

        embed.set_footer(text=f"Requested by {ctx.author} at {current_time}")

        await ctx.send(embed=embed)
  

它只能发送不是小数的随机数并且还不能检测到错误:(

【问题讨论】:

  • 什么用户?你,程序员?还是与机器人交互的不和谐用户(客户端)?你想怎么说?使用通知?
  • 如果你正在处理错误,它的here

标签: python discord discord.py bots


【解决方案1】:

我想我明白你在做什么了!从我的角度来看,如果缺少参数,您似乎正试图让机器人停止!这是我想出的:

@client.command()
async def random(ctx, arg1, arg2):
    import random
    if(arg1 and args2):
        try:
            import random
            arg = random.randrange(float(arg1), float(arg2))
        else:
            embed = discord.Embed(title = "Random Number",
            description = f"Here is your random number : {arg}",
            color = discord.Color.red())

            now = datetime.now()
            current_time = now.strftime("%H:%M")

            embed.set_footer(text=f"Requested by {ctx.author} at {current_time}")

            await ctx.send(embed=embed)
    else:
        ctx.send("You are missing some arguments! The command is random <Number 1> <Number 2")

这不是有条理和精确的,但这应该表明某种想法!希望这会有所帮助!

【讨论】:

  • python 中没有&amp;&amp; 运算符。相反,您应该使用and
  • 哎呀抱歉让我补充一下,忘了我和 js 和 python 混在一起了
【解决方案2】:

这是一个处理错误的例子,根据需要修改


@random.error
async def random_error(error, ctx):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("Wrong arguments")

See the docs for more info

【讨论】:

    【解决方案3】:

    各位,我已经找到答案了!你不用再回答了。

    @client.command()
    async def random(ctx, arg1=None, arg2=None):
        import random
        if str(type(arg1)) and str(type(arg2)) == "<class 'NoneType'>":
            await ctx.send("You are missing some arguments! The command is random <Number 1> <Number 2>")
        else:
            try: 
              import random
              arg = random.randrange(float(arg1), float(arg2))
    
              embed = discord.Embed(title = "Random Number",
              description = f"Here is your random number : {arg}",
              color = discord.Color.red())
    
              now = datetime.now()
              current_time = now.strftime("%H:%M")
    
              embed.set_footer(text=f"Requested by {ctx.author} at {current_time}")
    
              await ctx.send(embed=embed)
            except ValueError:
                await ctx.send("Please type a valid number")
    

    【讨论】:

    • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
    猜你喜欢
    • 2021-09-17
    • 2021-07-07
    • 2022-01-19
    • 2020-12-18
    • 2021-03-03
    • 1970-01-01
    • 2020-05-18
    • 2020-06-20
    • 2022-01-11
    相关资源
    最近更新 更多