【发布时间】:2021-08-24 02:59:47
【问题描述】:
我正在制作一个自定义前缀命令,该命令使用 JSON 来获取所述服务器的前缀,但由于某种原因它无法正常工作。我认为正在发生的是我的异步功能正常工作,但我无法弄清楚发生了什么。
代码 这是没有不需要的不和谐机器人的代码
async def get_prefix(client, message):
with open('storage\pp.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
#Defining Bot:
Bot = commands.Bot(command_prefix= get_prefix)
@Bot.event
async def on_guild_join(guild):
with open('storage\pp.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '.'
with open('storage\pp.json', 'w') as f:
json.dump(prefixes, f, indent=4)
print(f'{guild.id} Joined')
@Bot.event
async def on_guild_remove(guild):
with open('storage\pp.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('storage\pp.json', 'w') as f:
json.dump(prefixes, f, indent=4)
print(f'{guild.id} Left')
#the command
@Bot.command(pass_context = True)
@commands.has_permissions(administrator=True)
async def prefix(ctx, prefix):
with open('storage\pp.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('storage\pp.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'***Prefix has been changed to*** ✅ ``{prefix}``')
这是我尝试运行时的错误。
return await value
File "c:\Users\beaty\Desktop\junk\code\python\pinky\bot.py", line 101, in get_prefix
return prefixes[str(message.guild.id)]
KeyError: '67685146592647##' #I took out two letters of that key because I don't know what it's referring to.
完全错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 979, in on_message
await self.process_commands(message)
File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 975, in process_commands
ctx = await self.get_context(message)
File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 886, in get_context
prefix = await self.get_prefix(message)
File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 831, in get_prefix
ret = await discord.utils.maybe_coroutine(prefix, self, message)
File "C:\Users\beaty\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\utils.py", line 343, in maybe_coroutine
return await value
File "c:\Users\beaty\Desktop\junk\code\python\pinky\bot.py", line 101, in get_prefix
return prefixes[str(message.guild.id)]
KeyError: '6768514659264758##'
【问题讨论】:
标签: python discord discord.py