首页 > 解决方案 > 如何让机器人告诉用户它的错误?不和谐.py

问题描述

我一个人做一个随机数命令,例如:
我:{commandprefix}random
机器人:请输入两个参数,一个数字和一个更大的数字。

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

我:{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)
  

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

标签: pythondiscorddiscord.pybots

解决方案


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

@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")

这没有组织和精确,但这应该表明某种想法!希望这可以帮助!


推荐阅读