如何在discord.py中创建货币系统?

如何在discord.py中创建货币系统?,discord,discord.py,discord.py-rewrite,Discord,Discord.py,Discord.py Rewrite,我最近用python创建了一个discord bot,并希望向其添加一个货币系统。我希望能够存储和回忆每个玩家的平衡。 任何关于从哪里开始的提示或视频链接都会非常有用。 提前谢谢 我有一个这样的系统,这是我的机器人,我通过将数据存储在JSON文件中来实现。您只需创建一个名为data.TXT的TXT文件并在其中键入即可。另外,请确保导入JSON模块 {points: []} 然后在python代码中,可以执行类似的操作 with open("data.txt") as jso

我最近用python创建了一个discord bot,并希望向其添加一个货币系统。我希望能够存储和回忆每个玩家的平衡。 任何关于从哪里开始的提示或视频链接都会非常有用。
提前谢谢

我有一个这样的系统,这是我的机器人,我通过将数据存储在JSON文件中来实现。您只需创建一个名为data.TXT的TXT文件并在其中键入即可。另外,请确保导入JSON模块

{points: []}
然后在python代码中,可以执行类似的操作

with open("data.txt") as json_file:
    points = json.load(json_file)

for user in points["points"]:
    if user["id"] == ctx.author.id:
        point_num = user["points"]

await ctx.send(f"You have {point_num} points")

with open("data.txt", "w") as outfile:
    json.dump(points, outfile)
with open("data.txt") as json_file:
    points = json.load(json_file)

for user in points["points"]:
    if user["id"] == member.id:
        user["points"] += amount

with open("data.txt", "w") as outfile:
    json.dump(points, outfile)
如果你想增加分数,你可以这样做

with open("data.txt") as json_file:
    points = json.load(json_file)

for user in points["points"]:
    if user["id"] == ctx.author.id:
        point_num = user["points"]

await ctx.send(f"You have {point_num} points")

with open("data.txt", "w") as outfile:
    json.dump(points, outfile)
with open("data.txt") as json_file:
    points = json.load(json_file)

for user in points["points"]:
    if user["id"] == member.id:
        user["points"] += amount

with open("data.txt", "w") as outfile:
    json.dump(points, outfile)
您可能会遇到一些问题,例如每个用户都没有自己的存储空间,因此在安装开始时,您应该像这样为每个用户分配自己的存储空间

with open("data.txt") as json_file:
    points = json.load(json_file)

for user in ctx.guild.members:
    points.append({
"id": member.id,
"points": 0    
})

with open("data.txt", "w") as outfile:
    json.dump(points, outfile)
这将确保目前在discord帮会中的每个人都有自己的存储空间。因此,您现在可以在运行一次并确保将其保存到TXT文件后删除此代码。现在我们应该添加一些代码,确保每个新用户都能获得存储。因此,创建on_member_join事件的新实例并将其放入其中

with open("data.txt") as json_file:
    points = json.load(json_file)

points["points"].append({
    "id": member.id,
    "points": 0,
})

with open("data.txt", "w") as outfile:
    json.dump(points, outfile)
你应该完成!很抱歉这是一篇很长的帖子,只是这样做需要时间。希望你们能理解这一点,并成功地建立起你们的经济体系。如果你有任何问题,请继续评论,别担心,我会看到的