【问题标题】:is it possible to have different commands for different prefixes in discord.py?是否可以在 discord.py 中为不同的前缀使用不同的命令?
【发布时间】:2022-01-22 20:44:04
【问题描述】:

每个前缀可以有不同的命令吗?例如,一个命令用于!,另一个命令用于? 例如,某人执行!foo 机器人响应"foo",但另一个人执行?foo 机器人响应"bar"

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    您可以制作一个带有两个前缀的机器人并创建一个foo 命令。然后检查使用 ctx.prefix 调用命令时使用的前缀。

    from discord.ext import commands
    
    bot = commands.Bot(command_prefix=("!", "?"))
    
    
    @bot.command()
    async def foo(ctx):
        await ctx.send("foo" if ctx.prefix == "!" else "bar")
    
    bot.run("TOKEN")
    

    或者您可以使用on_message 事件手动完成:

    @bot.event
    async def on_message(message):
        if message.content.lower() == "!foo":
            await message.channel.send("foo")
        elif message.content.lower() == "?foo":
            await message.channel.send("bar")
        await bot.process_commands(message)
    

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2021-04-04
      • 1970-01-01
      • 2019-11-09
      • 2021-02-07
      • 2014-06-03
      • 1970-01-01
      • 2023-03-20
      • 2021-02-25
      相关资源
      最近更新 更多