json - Discord.py 如何使用 json 来存储用户 ID 并且仅存储用户 ID
问题描述
那么,我将如何在 json 中存储用户不和谐 id,只有一个用户 id,仅此而已。不知道该怎么做。
帮助表示赞赏!
解决方案
您可以为此使用常规的 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))
推荐阅读
- intellij-idea - 在 Intellij IDEA 中实现方法时 org.jetbrains.annotations.NotNull 而不是 javax.annotation.Nonnull
- javascript - 仅在具有多个地图的 web 中的最后一张地图上显示地图标记
- flutter - 使用 Flutter 的动画包进行导航时出现重复的全局键错误
- javascript - 如何在邮递员中测试和比较以前的值和新值?
- python - 使用 pybind11 和 pytorch 在 C++ 中运行 python 时出现无效指针错误
- json - 使用 powershell 部署 Azure Json 策略时出错
- c++ - Valgrind 忽略输出中的模板
- pdf - ng2-pdfjs-viewer:PDF全黑
- python - 当列可能包含 NaT 时,将 pandas 中两个时间列之间的差异计算为不包括周末的新列
- android - 为什么我的第一个颤振程序运行失败了?