【问题标题】:BlackList Json discord.py黑名单 Json discord.py
【发布时间】:2019-05-03 13:29:12
【问题描述】:

我想在异步中为我的 discord.py 机器人创建一个黑名单系统...我想使用 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")```

【问题讨论】:

  • 这段代码没有按您预期的方式工作怎么办?您是否看到错误消息?

标签: python python-3.x asynchronous discord discord.py


【解决方案1】:

如果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")

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 2021-02-25
    • 2021-02-21
    • 2020-10-26
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多