如何计算命令执行的频率?

时间:2021-02-22 14:31:14

标签: python discord discord.py counter

我想通过机器人显示一个命令被执行了多少次。 为此,我已经在平台上进行了查询,并尝试了不同的方法。关于我的代码:

e = discord.Embed(color=discord.Colour.green())
user = ctx.author
e.title = f "New suggestion!"
e.set_footer(text=f"{user} | {user.id}")
e.description = f "**__Submitter:__**\n {ctx.author}"
e.add_field(name="__Suggestion:__", value=f"{text}")
e.set_thumbnail(url=ctx.message.author.avatar_url)
e.timestamp = datetime.utcnow()
await channel.send(embed=e)
await ctx.message.add_reaction("✅")

bot 应该在标题中添加 Suggestion no. #number,它会在命令执行时一次上升一个。我已经尝试了以下方法:

    def __init__(self, bot):
        self.bot = bot
        self.counter = 0

#Shortened
e.title = f "Suggestion no. {self.counter}"

或者也一样:

e.title = f "Suggestion no. {self.counter + 1}

但这并没有帮助,所以我如何确保显示正确的数字,并且在机器人重启的情况下,这个数字继续上升并且不会从头开始。 请注意,以某种方式,全球活动对我不起作用!

1 个答案:

答案 0 :(得分:0)

您实际上需要更新 counter 变量

self.counter += 1
e.title = f"Suggestion no. {self.counter}"

如果您希望在机器人重新启动后变量继续存在,您可以将其保存在 JSON 或文本文件中