更新时间:2023-02-23 21:54:05
我不确定您要命令执行什么操作,但这是如何将json实现到discord.py中的示例.
I'm not sure what you want your command to do, but here's an example of how you would implement json into discord.py.
在这里,无论何时执行命令,机器人都会打开一个json文件,读取数据,并查看消息作者是否在数据中.如果作者在数据中,则将键/值对删除,并将数据重写到json文件中:
Here, whenever the command is executed, the bot opens a json file, reads the data, and sees if the message author is in the data. If the author is in the data, the key/value pair is deleted, and the data is rewritten into the json file:
import json
@bot.command()
async def afkremoveme(ctx):
f = "yourFile.json"
author = str(ctx.author.id)
with open(f, "r") as read_file:
data = json.load(read_file)
if author in data: # if any key in the dictionary is an integer, it is converted to a string when a json file is written
del data[author]
newData = json.dumps(data, indent=4)
with open(f, "w") as write_file:
write_file.write(newData)
await ctx.send(f"{ctx.author.display_name} is no longer afk...")
这正在读取一个看起来像这样的json文件(用您的ID替换000000):
This is reading a json file that looks like this (replace 000000 with your id):
{
"000000" : "afk",
"someOtherGuy" : "afk"
}
所有这些都使用字典和json模块.如果您不熟悉这两个概念,这里有一些链接可以帮助您:-)
All of this uses dictionaries and the json module. If you're unfamiliar with either of the concepts, here are a few links to help you out :-)