暂时禁用py-cord中的slash_command

问题描述 投票:0回答:1

我有用于多个项目的机器人代码。对于某些项目,我想完全禁用斜杠命令。我发现没有办法完全禁用斜杠命令..只能使用default_permission来处理这个问题。但在这里我只能为非管理员禁用它。

代码示例:

@bot.event
async def on_ready():
    xxxx.start()
    yyyy.start()
    zzzz.start()

@bot.slash_command(name="commandx")
async def commandx(update):
[..code..]

@bot.slash_command(name="commandy")
async def commandy(update):
[..code..]

我想禁用 commandx 但保留 commandy。

python pycord
1个回答
0
投票

这根本不可能

嗯,实际上没有办法做到这一点,因为不和谐改变了一些东西。 请参阅 Pycord 的 git 上的 github 帖子

@DamiansNase 由于不和谐切换到另一个系统,它不再存在。 请下次早点告诉自己,而不是对一个大约 2 年前的问题发表评论。

(来自 Pycord 上的顶级贡献者)

但是,我们可以尝试

这意味着我们必须自己编写一个禁用该命令的系统。但是,它仍然存在于不和谐端的应用程序命令中。

因此,一种方法是使用一个 cough 全局变量来禁用该命令。

在您的示例中,您说您想要:

我想禁用 commandx 但保留 commandy。

这应该不是问题,因为无论谁运行命令,全局变量都应该禁用它。这是一个例子:

@bot.slash_command()
async def test_command(ctx):
    global is_test_command_enabled  # Use the global variable
    if is_test_command_enabled:
        await ctx.respond("Hi!")
    else:  # make an exception for if it's disabled
        await ctx.respond("This command is disabled!")


@bot.slash_command()
async def disable_another_command(ctx):  # command to disable the test_command
    global is_test_command_enabled  # Use the global variable
    if is_test_command_enabled:
        is_test_command_enabled = False

    await ctx.respond("test_command is disabled!")

输出:

Global variable method working

你想避免咳嗽全局变量吗?

您可以以不同的方式实例化您的机器人,以便在机器人的自身变量中包含禁用变量。这是一个例子: # --- Bot Setup --- class bot(discord.Bot): def __init__(self): super().__init__(intents=discord.Intents.all()) self.is_test_command_enabled = True # self var to disable the below command @self.slash_command() # add it as a self command so that it can access the self var above async def test_command(ctx): if self.is_test_command_enabled: await ctx.respond("Hi!") else: await ctx.respond("This command is disabled!") Bot = bot() # instance bot @Bot.slash_command() async def disable_another_command(ctx): if Bot.is_test_command_enabled: Bot.is_test_command_enabled = False await ctx.respond("test_command is disabled!") Bot.run(os.getenv("TOKEN")) # run bot

这可能是一种有点混乱的方式来实例化你的机器人,但它确实有效。

输出:

The same thing as before! 和以前一样...

© www.soinside.com 2019 - 2024. All rights reserved.