【问题标题】:Discord.py The command is already an existing command or alias. errorDiscord.py 命令已经是现有命令或别名。错误
【发布时间】:2022-01-09 18:03:39
【问题描述】:

我是 python 新手,标题中出现错误。我知道问题出在哪里,但我想根据用户在命令中提供的论点创建一个命令来提供不同的结果。

例如,如果它将是“!sens 20 cm 800 dpi”,机器人会回复“{} ingame”,但如果“!sens 10 ingame 800 dpi”,机器人会回复“{} cm”

我不想创建两个不同名称的命令。

 import math
    import discord
    from discord.ext import commands
    TOKEN = ''
    bot = commands.Bot(command_prefix='!')
    d= 166461.6
    @bot.command(name="sens")
    async def sens_cm(ctx, x: float,cm, y: float,dpi):
        await ctx.send('{} ingame'.format(d/(x*y)))
    @bot.command(name="sens")
    async def sens(ctx, x1: float,ingame, y1: float,dpi):
        await ctx.send('{} cm'.format(d/(x1*y1)))
    bot.run(TOKEN)

【问题讨论】:

    标签: discord discord.py bots


    【解决方案1】:

    为了解决此类问题,您可以创建一个 if-else 语句进行检查,如下面的代码所示

    @bot.command(name="sens")
    async def sens_cm(ctx, x: float,cm:str, y: float,dpi):
      if cm=="ingame":
           await ctx.send(f'{d/(x*y)} cm')
      elif cm=="cm":
           await ctx.send(f'{d/(x*y)} ingame')
      else:
           await ctx.send("Invalid argument")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 2021-03-23
      • 2022-10-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      相关资源
      最近更新 更多