問題描述
我最近開始用 Python 制作一個 Discord 機器人(用它測試 Python 的基礎),并自己創建了一個帶有多個命令的功能性機器人.為了擴大它的用途,我添加了一個級別/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, commands.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.
最后,我希望機器人每兩分鐘只授予一次 XP.
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]的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!