首页 > 解决方案 > 每个用户可以使用一次命令吗?

问题描述

我正在制作(高级)嵌入创建命令。基本上,您键入的任何内容都将显示为嵌入。长话短说,我告诉我的朋友测试它是否有任何错误。他尝试的第一件事是在不实际使用的情况下运行完全相同的命令。我的意思是什么?好吧,他只是输入/createembed了大约 x 次。该机器人开始响应每个呼叫,并且基本上最终发送垃圾邮件:

在此处输入图像描述

我认为现在更容易理解我的意思。无论如何,我知道如何通过告诉机器人用户每个用户只能使用这个确切的命令一次来阻止这种情况?

@commands.command()
async def cad(self, ctx, channel : discord.TextChannel = None):
    def check(message):
        return message.author == ctx.author and message.channel == ctx.channel

    aPP = ctx.author.avatar_url

    if not channel:
        await ctx.channel.send(f"**{ctx.author}** | Waiting for an Advanced Embed Title. If you wish to cancel this action, just type `Cancel`.")
        title = await self.client.wait_for("message", check=check)

        if title.content.lower() == "cancel" or title.content.upper() == "Cancel":
            await ctx.channel.send(f'**{ctx.author}** | Action has been canceled.')
            return

        else:
            await ctx.send(f"**{ctx.author}** | Waiting for an Advanced Embed Description. If you wish to cancel this action, just type `Cancel`.")
            desc = await self.client.wait_for('message', check=check)

            if desc.content.lower() == "cancel" or desc.content.upper() == "Cancel":
                await ctx.channel.send(f'**{ctx.author}** | Action has been canceled.')
                return
            
            else:
                await ctx.send(f"**{ctx.author}** | Waiting for an Advanced Embed Author Text. If you wish to skip this type `Skip` otherwise if you want to cancel this action, just type `Cancel`.")
                authortext = await self.client.wait_for('message', check=check)

                if authortext.content.lower() == "cancel" or authortext.content.upper() == "Cancel":
                    await ctx.channel.send(f'**{ctx.author}** | Action has been canceled.')
                    return

它有 500 多行,但基本上是一样的,有更多的回报等。

标签: pythonpython-3.xdiscord.py

解决方案


您可以使用discord.ext.commands 中的这个装饰器

例如 :

from discord.ext import commands

@commands.command()
@commands.max_concurrency(number=1, per=commands.BucketType.user, wait=False)
async def cad(self, ctx):
    #your command

将允许您的用户同时运行一次命令。


推荐阅读