每次僵尸程序离线时,如果有人添加了该僵尸程序,则该僵尸程序不会自动添加该僵尸程序离线时的前缀。我不知道该如何构建可以自动添加的内容,知道吗?
我收到此错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 930, in on_message
await self.process_commands(message)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 926, in process_commands
ctx = await self.get_context(message)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 842, in get_context
prefix = await self.get_prefix(message)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 787, in get_prefix
ret = await discord.utils.maybe_coroutine(prefix, self, message)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\utils.py", line 317, in maybe_coroutine
value = f(*args, **kwargs)
File "C:\Users\Administrator\Desktop\doob\bot.py", line 16, in get_prefix
return prefixes[str(message.guild.id)]
KeyError: '{insert server id here}
(prefix.py):
class prefix(commands.Cog):
def __init__(self, client):
self.client = client
# Opens json file then dumps '-'
@commands.Cog.listener()
async def on_guild_join(self, 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)
# Removes guild from json file when Doob leaves.
@commands.Cog.listener()
async def on_guild_remove(self, 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)
# Changes the prefix (that the user provides.) for the specific server.
@commands.command(aliases=['prefix'])
@commands.has_permissions(administrator=True)
async def changeprefix(self, 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)
embed = discord.Embed(title="An administrator has changed the prefix.", description=f"An administrator has changed the prefix to {prefix}.", colour=discord.Color.blue())
embed.add_field(name="The prefix has been changed to:", value=prefix)
embed.set_thumbnail(url=doob_logo)
await ctx.send(embed=embed)
(bot.py)
# Creates and loads the json file.
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)
有人问我的代码,所以这是我的代码,我仍然需要帮助,所以任何事情都会有所帮助!谢谢高级!!!
答案 0 :(得分:0)
我个人会做不同的事情,而不是为每个新的公会设置一个前缀,只有为公会设置了自定义前缀才能避免这种情况。像这样的事情将做您现在的工作,除了它应该处理所有公会,因为它是后备/默认前缀
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
try:
return prefixes[str(message.guild.id)]
except KeyError:
return '-