0
votes

I tried these code

def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
    prefixes = json.load(float)

    return prefixes[str(message.guild.id)]

client = commands.Bot(command_prefix = get_prefix)

@client.event
async def on_guild_join(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(float)

    prefixes[str(guild.id)] = ','

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, float, indent=4)

@client.event
async def on_guild_remove(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    prefixes.pop(str(guild.id))

    with open('prefixes.json', 'w') as float:
        json.dump(prefixes, float, indent=4)

@client.command()
@commands.has_permissions(manage_channels=True)
async def prefix(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)

    await ctx.send(f'Prefix changed succesfully, now my prefix for this server is: "{prefix}"')

and i got these erors

Ignoring exception in on_message Traceback (most recent call last): File "C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", >line 312, in _run_event await coro(*args, **kwargs) File "C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 943, in on_message await self.process_commands(message) File "C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 939, in process_commands ctx = await self.get_context(message) File "C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 853, in get_context prefix = await self.get_prefix(message) File "C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 798, in get_prefix ret = await discord.utils.maybe_coroutine(prefix, self, message) File "C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\utils.py", line >331, in maybe_coroutine value = f(*args, **kwargs) File "C:\Users\PC\Desktop\Code\Project 01\index.py", line 20, in get_prefix return prefixes[str(message.guild.id)] KeyError: '744103149471662152'

Please help, it was working a few weeks ago.

3
Can you explain what json.load(float) and json.dump(.., float,..) do? Did you mean f?L D
I tried changing it to F to Float and it's the same errorRaphiel

3 Answers

0
votes

The server ID is not in the JSON File

Add it manually OR Kick the bot from that server and re-invite it again

0
votes

Ok. First, you are running json.loads(float). Float is a function, not the file. I edited the code you posted, try using this:

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)

@client.event
async def on_guild_join(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)

@client.event
async def on_guild_remove(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)

@client.command()
@commands.has_permissions(manage_channels=True)
async def prefix(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)

    await ctx.send(f'Prefix changed successfully, now my prefix for this server is: "{prefix}"')

(I also fixed some spelling errors in your messages)

If that still gives you the error, kick the bot and re-invite. Should work then.

0
votes
def get_prefix(client, message):
    with open('./database/prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes[str(message.guild.id)]

you did right load float instead of writing f and check if the server id in the json file try edit it manual or kick the bot and invite it again