如何使用单个命令一次禁用所有命令

时间:2021-02-22 00:18:02

标签: discord.py

我想找到一种使用 discord.py 一次性禁用所有命令的方法,我是 discord.py 的新手,我还没有找到让它工作的方法。这是我的代码如下:

ID='no'
...............
@commands.command()
@commands.is_owner()
async def nocmdrun(self,ctx):   
    global ID
    ID = "yes"
    await ctx.send('No Command Run has been activated.')

@commands.command()
@commands.is_owner()
async def yescmdrun(self,ctx):
    try:
        global ID
        ID = "no"
        await ctx.send('No Command Run has been deactivated.')
    except:
        await ctx.send('Failed to deactivate No Command Run.')



def is_no_cmd_run_activated(self):
    global ID
    ID = "no"   
   


@commands.command()
@commands.check(is_no_cmd_run_activated)
async def tester(self,ctx):
    await ctx.send('congrats')

(这是在一个名为 developer.py 的齿轮中)

1 个答案:

答案 0 :(得分:0)

首先,将 import os 添加到主 .py 文件的开头。然后,要加载和卸载所有 cog 命令,请将此代码添加到主 .py 文件中:

@bot.command()
async def unload(ctx):
    for filename in os.listdir('./cogs'):
        if filename.endswith('.py'):
            bot.unload_extension(f'cogs.{filename[:-3]}')
    await ctx.send('commands unloaded successfully.')

另外,请确保您的 cog 文件所在的文件夹名为 cogs。 如果你想创建一个命令来加载所有的 cog 命令,你可以简单地这样做:

@bot.command()
async def load(ctx):
    for filename in os.listdir('./cogs'):
        if filename.endswith('.py'):
            bot.load_extension(f'cogs.{filename[:-3]}')
    await ctx.send('commands loaded successfully.')