Discord.py-使用命令更改前缀 [英] Discord.py - Changing prefix with command
问题描述
我想创建一个命令,管理员可以更改命令的前缀(例如:代替使用.",他们可以将其更改为-",并且只有-"才有效),我会能够设置权限,使只有管理员才能使用命令
I want to make a command where an admin can change the prefix for commands (eg: instead of using "." they can change it to "-" and only "-" will work if they do) I'd be able to setup the permissions to make only admins able to use the command
我通过文档&环顾了四周跨网,但没有发现任何东西,我对如何执行此操作一无所知
I've looked around, through the docs & interwebs but haven't found anything and I haven't had any idea on how to do this
推荐答案
您应使用 discord.Bot
接受字符串(表示一个bot宽前缀)或可调用的(表示根据条件返回前缀的函数).
You should use the command_prefix
argument for discord.Bot
this accepts either a string (meaning one bot wide prefix) or a callable (meaning a function that returns a prefix based on a condition).
您的情况取决于调用消息
的情况.因此,您可以允许公会定义自己的前缀.我将以dict作为简单示例:
Your condition relies on the invoking message
. Therefore you can allow guilds to define their own prefixes. I'll use a dict as a simple example:
...
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 (discord.py的创建者)自己的 RoboDanny 机器人,他将其作为教育用途的公共仓库例子.具体而言,请参见 prefix_callable
函数,它是我的 determine_prefix
示例的更强大的版本.
For a great example of this in use, refer to 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
function, it's a more robust version of my determine_prefix
example.
这篇关于Discord.py-使用命令更改前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!