【问题标题】:Cooldown on on_message event discord.pyon_message 事件 discord.py 的冷却时间
【发布时间】:2022-01-16 21:49:21
【问题描述】:

我正在尝试使用 discord.py 创建一个建议系统。用户向机器人发送消息,机器人在特定频道中发送该消息。该代码有效,我想了解是否可以在向机器人发送 DM 时添加冷却时间以及如何做到这一点。

@client.event        
async def on_message(message):    
if not message.author.bot:
    if isinstance(message.channel, DMChannel):
        if len(message.content) > 700:
            await message.channel.send("Suggerimento troppo lungo")

        else:
            embedMod= discord.Embed(title="Suggerimento:", description=message.content, colour=0xfc5a50, timestamp=datetime.utcnow())
            embedMod.set_footer(icon_url="https://s3.us-west-2.amazonaws.com/cdn.lnk.bio/profilepics/-1230732_20210524863.jpg")
            embedMod.set_author(name=f'{message.author}', icon_url=f'{message.author.avatar_url}')
            mod = client.get_channel(900085945414582312)
            await mod.send(embed=embedMod)
            await message.channel.send('Suggerimento registrato con successo, grazie mille.')

更新,对吗?

@client.event        # Suggerimenti
async def on_message(message):    
   bucket = cd_mapping.get_bucket(message)
   retry_after = bucket.update_rate_limit()
   if retry_after:
      await message.channel.send("Scimmia")


   else:
       if not message.author.bot:
               if isinstance(message.channel, DMChannel):
                   if len(message.content) > 700:
                    await message.channel.send("Suggerimento troppo lungo")
            

              else:
                    embedMod= discord.Embed(title="Suggerimento:", description=message.content, colour=0xfc5a50, timestamp=datetime.utcnow())
                   embedMod.set_footer(icon_url="https://s3.us-west-2.amazonaws.com/cdn.lnk.bio/profilepics/-1230732_20210524863.jpg")
                    embedMod.set_author(name=f'{message.author}', icon_url=f'{message.author.avatar_url}')
                    mod = client.get_channel(900085945414582312)
                    await mod.send(embed=embedMod)
                    await message.channel.send('Suggerimento registrato con successo, grazie mille.')
    else:    
                   await client.process_commands(message)

【问题讨论】:

标签: discord.py


【解决方案1】:

您可以使用 discord.py 冷却时间来做到这一点:

from discord.ext import commands

cd_mapping = commands.CooldownMapping.from_cooldown(1, 60, commands.BucketType.user)

@client.event
async def on_message(message):
    bucket = cd_mapping.get_bucket(message)
    retry_after = bucket.update_rate_limit()
    if retry_after:
        # rate limited
    else:
        # not rate limited

【讨论】:

    猜你喜欢
    • 2021-05-02
    • 2021-03-25
    • 1970-01-01
    • 2020-10-14
    • 2018-10-21
    • 2021-05-15
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多