Jak*_*boi 7 python python-3.x discord.py-rewrite
我想创建一个命令,管理员可以在其中更改命令的前缀(例如:而不是使用“。”他们可以将其更改为“-”并且只有“-”才能工作)我可以设置使只有管理员能够使用该命令的权限
我环顾四周,通过文档和互联网,但没有找到任何东西,我也不知道如何做到这一点
Jab*_*Jab 14
您应该使用此command_prefix参数discord.Bot接受字符串(意味着一个机器人宽前缀)或可调用(意味着基于条件返回前缀的函数)。
您的条件依赖于调用message. 因此,您可以允许公会定义自己的前缀。我将使用 dict 作为一个简单的例子:
...
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!")
Run Code Online (Sandbox Code Playgroud)
有关使用中的一个很好的示例,请参阅Rapptz (discord.py的创建者)自己的RoboDanny机器人,他将其作为用于教育目的的公共存储库作为示例。具体来说,请参阅prefix_callable函数,它是我determine_prefix示例的更强大版本。
| 归档时间: |
|
| 查看次数: |
17950 次 |
| 最近记录: |