Discord.py如何使用json来存储用户ID和仅存储用户ID [英] Discord.py how can I use json to store a users id and only a users id

查看:113
本文介绍了Discord.py如何使用json来存储用户ID和仅存储用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我将如何在json中存储用户不和谐ID,仅存储用户ID,别无其他.不确定如何执行此操作.

So, how would I store a users discord id in json, and only a users id, nothing else. Not sure how to do this.

感谢您的帮助!

推荐答案

您可以为此使用常规的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))

这篇关于Discord.py如何使用json来存储用户ID和仅存储用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆