Search code examples
pythonjsonpython-3.xdiscord.pypycord

Will not load json file with json.load


Working with my discord bot, and I'm trying to store a bunch of responses for a bunch of different commands in a json file. This command is just a set of dares, with the key "dare" and the value is a list full of different strings. When I use the with open() function, it registers it as a directory, and you can print the raw file but when you try to use var = json.load it just doesn't do anything. Any code after that line will not run.

I've used shell commands to go straight into that file and open it. That works completely fine. The file prints out normally.

    @commands.command()
    async def dare(self, ctx):
        """Gives the user a dare to do"""
    
        with open("cogs/docs/json/responses.json") as f:
            print(-1)
            print(f)
            data = json.load(f)
            print(0)
            dares = data["dare"]
            print(1)
            selectedDare = random.choice(dares)

        await ctx.reply(selectedDare)

The code above only print to -1 and f. Nothing past that. f simply prints the object. I've also tried json.loads and such, nothing works. JSON is properly installed. I'm completely lost.

All of the responses are stored in a list, and here's the one that is just for this command.

// this is all the responses for the dares
    {
        "dare":[
            "Dare #1",
            "Dare #2",
            "Dare #3"
        ]
    }

My bot handles the error has a CommandError, but nothing further.


# if a command flags an error it handles it
@client.event
async def on_command_error(ctx, error):
    """Handles errors"""

    if isinstance(error, commands.CommandError):
        print("CommandError found")
        return

Solution

  • As stated in comments, the issue comes from the author's defining a on_command_error or on_error function ( https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=on_command_error#discord.ext.commands.Bot.on_command_error )

    Which will be called with the exception/error when it is raised anywhere in the bot/cog.

    -> There should always be a default case for that defined function, for example if you write a few if isinstance(e, SomeException): ..., you will need a else: raise e or else: super().on_command_error(e) to make sure the default case is handled further, in case of an exception you didn't plan or that you don't need to worry about in your bot/cog

    You could change your function to this

    # if a command flags an error it handles it
    @client.event
    async def on_command_error(ctx, error):
        """Handles errors"""
    
        if isinstance(error, commands.CommandError):
            print("CommandError found")
            return
        super().on_command_error(ctx, error)
    

    or this

    # if a command flags an error it handles it
    @client.event
    async def on_command_error(ctx, error):
        """Handles errors"""
    
        if isinstance(error, commands.CommandError):
            print("CommandError found")
            return
        raise error
    

    But I think the best approach would be

    # if a command flags an error it handles it
    @client.event
    async def on_command_error(ctx, error):
        """Handles errors"""
    
        if isinstance(error, commands.CommandError):
            print("CommandError found")
        else:
            super().on_command_error(ctx, error)
    

    and when adding other condition, for example other errors, you will have to use elif