問題描述
我想創(chuàng)建一個(gè)命令,管理員可以在其中更改命令的前綴(例如:而不是使用."他們可以將其更改為-",并且只有-"才會(huì)起作用)我會(huì)能夠設(shè)置權(quán)限以使只有管理員能夠使用該命令
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
我環(huán)顧四周,通過 docs &互聯(lián)網(wǎng),但沒有找到任何東西,我不知道如何做到這一點(diǎn)
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
推薦答案
您應(yīng)該使用 command_prefix
參數(shù)/api.html#bot" rel="noreferrer">discord.Bot
這接受字符串 (表示一個(gè)機(jī)器人范圍的前綴) 或可調(diào)用的 (表示根據(jù)條件返回前綴的函數(shù)).
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).
您的條件依賴于調(diào)用 message
.因此,您可以允許公會(huì)定義自己的前綴.我將使用一個(gè) dict 作為一個(gè)簡單的例子:
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!")
關(guān)于這個(gè)使用的一個(gè)很好的例子,請(qǐng)參考 Rapptz (discord.py 的創(chuàng)建者) 自己的 RoboDanny 機(jī)器人,他將其作為教育目的的公共回購例子.具體見prefix_callable
函數(shù),這是我的 determine_prefix
示例的更強(qiáng)大的版本.
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.
這篇關(guān)于Discord.py - 使用命令更改前綴的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!