我尝试了这些代码
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(float)
return prefixes[str(message.guild.id)]
client = commands.Bot(command_prefix = get_prefix)
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(float)
prefixes[str(guild.id)] = ','
with open('prefixes.json', 'w') as f:
json.dump(prefixes, float, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefixes.json', 'w') as float:
json.dump(prefixes, float, indent=4)
@client.command()
@commands.has_permissions(manage_channels=True)
async def 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, indent=4)
await ctx.send(f'Prefix changed succesfully, now my prefix for this server is: "{prefix}"')
我得到了这些错误
忽略on_message中的异常 追溯(最近一次通话): _run_event中的文件“ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ client.py”,第312行 等待科罗(* args,** kwargs) 文件“ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ ext \ commands \ bot.py”,行943,在on_message中 等待self.process_commands(消息) 文件“ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ ext \ commands \ bot.py”,行939,在process_commands中 ctx =等待self.get_context(消息) get_context中的第853行“ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ ext \ commands \ bot.py” 前缀=等待self.get_prefix(消息) get_prefix中的第798行的文件“ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ ext \ commands \ bot.py” ret =等待discord.utils.maybe_coroutine(前缀,自身,消息) 文件“ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ site-packages \ discord \ utils.py”,行> 331,在may_coroutine中 值= f(* args,** kwargs) get_prefix中的第20行的文件“ C:\ Users \ PC \ Desktop \ Code \ Project 01 \ index.py” 返回前缀[str(message.guild.id)] KeyError:'744103149471662152'
请帮助,它已经在几周前工作了。
答案 0 :(得分:0)
服务器ID不在JSON文件中
手动添加它,或者从该服务器中踢出机器人并再次重新邀请它
答案 1 :(得分:0)
好的。首先,您正在运行json.loads(float)
。浮动是一种功能,而不是文件。我编辑了您发布的代码,请尝试使用此代码:
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
client = commands.Bot(command_prefix = get_prefix)
@client.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, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.command()
@commands.has_permissions(manage_channels=True)
async def 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, indent=4)
await ctx.send(f'Prefix changed successfully, now my prefix for this server is: "{prefix}"')
(我还修复了您邮件中的一些拼写错误)
如果那仍然给您带来错误,请启动bot并重新邀请。那应该可以。
答案 2 :(得分:0)
def get_prefix(client, message):
with open('./database/prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
你正确加载了 float
而不是写 f
并检查json文件中的服务器ID
尝试手动编辑或踢机器人并再次邀请它