命令冷却无法识别ctx

时间:2019-05-27 18:10:23

标签: python discord.py

好的,基本上,我写了这段代码,说的是

File "C:\Users\jellis\Desktop\Suggestion Bot\bot.py", line 28, in <module>
    @commands.cooldown(1, 1500, ctx)
NameError: name 'ctx' is not defined

我尝试将@commands.cooldown(1,1500 ctx)移到async def suggest(ctx, *args)之后,但是它给出了相同的错误。

@bot.command(pass_context = True)
@commands.cooldown(1, 1500, ctx)
async def suggest(ctx, *args):

    mesg = ' '.join(str(*args))
    embed = discord.Embed(title='New Suggestion', description='-----------', color=0x4C4CE7)

    if chatFilter in mesg:
        await bot.say(':x: Suggestion Could Not Be Sent.')
    elif chatFilter not in mesg:
        embed.add_field(name='{}'.format(ctx.message.author.display_name), value='{}'.format(mesg))
        await bot.send_message(discord.Object(id=suggestionsChannelID), embed=embed)

        white_check_mark = get(bot.get_all_emojis(), name='white_check_mark')
        await bot.add_reaction(message, white_check_mark)

        x = get(bot.get_all_emojis(), name='x')
        await bot.add_reaction(message, x)
        suggestionCount = suggestionCount + 1
    else:
        raise error


@bot.error
async def bot_error(error, member: discord.Member, ctx):
    if isinstance(error, commands.CommandOnCooldown):
        msg = ':x: {member} This command on cooldown, please try again in `{:.2f}s`'.format(error.retry_after)
        await bot.send_message(ctx.message.channel, msg)
    else:
        raise error

我希望它检测到命令处于冷却状态,然后运行@bot.error事件。

1 个答案:

答案 0 :(得分:1)

您的冷却时间将永远不会激活,并且始终会出现错误。 因为,由于ctx是Bot的上下文容器,因此不能作为属性去那里。

@ commands.cooldown的指定如下:

discord.ext.commands.cooldown(rate, per, type=<BucketType.default: 0>)

您必须将要使用的存储桶传递给枚举,而不是上下文容器(ctx)。

您可以使用的可用存储桶是:

BucketType.default for a global basis.

BucketType.user for a per-user basis.

BucketType.guild for a per-guild basis.

BucketType.channel for a per-channel basis.

BucketType.member for a per-member basis.

BucketType.category for a per-category basis.

有关更多信息,您可以在此处找到Discord Documentation

相关问题