且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Discord.py如何使用json来存储用户ID和仅存储用户ID

更新时间:2023-12-04 11:26:46

您可以为此使用常规的txt文件.看到新的ID后,请附加它们.启动机器人时,请将所有ID加载到set中以防止重复

You could just use a regular txt file for this. Append new ids when you see them. When you start your bot, load all the ids into a set to prevent duplicates

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))