how do i add prefixes from when the bot was offline. discord.py

523 views Asked by At

Every time the bot goes offline, and someone adds the bot, the bot doesn't automatically add the prefix from when the bot was offline. I don't know how to build something to automatically add that, any idea?

i get this error:

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)

Someone asked for my code, so this is my code, I still need help, so anything would help! Thank you in advanced!!!

1

There are 1 answers

0
Ethan M-H On BEST ANSWER

I would personally do it differently, rather then set a prefix for every new guild, only have custom prefixes for guilds that have it set to avoid this. Something like this would do what yours does now except it should handle all guilds as it is the fallback/default prefix

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 '-