【问题标题】:Make Python Discord Bot send a message if on cooldown让 Python Discord Bot 在冷却时发送消息
【发布时间】:2018-05-03 03:02:25
【问题描述】:

我正在尝试将命令设置为具有 3 秒冷却时间的 python discord 机器人,如果在冷却时间之前使用该命令,则会发送一条消息。我该怎么做?这是命令的代码:

@client.command(pass_context=True)
@commands.cooldown(1, 3, commands.BucketType.channel)
async def message (ctx):

谢谢!

【问题讨论】:

标签: python python-3.x bots discord discord.py


【解决方案1】:

通过添加“on function error”作为自己的函数,您几乎可以发送任何错误消息。虽然对冷却错误很有用,但它也可以应用于MissingPermissions 错误。您可以在我的 GitHub here 上查看我的机器人,它在项目中有更多示例。

我想这就是你要找的东西:

@client.command()
@commands.cooldown(1, 40 commands.BucketType.user) # currently 40 seconds
async def function(ctx):
    ctx.send('whatever thing here')

@function.error
async def function_error(ctx, error):
    if isinstance(error, commands.CommandOnCooldown):
        await ctx.reply('This command is on cooldown! Try again in `{e:.1f}` seconds'.format(e = error.retry_after))
    else:
        raise error

【讨论】:

  • @function.error 将捕获此命令中的错误。因此,如果您要使用通用错误消息,最好使用on_command_error 来捕获任何命令中的冷却错误;然后是那里的通用消息。
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 2017-11-21
  • 2022-07-11
  • 2021-09-17
  • 2018-02-15
  • 2020-12-01
  • 2021-03-02
  • 2021-01-12
相关资源
最近更新 更多