【发布时间】:2021-05-14 06:50:21
【问题描述】:
我目前正在处理我的状态,其中包含正在收听 .help。现在我想,当我使用命令 .setprefix 更改前缀动态时,前缀会更改,我的代码是:
@client.command()
async def setprefix(ctx, prefix):
with open("./cogs/prefixes.json", "r") as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open("./cogs/prefixes.json", "w") as f:
json.dump(prefixes, f, indent=4)
await ctx.send(embed=discord.Embed(color=discord.Color.green(), title="Changed Prefix", description=f"Changed the prefix to **`{prefix}`**"))
我现在如何设置前缀以重新调整到新状态? 我的状态循环包含:
while True:
await self.client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name="the developers"))
await asyncio.sleep(10)
await self.client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="to .help"))
await asyncio.sleep(10)
为了使前缀动态化,我使用以下代码:
def get_prefix(client, message):
with open("./cogs/prefixes.json", "r") as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
client = commands.Bot(command_prefix=get_prefix)
【问题讨论】:
-
您的代码目前没有更改机器人的前缀,仅在 JSON 文件中,这是您想要的吗?
-
请edit您的问题并在此处添加代码,而不是在 cmets 中
-
@ŁukaszKwieciński 我在帖子中添加了代码
标签: python json discord.py