如何在discord.py中创建多个同名的discord命令?

时间:2021-01-06 04:57:06

标签: python discord.py

如何在 Discord.py 中添加多个同名命令? 例如:

@client.command(aliases=["dices"])
async def dice(ctx, num):
  try:
    num=int(num)
    bla bla bla
  except ValueError:
    await ctx.send("Invalid Number!")

@client.command(name='dice',aliases=["dices"])
async def dice_no_param(ctx):
  try:
      roll = random.randint(1,6)
      bla bla bla
  except ValueError:
    await ctx.send("Invalid Number!")

但很明显,我得到了一个错误。

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import bot
  File "/home/runner/HamburgerBot/bot.py", line 147, in <module>
    async def dice_no_param(ctx):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1163, in decorator
    self.add_command(result)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1071, in add_command
    raise discord.ClientException('Command {0.name} is already registered.'.format(command))
discord.errors.ClientException: Command dice is already registered.

1 个答案:

答案 0 :(得分:1)

你不能不为了你的 dice 命令目的,你可以这样做

@client.command(name='dice', aliases=['dices'])
async def dice(ctx, num=None):
    num = num or random.randint(1, 6)
    try:
        num = int(num)
    except ValueError:
        return await ctx.send("Invalid Number!")
    # bla bla bla other code