简体   繁体   English

我如何计算会员每次发送消息的次数? Discord.py

[英]How do I count every time a member sends a message? Discord.py

I want to store the amount of messages each user has sent in a json file.我想将每个用户发送的消息量存储在 json 文件中。 So everytime a user sends a message +1 to their amount in a json file.因此,每次用户在 json 文件中向他们的金额发送消息 +1。 Then they can use a command to see how many messages they have sent.然后他们可以使用命令查看他们发送了多少消息。 Im unsure how to do this as I am very unfamiliar with writing and reading to files.我不确定如何执行此操作,因为我对写入和读取文件非常不熟悉。

You can use the inbuilt json module -您可以使用内置的 json 模块 -

import json
Messages=json.load(open(‘messages.json’))

And when you want to edit -当你想编辑时 -

Messages[username]+=1
json.dump(Messages,open(‘messages.json’,’w’))

First, you would want to have an on message event.首先,您希望有一个on message 事件。 Then, just open your json file, and add 1 to it.然后,只需打开您的 json 文件,并在其中添加 1。 I would format it like so: {userid: messages}我会像这样格式化它: {userid: messages}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM