discord.py重写|仅允许一个命令实例

时间:2019-02-24 14:50:58

标签: python discord.py-rewrite

在使用我的机器人时,我发现多个人可以同时使用同一命令。由于它的性质,我只希望一次执行一个命令。

是否有一种方法可以确保仅运行该命令的一个实例?如果有,请告诉我。我需要这种固定的方式,所以能提供任何帮助。

3 个答案:

答案 0 :(得分:1)

您可以对所有超时时间较长的用户进行冷却,然后在命令末尾重置冷却时间:

from discord.ext.commands import cooldown

@bot.command()
@cooldown(1, 1000)  # 1000 second cooldown
async def comm(ctx):
    ...
    comm.reset_cooldown(ctx)

答案 1 :(得分:0)

使用全局变量也是一种选择。

global set_active
set_active = 0
...
...
@bot.command("turn_on")
async def "Your command name"(ctx):
    global set_active
    #start command
    if set_active == 1:
        await ctx.send("This is already active")
    else/elif:
        ......
    #end command
    set_active = 1

@bot.command("turn_off")
async def "Your command name"(ctx):
    global set_active
    #start command
    ......
    #end command
    set_active = 0

我希望这很清楚。这是我第一次帮助别人。 我的机器人中也有这个。让我知道是否能为您提供更多帮助!

答案 2 :(得分:0)

在命令上使用@ commands.max_concurrecy(number,per =,wait = False)装饰器。

示例:

Retarget Solution

使用max_concurrency装饰器时,如果具有wait = False,则当实例超过指定数量时,它将返回MaxConcurrencyReached错误。错误显示与上述相同命令的示例如下

示例:

@commands.command()
@commands.max_concurrency(1, per=commands.BucketType.default, wait=False)
async def poll(self, ctx, *question):
    **Code**

如果wait = True,则该命令将在队列中等待直到可以运行。

请记住,您的漫游器中也应包含此行 @poll.error async def poll_handler(self, ctx, error): if isinstance(error, commands.MaxConcurrencyReached): (Whatever you want to do here)

相关问题