首页 > 解决方案 > Discord.py 如何使用 json 来存储用户 ID 并且仅存储用户 ID

问题描述

那么,我将如何在 json 中存储用户不和谐 id,只有一个用户 id,仅此而已。不知道该怎么做。

帮助表示赞赏!

标签: jsonpython-3.xdiscord.py

解决方案


您可以为此使用常规的 txt 文件。当你看到它们时附加新的 id。启动机器人时,将所有 id 加载到 aset中以防止重复

ids = set()

@bot.event
async def on_ready():
    with open("ids.txt") as f:
        for id in f:
            ids.add(id.strip())

@bot.command(pass_context=True)
async def register(ctx):
    id = ctx.message.author.id
    if id not in ids:
        ids.add(id)
        with open("ids.txt", "a") as f:
            print(id, file=f)
        await bot.say("ID {} added".format(id))

推荐阅读