首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >命令冷却无法识别ctx

命令冷却无法识别ctx
EN

Stack Overflow用户
提问于 2019-05-28 02:10:23
回答 1查看 147关注 0票数 0

好吧,基本上,我写了这段代码,它说

代码语言:javascript
复制
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)之后,但它给出了相同的错误。

代码语言:javascript
复制
@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事件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-28 02:56:44

你的冷却时间将永远不会被激活,并且总是会遇到错误。因为,由于ctx是Bot的Context-Container,所以不能作为属性放在那里。

@commands.cooldown的指定方式如下:

代码语言:javascript
复制
discord.ext.commands.cooldown(rate, per, type=<BucketType.default: 0>)

您必须使用要使用的Bucket而不是Context-Container (ctx)来传递Enum。

您可以使用的可用存储桶包括:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56330683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档