Discord.py - 使用命令更改前缀
Discord.py - Changing prefix with command
我想创建一个管理员可以更改命令前缀的命令(例如:不使用“.”,他们可以将其更改为“-”,只有“-”才有效)我' d 能够设置权限,使只有管理员能够使用命令
我四处查看,通过文档和互联网,但没有找到任何东西,我也不知道如何做到这一点
你应该使用 command_prefix
argument for discord.Bot
它接受一个字符串 (意思是一个机器人宽前缀) 或一个可调用的 (意思是一个函数 returns 基于条件的前缀)。
您的条件取决于调用 message
。因此,您可以允许公会定义自己的前缀。我将使用字典作为一个简单的例子:
...
custom_prefixes = {}
#You'd need to have some sort of persistance here,
#possibly using the json module to save and load
#or a database
default_prefixes = ['.']
async def determine_prefix(bot, message):
guild = message.guild
#Only allow custom prefixs in guild
if guild:
return custom_prefixes.get(guild.id, default_prefixes)
else:
return default_prefixes
bot = commands.Bot(command_prefix = determine_prefix, ...)
bot.run()
@commands.command()
@commands.guild_only()
async def setprefix(self, ctx, *, prefixes=""):
#You'd obviously need to do some error checking here
#All I'm doing here is if prefixes is not passed then
#set it to default
custom_prefixes[ctx.guild.id] = prefixes.split() or default_prefixes
await ctx.send("Prefixes set!")
有关此应用的一个很好的示例,请参阅 Rapptz (the creator of discord.py)'s very own RoboDanny bot, he made it a public repo for educational purposes as an example. In specific, see prefix_callable
函数,它是我的 determine_prefix
示例的更强大的版本。
我想创建一个管理员可以更改命令前缀的命令(例如:不使用“.”,他们可以将其更改为“-”,只有“-”才有效)我' d 能够设置权限,使只有管理员能够使用命令
我四处查看,通过文档和互联网,但没有找到任何东西,我也不知道如何做到这一点
你应该使用 command_prefix
argument for discord.Bot
它接受一个字符串 (意思是一个机器人宽前缀) 或一个可调用的 (意思是一个函数 returns 基于条件的前缀)。
您的条件取决于调用 message
。因此,您可以允许公会定义自己的前缀。我将使用字典作为一个简单的例子:
...
custom_prefixes = {}
#You'd need to have some sort of persistance here,
#possibly using the json module to save and load
#or a database
default_prefixes = ['.']
async def determine_prefix(bot, message):
guild = message.guild
#Only allow custom prefixs in guild
if guild:
return custom_prefixes.get(guild.id, default_prefixes)
else:
return default_prefixes
bot = commands.Bot(command_prefix = determine_prefix, ...)
bot.run()
@commands.command()
@commands.guild_only()
async def setprefix(self, ctx, *, prefixes=""):
#You'd obviously need to do some error checking here
#All I'm doing here is if prefixes is not passed then
#set it to default
custom_prefixes[ctx.guild.id] = prefixes.split() or default_prefixes
await ctx.send("Prefixes set!")
有关此应用的一个很好的示例,请参阅 Rapptz (the creator of discord.py)'s very own RoboDanny bot, he made it a public repo for educational purposes as an example. In specific, see prefix_callable
函数,它是我的 determine_prefix
示例的更强大的版本。