【问题标题】:(Discord.py) Add delay to dm messages with on_guild_join event(Discord.py) 使用 on_guild_join 事件为 dm 消息添加延迟
【发布时间】:2021-07-04 00:10:34
【问题描述】:

我拥有一个大型 Discord 服务器,在我的机器人发布后,每个人都在几秒钟内添加了它。我的机器人的功能之一是对所有者说“感谢您邀请我,输入 -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!")

【问题讨论】:

    标签: python events discord discord.py


    【解决方案1】:

    简单。您可以使用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!")
    

    【讨论】:

    • 请修正缩进。
    • 好的,抱歉,新的这里
    • @OlauPlaRichart 此命令将在添加后等待 15 秒,然后将 dm 发送给所有者。但这不是我愿意做的。因为如果其他人同时添加机器人,则 2 位所有者将在 15 秒后同时收到消息。让我知道你是否理解
    • 我知道如何更好地解释它,我的英语也很糟糕。想象一下,一个所有者邀请该机器人并且他被 dmed,但 2 秒后另一个所有者邀请该机器人他无法被 dmed,因为在第一次和第二次邀请之间没有经过 15 秒。一旦过了 15 秒,他就会被 dmed,依此类推。清楚了吗?
    • 为了澄清@Cusihko,他的意思是如果邀请之间的差异是 2 秒,机器人将在 15 秒后发送第一条消息,然后在发送第一条消息后 2 秒发送第二条消息。所以这个答案只会给所有消息增加 15 秒的延迟,导致与原来的行为相同
    【解决方案2】:

    根据您的要求,您希望机器人等待 15 秒,直到向所有者发送消息。您可以通过使用名为last_dm_sent_time 的全局变量来存储上次发送 dm 的日期和时间。在 on_guild_join 事件中,您可以等到 15 秒过去

    在导入后将其添加到文件的开头

    import datetime
    import asyncio
    
    last_dm_sent_time = datetime.datetime.now()
    TIME_DIFFERENCE = 15  # Replace with how many seconds before sending new messages
    

    现在在你的函数中:

    @bot.event
    async def on_guild_join(guild):
        global last_dm_sent_time
        while datetime.datetime.now() <= abs(last_dm_sent_time+datetime.timedelta(seconds=TIME_DIFFERENCE)):  # Wait Until TIME_DIFFERENCE seconds have passed from last message sent time
            asyncio.sleep(1)  # Wait before rechecking while loop condition (Helps in reducing errors)
        last_dm_sent_time = datetime.datetime.now()  # Reset the timer for other instanced of the same function call.
        await guild.owner.send("Thanks for inviting the bot! Type -help to check the commands!")
    

    注意:该解决方案完全符合 OP 的要求,但如果例如 4 个用户同时邀请它,则需要 1 分钟才能发送给所有用户,因为它将等待 15 秒每一个。这不是bug,只是为了让别人理解。

    这是您可以使用的替代实现:

    import asyncio
    
    TIME_DIFFERENCE = 15
    
    global message_queue = []
    
    async def message_sender():
        while True:
            asyncio.sleep(TIME_DIFFERENCE)
            if len(message_queue) > 0:
                await message_queue.pop(0)
    

    对于以上内容,这里是on_guild_join 事件:

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

    【讨论】:

    • @ŁukaszKwieciński 怎么样?全局变量 last_dm_sent 应该让它工作
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2020-12-27
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多