【发布时间】:2022-01-21 11:57:51
【问题描述】:
问题是我想用一个命令动态地改变我的前缀。前缀在 json 文件中更新,但唯一的问题是机器人需要重新加载才能接受新的前缀。所以我需要一种方法让机器人在 config.json 中更新后立即读取新前缀。
JSON 文件:
"command_prefix": "."
更新前缀 CMD:(该命令在我的核心 cog 中)
# PREFIX CHANGE COMMAND
@commands.group(name="prefix", invoke_without_command=True)
@commands.has_permissions(administrator=True)
async def prefix(self, ctx):
current_prefix = get_config_file("command_prefix")
await ctx.channel.send(f'Current prefix is: **{current_prefix}** \n If you want to change the prefix use subcommand: _set_')
@prefix.command(name="set")
@commands.has_permissions(administrator=True)
async def set(self, ctx, mode):
mode != None
with open('config.json', 'r') as f:
config = json.load(f)
config["command_prefix"] = mode
with open('config.json', 'w') as f:
json.dump(config, f, indent=4)
f.close()
await ctx.channel.send(f'Prefix has been changed to `{mode}`')
【问题讨论】:
标签: python discord discord.py prefix