【发布时间】:2021-11-19 00:27:45
【问题描述】:
我试图让机器人有一个前缀,你可以为你想要的每台服务器使用,但是当你使用命令时,它会在控制台中弹出
return prefixes[str(message.guild.id)] #recieve the prefix for the guild id given
KeyError: '882434407212925000'
这是我使用的代码和它说它失败的那一行
def get_prefix(client, message): ##first we define get_prefix
with open("prefixes.js", "r") as f: ##we open and read the prefixes.json, assuming it's in the same file
prefixes = json.load(f) #load the json as prefixes
return prefixes[str(message.guild.id)] #recieve the prefix for the guild id given
【问题讨论】:
-
公会ID不存在于前缀var中
-
@FloweyTF 所以你能告诉我我需要添加什么,那就太好了
-
尝试调试?在返回前缀之前打印出前缀的值?
-
为什么JSON文件命名为
prefixes.js?此外,在加载时打印prefixes的键。如果它不存在,您可以创建一个函数将公会 ID 和默认前缀作为键值对添加到字典prefixes中,然后再添加到文件prefixes.json
标签: discord.py command