簡體   English   中英

在 discord.py 中使用 on_message 進行冷卻

[英]Cooldowns in discord.py with on_message

所以,我基本上搞砸了。 這是我第一次嘗試使用 discord bot,並且我已經在 on_message 中完成了我的所有代碼,它檢查消息的內容以查看它是否與命令名稱匹配(下面的示例)。 我已經添加了一些命令,它們很長,我真的不想重寫它。 有什么辦法可以解決這個問題還是我必須重寫它?

  if message.content.lower().startswith("!test"):  
    await message.channel.send(db[str(message.author.id)])

我正在做的簡單示例,只是一個測試命令。

我曾嘗試查看其他問題,但我要么:不想這樣做,要么不明白人們在說什么。

任何幫助將不勝感激,因為我是 discord.py 的新手; 我可能需要用更簡單的術語解釋一下。

你可以這樣做:

users_on_cooldown = [] # Consider renaming this if you are going to have multiple commands with cooldowns.
def on_message(msg):
    if msg.content.lower().startswith("!test") and not msg.author.id in users_on_cooldown:  
        await msg.channel.send(db[str(msg.author.id)])
        users_on_cooldown.append(msg.author.id)
        asyncio.sleep(20) # time in seconds
        users_on_cooldown.remove(msg.author.id)

由於您說您是初學者,請注意,如果您使用單獨的冷卻時間創建另一個命令,請使用另一個變量users_on_cooldown ,可能類似於ban_cmd_cooldowntest_cmd_cooldown

工作原理使用該命令時,用戶被添加到列表中,並在一定秒數后被刪除。 運行該命令時,會檢查用戶是否在列表中。

注意:當機器人被重置時,冷卻時間也將被重置。

如果您對此有任何疑問,請隨時在下面的評論中提問。

這里如何使用

@client.command()
@commands.cooldown(1, 60, commands.BucketType.user)
async def test(ctx):
    await ctx.send(db[str(message.author.id)])

(1, 60, commands.BucketType.user)表示每(1, 60, commands.BucketType.user)秒 1 msg 或(1, 60, commands.BucketType.user)秒冷卻。

我建議你重寫你的機器人。 這可能需要一些時間,但這是值得的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM