【问题标题】:How do I make a Discord bot that has more than one command?如何制作具有多个命令的 Discord 机器人?
【发布时间】:2021-12-20 00:39:47
【问题描述】:

我正在尝试制作一个不和谐的机器人,但是当我尝试添加另一个命令时,第二个命令无法识别。这是相关代码:

@bot.command()
async def parrot(ctx, *, arg):
    await ctx.channel.send(arg)

async def talkparrot(ctx, *, arg):
    await ctx.channel.send(arg, tts=True)

当我输入“.parrot arg1”时,它工作正常,但“.taklparrot arg1”不起作用。这是为什么呢?

【问题讨论】:

  • 多次使用@bot.command。对于每个命令
  • 感谢@12944qwerty,如果您将其添加为答案将不接受它

标签: discord.py


【解决方案1】:

你需要多次应用装饰器:

@bot.command()
async def parrot(ctx, *, arg):
    await ctx.channel.send(arg)

@bot.command()
async def talkparrot(ctx, *, arg):
    await ctx.channel.send(arg, tts=True)

现在bot.command() 装饰器将应用于这两个函数。您可以根据需要将装饰器应用于任意数量的命令。不要将此装饰器应用于每个函数,但仅限于命令。

【讨论】:

    【解决方案2】:

    为了制作命令,您可以将快捷方式装饰器添加到函数中,它将将该函数转换为命令。

    这也是可重复的,因此您不必只为一个命令执行此操作,它会为您想要的每个功能执行此操作。

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 2022-11-12
      • 2018-05-04
      • 2021-03-11
      • 2021-07-07
      • 2018-05-06
      • 2021-10-06
      • 2021-08-14
      相关资源
      最近更新 更多