'Command not returning expected output

I have made a changeprefix command. I noticed it wasn't returning the output i want it to and not changing the prefix with no errors in the console. I did some debugging with the print method and found out the code stops at this line.

prefixes[str(ctx.guild.id)] = prefix

The whole code is.

import discord
from discord.ext import commands
import logging
import json

logging.basicConfig(filename='log.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s', level=logging.INFO)

class changeprefix(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    @commands.has_guild_permissions(administrator = True)
    async def changeprefix(ctx, prefix):
        print("Yo!")
        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)
        
        await ctx.send(f"The prefix has been changed to {prefix}")


def setup(bot):
    bot.add_cog(changeprefix(bot))

If anyone can help that would be great.



Solution 1:[1]

Thanks for all of the help with this. It was a very stupid idiot mistake that i made. Because i am using a cog my command must start with self, ctx but it started with ctx.

The new updated code is

    @commands.command()
    @commands.has_guild_permissions(administrator = True)
    async def changeprefix(self, ctx, prefix : str):
        print("Yo!")
        with open("prefixes.json", "r") as f:
            prefixes = json.load(f)
        
        string = str(ctx.guild.id)
        prefixes[string] = prefix

        with open("prefixes.json","w") as f:
            json.dump(prefixes,f)
        
        await ctx.send(f"The prefix has been changed to {prefix}")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Knife worm