将冷却时间/计时器添加到on_message [Discord.py] [英] Add cooldown / timer to on_message [Discord.py]
问题描述
最近,我开始用Python制作Discord机器人(以此来测试Python的基础),然后自己创建了一个具有多个命令的功能机器人。为了扩大其用途,我添加了一个Level / XP系统,该系统到目前为止已经可以使用。
I got into making a Discord bot in Python very recently (testing the grounds of Python with it) and created a functioning one with several commands myself. To widen its uses, I have added a level/XP system, which is working so far.
[...]
@bot.event
async def on_message(message):
user_add_xp(message.author.id, 2)
await bot.process_commands(message)
# commands go here
def user_add_xp(user_id, xp):
if os.path.isfile('users.json'):
try:
with open('users.json', 'r') as fp:
users = json.load(fp)
users[user_id]['xp'] += xp
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
except KeyError:
with open('users.json', 'r') as fp:
users = json.load(fp)
users[user_id] = {}
users[user_id]['xp'] = xp
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
else:
users = {user_id: {}}
users[user_id]['xp'] = xp
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
[...]
但是为了防止用户充斥某些频道/向其发送垃圾邮件并迅速攀升至顶部,我想在授予XP时增加冷却时间/计时器。我试图将 @ commands.cooldown(1,120,命令.BucketType.server)
添加到两个 @ bot.event
和 user_add_xp
,但两者均无法达到预期的效果。
我不知道如何添加此冷却时间/计时器。
But to prevent users from just flooding/spamming some channels and rocketing to the top, I want to add a cooldown/timer on the awarding of XP. I have tried to add @commands.cooldown(1, 120, commands.BucketType.server)
to both @bot.event
and user_add_xp
, but both do not get me the desired result.
I have no other idea how to add this cooldown/timer.
最后,我希望机器人仅每2分钟授予一次XP。 / p>
In the end, I want the bot to only grant XP once every two minutes.
推荐答案
不确定仅 discord.py
是否可行,但是您可以在您的字典中存储上一次向用户授予消息XP的时间。
Not sure if it's possible with just discord.py
, but you can store the last time a message was awarded XP to a user in your dictionary.
下面的代码存储自静态开始日期以来的秒数( epoch
),则消息奖励XP。然后,它会在这个时间检查是否发生新的邮件事件。
The below code stores the number of seconds since a static start date (epoch
) when a message awards XP. It then checks against this time when a new message event happens.
[...]
import datetime
epoch = datetime.datetime.utcfromtimestamp(0)
@bot.event
async def on_message(message):
user_add_xp(message.author.id, 2)
await bot.process_commands(message)
# commands go here
def user_add_xp(user_id, xp):
if os.path.isfile('users.json'):
try:
with open('users.json', 'r') as fp:
users = json.load(fp)
time_diff = (datetime.datetime.utcnow() - epoch).total_seconds() - users[user_id]['xp_time']
if time_diff >= 120:
users[user_id]['xp'] += xp
users[user_id]['xp_time'] = (datetime.datetime.utcnow() - epoch).total_seconds()
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
except KeyError:
with open('users.json', 'r') as fp:
users = json.load(fp)
users[user_id] = {}
users[user_id]['xp'] = xp
users[user_id]['xp_time'] = (datetime.datetime.utcnow() - epoch).total_seconds()
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
else:
users = {user_id: {}}
users[user_id]['xp'] = xp
users[user_id]['xp_time'] = (datetime.datetime.utcnow() - epoch).total_seconds()
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
[...]
这篇关于将冷却时间/计时器添加到on_message [Discord.py]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!