我如何从机器人离线时添加前缀。 discord.py
how do i add prefixes from when the bot was offline. discord.py
每次机器人下线,并且有人添加机器人时,机器人不会自动添加机器人离线时的前缀。我不知道如何构建自动添加的东西,知道吗?
我收到这个错误:
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)
有人要我的代码,所以这是我的代码,我仍然需要帮助,所以任何帮助!先谢谢你了!!!
我个人会采取不同的做法,而不是为每个新公会设置一个前缀,只为设置了前缀的公会设置自定义前缀以避免这种情况。像这样的事情会做你现在所做的事情,除了它应该处理所有行会,因为它是 fallback/default 前缀
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 '-
每次机器人下线,并且有人添加机器人时,机器人不会自动添加机器人离线时的前缀。我不知道如何构建自动添加的东西,知道吗?
我收到这个错误:
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)
有人要我的代码,所以这是我的代码,我仍然需要帮助,所以任何帮助!先谢谢你了!!!
我个人会采取不同的做法,而不是为每个新公会设置一个前缀,只为设置了前缀的公会设置自定义前缀以避免这种情况。像这样的事情会做你现在所做的事情,除了它应该处理所有行会,因为它是 fallback/default 前缀
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 '-