Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unban command in discord.py is not working

async def openinf():
    with open("infractions.json", "r") as f:
        users = json.load(f)
    return users

async def createinfraction(user, type, mod, date, caseid, reason, expireson = None):
        infractions = await openinf()
        
        infractions[str(caseid)] = {}
        infractions[str(caseid)]["type"] = str(type)
        infractions[str(caseid)]["mod"] = str(mod.id)
        infractions[str(caseid)]["date"] = str(date)
        infractions[str(caseid)]["user"] = str(user.id)
        infractions[str(caseid)]["reason"] = str(reason)
        infractions[str(caseid)]["avatar"] = str(user.avatar)
        infractions[str(caseid)]["expireson"] = str(expireson)
        infractions[str(caseid)]["status"] = "Active"


        with open("infractions.json", "w") as f:
            json.dump(infractions, f)
        return True

@client.tree.command()
@commands.has_role(modrole)
@app_commands.describe(user = "Provide an ID of the user to unban.")
async def unban(interaction : discord.Interaction, user : str):
        user = await client.fetch_user(int(user))

    
        infractions = await openinf()

        for i in infractions:
            if i["user"] == user.id:
                if i["type"] == "Ban":
                    if i["status"] == "Active":
                        await user.unban()
                        await interaction.response.send_message(content=f"{user} has been unbanned!", ephemeral=True)
                        i["status"] = "Revoked"
                        with open("infractions.json", "w") as f:
                            json.dump(infractions, f)

                        return

This code returns the following error

       ~^^^^^^^^
TypeError: string indices must be integers, not 'str'

Basically what I want it to do is, when the /unban command is ran, look into the json file for every case the user has, check every ban, and check every active ban, once the bot found the active ban, change that ban status into "Revoked". (You can see how I make the json file in the createinfractions function).

Now I realize that I is a list, however, I’m struggling to find a way to reach to what described above. I thank you in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the looks of it, infractions is a dict, so that for i in infractions would iterate over its keys (which are strings apparently). There are different ways to access the dictionary’s values, but if you want to iterate over the keys, you’ll have to use them as indices on the dict, like this:

For instance:

for i in infractions:
    if infractions[i]["user"] == user.id:

Another option is to iterate over the key/value pairs like:

for case_id, i in infractions.items():
    if i["user"] == ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading