如何在discord.py中建立货币系统? [英] How to make a currency system in discord.py?

查看:74
本文介绍了如何在discord.py中建立货币系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近用python创建了discord机器人,并希望为其添加货币系统。我希望能够存储和回忆每个玩家的余额。
关于从何处开始或链接到视频的任何提示将非常有帮助。
预先感谢!

I've recently created a discord bot in python and wanted to add a currency system to it. I want to be able to store and recall the balance of each player. Any tips on where to start or links to videos will be really helpful. Thanks in advance!

推荐答案

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

I have a system like this is my bot, I did it by storing my data in a JSON file. You can do this by simply creating a TXT file named data.txt and typing this inside. Also, be sure to import the JSON module.

{points: []}

然后在您的python代码中,您可以执行以下操作。

Then in your python code, you can do something like this.

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)

如果您要添加点,则可以执行以下操作。

And if you ever wanted to add points you could do something like this.

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)

您可能会遇到类似每个用户都没有的问题具有自己的存储空间,因此在设置之初,您应该像这样分配每个人自己的存储空间。

You may run into problems like every user not having their own storage, so at the beginning of your setup, you should assign everyone their own storage like so.

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)

这将确保当前不和谐公会中的每个人都有自己的存储空间。因此,您现在可以在运行一次并确保将其保存到TXT文件后将其删除。现在,我们应该添加一些代码,以确保每个新手都能获得存储。因此,创建on_member_join事件的新实例并将其放入内部。

This would ensure that everyone currently in the discord guild has their own storage. So you can now delete this code after running it once and making sure it saved to the TXT file. Now we should add some code that will make sure everyone new gets storage. So, create a new instance of the on_member_join event and put this inside.

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)

您应该做得到!很抱歉,发布了这么长时间,只是这样做需要时间。希望您理解了这一点,并能够成功地建立自己的经济体系。如有任何疑问,请继续对此发表评论,不要担心,我会看到它的。

And you should be done! Sorry for this being such a long post, it's just that doing this sort of this takes time. Hopefully, you understood this and were able to successfully make your economic system. If you have any questions, go ahead and comment on this, don't worry I'll see it!

这篇关于如何在discord.py中建立货币系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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