【发布时间】:2021-09-17 19:44:31
【问题描述】:
所以,我按照教程制作了更改前缀命令,但是当我尝试更改前缀时,我什么也没得到。也没有错误。我尝试使用默认前缀运行其他命令,但它们也不起作用。我使用的代码是:
import json
def get_prefix(bot,message):
with open ("prefixes.json", "r") as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
bot = commands.AutoShardedBot(command_prefix = get_prefix, help_command=None, intents=discord.Intents.all())
@bot.event
async def on_guild_join(guild):
with open ("prefixes.json", "r") as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = ","
with open ("prefixes.json", "w") as f:
json.dump(prefixes,f)
@bot.command()
@commands.has_permissions(administrator = True)
async def change_prefix(ctx, prefix):
with open ("prefixes.json", "r") as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open ("prefixes.json", "w") as f:
json.dump(prefixes,f)
@bot.event
async def on_message(msg):
if msg.mentions == bot.user:
with open ("prefixes.json", "r") as f:
prefixes = json.load(f)
pre = prefixes[str(msg.guild.id)]
await msg.channel.send(f"My Prefix is {pre}")
```
【问题讨论】:
-
你能显示你的 JSON 文件吗?
-
@Abdulaziz 我的 JSON 文件是空白的
标签: json discord discord.py bots prefix