黑名单Json discord.py

时间:2019-05-03 13:29:13

标签: asynchronous discord discord.py

我想异步地为我的discord.py bot创建一个黑名单系统...我想使用json而不是数据库,而我只是真的很困惑atm

@client.command()
async def blacklist(ctx, member: discord.Member = None):
    with open('blacklist.json', 'r')as f:
        users = json.load(f)
        if user.id in users:
            await client.say("already blacklisted")
        else:
            with open('blacklist.json', 'w')as f:
                json.dump(users, f)
                if not user.id in users:
                    users[user.id] = {}
                await client.say(f"done!! {member.name} has been blacklisted")```

2 个答案:

答案 0 :(得分:0)

如果blacklist.json是列表,则可以执行以下操作:

@client.command()
async def blacklist(ctx, member: discord.Member = None):
    if not member:
        return
    with open('blacklist.json', 'r+') as f:
        users = json.load(f)
        if member.id in users:
            await client.say("already blacklisted")
            return
        users.append(member.id)
        f.seek(0)
        json.dump(users, f)
        f.truncate()
        await client.say(f"done!! {member.mention} has been blacklisted")

答案 1 :(得分:0)

如果blacklist.json是列表,则可以执行以下操作:

@client.command()
async def blacklist(ctx, member: discord.Member = None):
    if not member:
        return
    with open('blacklist.json', 'r+') as f:
        users = json.load(f)
        if member.id in users:
            await client.say("already blacklisted")
            return
        users.append(member.id)
        f.seek(0)
        json.dump(users, f)
        f.truncate()
        await client.say(f"done!! {member.mention} has been blacklisted")