我试过自己做,但似乎不起作用。这是我唯一能想到的办法。
bot1 = commands.Bot(command_prefix='!')
bot2 = commands.Bot(command_prefix='?')
...
bot1.run('token')
bot2.run('token')
编辑:以下是我想要执行的命令类型的示例
py = commands.Bot(command_prefix='py')
js = commands.Bot(command_prefix='js')
@py.command("if")
async def py_if(ctx):
ctx.send("if <cond>:")
@js.command("if")
async def js_if(ctx):
ctx.send("if (<cond>){ }")
py.run('token')
js.run('token')
在本例中,多个前缀允许您为特定语言使用不同的前缀。
bot1.run('token')
bot2.run('token')
你不能这样做。bot1.run('token')
是阻塞呼叫。则不执行bot2.run('token')
。因此,只有bot1
将联机。
正如Roman在bot = commands.Bot(command_prefix=('!', '?'))
中所说,同一命令可以有多个前缀。
@py.command("if")
async def py_if(ctx):
ctx.send("if <cond>:")
@js.command("if")
async def js_if(ctx):
ctx.send("if (<cond>){ }")
如果你想把一个命令绑定到一个特定的前缀,你必须做一些类似于的事情
bot = commands.Bot(command_prefix=('!', '?'))
@bot.command("myCommand")
async def myCommand(ctx):
if ctx.prefix == "!":
await ctx.send("Command was invoked with ! prefix")
bot.run('token')
参见https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=prefix#discord.ext.commands.Context.prefix
或:
bot = commands.Bot(command_prefix=('!', '?'))
def check_prefix(ctx):
return ctx.prefix == "!"
@bot.command("myCommand")
@commands.check(check_prefix)
async def myCommand(ctx):
await ctx.send("Command was invoked with ! prefix")
bot.run('token')
我在不一致.py文档中发现了这一点:
命令前缀是消息内容最初必须包含的内容,才能调用命令。这个前缀可以是指示前缀应该是什么的字符串,也可以是将bot作为其第一个参数和discord的可调用前缀。消息作为其第二个参数,并返回前缀。这是为了方便使用"动态"命令前缀。这个可调用函数可以是正则函数,也可以是协程。
此外,您还可以使用前缀字符串的集合。例如:
import random
bot = commands.Bot(command_prefix=('!', '?'))
@bot.command(name='random')
async def my_random(ctx):
await ctx.send(random.random())
bot.run(TOKEN)
访问文档
编辑:来自文档链接:
命令前缀也可以是字符串的可迭代项,指示应该使用对前缀的多个检查,第一个匹配的将是调用前缀。您可以通过Context.prefix获取此前缀。为了避免混淆,不允许使用空的迭代。