GuildPrefix Command Confused

Question:

        @bot.command()
        async def guildprefix(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 to: {prefix}')
            name=f'{prefix}BotBot'

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

It logs the prefix but won’t send when I do the prefix that I set it to

Asked By: notmeas

||

Answers:

Your get_prefix method is setting the prefix to ".".

def get_prefix(bot, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    if str(message.guild.id) not in prefixes:
        # only set the default if it's not already in the file
        prefixes[str(message.guild.id)] = '.'
    return prefixes[str(message.guild.id)]

Now get_prefix will return the value in the file or "." if the guild ID isn’t already in the file.

Answered By: ESloman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.