记录一个命令在用户id上被使用了多少次[不和.py]

2024-10-02 16:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图记录一个命令在一个特定用户上被使用了多少次,然后将它写入一个文件,以便在每次命令被触发时添加到其中。在

这是我要做的命令

@client.command(pass_context=True)
async def boop(ctx):
    mentions = ctx.message.mentions
    for user in mentions:
        await client.say("{} has been Boop'ed!".format(user))

我试图将一个数字连同userid一起写入一个json文件。在


Tags: 文件用户命令clienttrueasyncdef记录
1条回答
网友
1楼 · 发布于 2024-10-02 16:30:30

最简单的方法可能是维护一个字典,将用户映射到他们被提到的次数。然后可以使用^{}将其发送到一个文件。你应该注意到这个比例不是很好。如果您看到自己不断更新这个文件,您应该考虑切换到异步数据库(特别是因为文件写入是一个阻塞操作)。在

import json
from collections import Counter

boop_dict = Counter()

@client.command(pass_context=True)
async def boop(ctx, *users: discord.User):
    for user in users:
        await client.say("{} has been Boop'ed!".format(user))
        boop_dict[user.id] += 1
    with open('boop_file.json', 'w+') as f:
        json.dump(boop_dict, f)

相关问题 更多 >