如何在discord.py中制作货币系统?
How to make a currency system in discord.py?
我最近在 python 中创建了一个 discord 机器人,并想为其添加一个货币系统。我希望能够存储和调用每个玩家的余额。
关于从哪里开始或视频链接的任何提示都将非常有帮助。
提前致谢!
我有一个像这样的系统,这是我的机器人,我通过将数据存储在 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 ctx.guild.members:
points.append({
"id": member.id,
"points": 0
})
with open("data.txt", "w") as outfile:
json.dump(points, outfile)
这将确保当前在不和谐公会中的每个人都有自己的存储空间。因此,您现在可以在 运行ning 一次并确保将其保存到 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)
你应该完成了!很抱歉这么长post,只是做这种事情需要时间。希望您理解这一点并能够成功建立您的经济体系。有什么问题就评论吧,别着急我会看到的!
我最近在 python 中创建了一个 discord 机器人,并想为其添加一个货币系统。我希望能够存储和调用每个玩家的余额。 关于从哪里开始或视频链接的任何提示都将非常有帮助。 提前致谢!
我有一个像这样的系统,这是我的机器人,我通过将数据存储在 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 ctx.guild.members:
points.append({
"id": member.id,
"points": 0
})
with open("data.txt", "w") as outfile:
json.dump(points, outfile)
这将确保当前在不和谐公会中的每个人都有自己的存储空间。因此,您现在可以在 运行ning 一次并确保将其保存到 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)
你应该完成了!很抱歉这么长post,只是做这种事情需要时间。希望您理解这一点并能够成功建立您的经济体系。有什么问题就评论吧,别着急我会看到的!