Unban命令在discord.py中不起作用。

huangapple go评论79阅读模式
英文:

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 caseid, infraction in infractions.items():
        if infraction["user"] == str(user.id) and infraction["type"] == "Ban" and infraction["status"] == "Active":
            await user.unban()
            await interaction.response.send_message(content=f"{user} has been unbanned!", ephemeral=True)
            infractions[caseid]["status"] = "Revoked"
            with open("infractions.json", "w") as f:
                json.dump(infractions, f)
            return
英文:
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

答案1

得分: 1

从外观上看,违规是一个字典,所以for i in infractions会迭代它的键(显然是字符串)。访问字典的值有不同的方法,但如果你想迭代键,你需要将它们用作字典的索引,就像这样:

例如:

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

另一种选项是迭代键/值对,如下所示:

for case_id, i in infractions.items():
    if i["user"] == ...
英文:

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"] == ...

huangapple
  • 本文由 发表于 2023年7月13日 23:08:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定