首页 > 解决方案 > (Discord.py) 使用 on_guild_join 事件为 dm 消息添加延迟

问题描述

我拥有一个大型不和谐服务器,在我的机器人发布后,每个人都在几秒钟内添加了它。我的机器人的功能之一是对所有者说“感谢您邀请我,输入 -help 以检查命令”。这背后有一个问题:该机器人在 5 秒内向不同的所有者发送了大约 30 条消息,而我的机器人最终因垃圾邮件的不和谐原因而被禁止。经过几次尝试后,我了解到可以每 15 秒发送一次 dm,以免被禁止。

我如何为每个dm添加冷却时间?例如,如果有人添加它,机器人会向所有者发送一个 dm,然后另一个所有者在 15 秒之前邀请该机器人,但他会在冷却后收到 dm。我使用的命令:

@bot.event
async def on_guild_join(guild):
    await guild.owner.send("Thanks for inviting the bot! Type -help to check the commands!")

标签: pythoneventsdiscorddiscord.py

解决方案


简单的。您可以asyncio.sleep用于等待

import asyncio 

@bot.event
async def on_guild_join(guild):
    await asyncio.sleep(time_seconds)
    await guild.owner.send("Thanks for inviting the bot! Type -help to check the commands!")

推荐阅读