[英]cooldown on an on_message event, discord.py
I'm trying to apply a cooldown on the on_message event.我正在尝试对 on_message 事件应用冷却时间。 how can I do that?
我怎样才能做到这一点?
here is the code这是代码
import discord
from discord.ext import commands
json
import os
client = commands.Bot(command_prefix = '!')
#more code
@client.event
async def on_message(message):
await open_account(message.author) #opens an account in a json file for this particular user
users = await get_users_data() #returns the users in the json file
user = message.author
#more code
this code is used for a leveling system and I would like it to only happen every 1 minute once so people won't spam, how can that be done?此代码用于调平系统,我希望它每 1 分钟只发生一次,这样人们就不会发送垃圾邮件,如何做到这一点?
This isn't possible in discord.py. A custom handler is necessary.这在 discord.py 中是不可能的。需要自定义处理程序。 For example:
例如:
COOLDOWN_AMOUNT_SECONDS = 60
last_executed = {}
async def on_message(message):
# ...
if last_executed.get(message.author.id, 1.0) + COOLDOWN_AMOUNT_SECONDS < time.time():
do_things()
last_executed[message.author.id] = time.time()
else:
do_things_if_the_cooldown_isnt_over_or_just_do_nothing()
This keeps a dict last_executed
with the timestamp that the event ran for each user.这会保留一个
last_executed
字典,其中包含事件为每个用户运行的时间戳。 Every time it is fired, it checks that enough time has passed, and if so, does things and sets the time to the current time.每次它被触发时,它都会检查是否已经过了足够的时间,如果是,则执行操作并将时间设置为当前时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.