gpt4 book ai didi

python - 是否可以在 discord.py 中对不同的前缀使用不同的命令?

转载 作者:行者123 更新时间:2023-12-05 04:39:29 24 4
gpt4 key购买 nike

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

最佳答案

您可以制作一个带有两个前缀的机器人并创建一个 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)

关于python - 是否可以在 discord.py 中对不同的前缀使用不同的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70435262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com