Added enable and disable to cogs
This commit is contained in:
19
bot.py
19
bot.py
@@ -28,7 +28,7 @@ bot = commands.Bot(command_prefix=determine_prefix,
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(work_with_cogs('load', bot, cog_list()))
|
asyncio.run(work_with_cogs('load', bot, asyncio.run(cog_list())))
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
@@ -50,7 +50,9 @@ async def on_ready():
|
|||||||
choices=[
|
choices=[
|
||||||
OptionChoice("load", "load"),
|
OptionChoice("load", "load"),
|
||||||
OptionChoice("unload", "unload"),
|
OptionChoice("unload", "unload"),
|
||||||
OptionChoice("reload", "reload")
|
OptionChoice("reload", "reload"),
|
||||||
|
OptionChoice("enable", "enable"),
|
||||||
|
OptionChoice("disable", "disable"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
Option(
|
Option(
|
||||||
@@ -61,15 +63,18 @@ async def on_ready():
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
async def slash_cogs(inter, what_do, cog: str = cog_list()) -> None:
|
async def slash_cogs(inter, what_do, cog: str = asyncio.run(cog_list())) -> None:
|
||||||
print(type(inter.guild.text_channels))
|
|
||||||
await work_with_cogs(what_do, bot, cog)
|
await work_with_cogs(what_do, bot, cog)
|
||||||
await inter.response.send_message(f'Cogs are {what_do}ed', ephemeral=True)
|
await inter.response.send_message(f'Cog {cog} is {what_do}ed', ephemeral=True)
|
||||||
|
|
||||||
|
|
||||||
@slash_cogs.autocomplete('cog')
|
@slash_cogs.autocomplete('cog')
|
||||||
async def _cog_opt(inter: disnake.ApplicationCommandInteraction, current: str) -> List[OptionChoice]:
|
async def _cog_opt(inter: disnake.ApplicationCommandInteraction, current: str, what_do) -> List[OptionChoice]:
|
||||||
_list = cog_list()
|
_what = ['load', 'reload', 'unload', 'disable']
|
||||||
|
if what_do in _what:
|
||||||
|
_list = await cog_list()
|
||||||
|
elif what_do == 'enable':
|
||||||
|
_list = await cog_list('./cogs/disabled/')
|
||||||
return [
|
return [
|
||||||
OptionChoice(name=choice, value=choice)
|
OptionChoice(name=choice, value=choice)
|
||||||
for choice in _list if current.lower() in choice.lower()
|
for choice in _list if current.lower() in choice.lower()
|
||||||
|
|||||||
@@ -6,15 +6,18 @@ cog_list: return list of cog filenames
|
|||||||
work_with_cogs: loads, reloads and unloads cogs files
|
work_with_cogs: loads, reloads and unloads cogs files
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
from os import listdir
|
from os import listdir
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from disnake.ext import commands
|
from disnake.ext import commands
|
||||||
from .Logger import logger
|
from .Logger import logger
|
||||||
|
|
||||||
|
|
||||||
def cog_list():
|
async def cog_list(fold='./cogs') -> List[str]:
|
||||||
cogs_list = []
|
cogs_list = []
|
||||||
for _filename in listdir('./cogs'):
|
for _filename in listdir(fold):
|
||||||
if _filename.endswith('.py'):
|
if _filename.endswith('.py'):
|
||||||
cogs_list.append(_filename[:-3])
|
cogs_list.append(_filename[:-3])
|
||||||
return cogs_list
|
return cogs_list
|
||||||
@@ -44,3 +47,11 @@ async def work_with_cogs(what_do, bot, cog):
|
|||||||
elif what_do == 'reload':
|
elif what_do == 'reload':
|
||||||
bot.reload_extension(f'cogs.{_filename}')
|
bot.reload_extension(f'cogs.{_filename}')
|
||||||
logger.info(f'Cog {_filename} reloaded')
|
logger.info(f'Cog {_filename} reloaded')
|
||||||
|
elif what_do == 'disable':
|
||||||
|
bot.unload_extension(f'cogs.{_filename}')
|
||||||
|
os.rename(f'cogs/{_filename}.py', f'cogs/disabled/{_filename}.py')
|
||||||
|
logger.info(f'Cog {_filename} stopped and disabled')
|
||||||
|
elif what_do == 'enable':
|
||||||
|
os.rename(f'cogs/disabled/{_filename}.py', f'cogs/{_filename}.py')
|
||||||
|
bot.load_extension(f'cogs.{_filename}')
|
||||||
|
logger.info(f'Cog {_filename} started and enabled')
|
||||||
|
|||||||
Reference in New Issue
Block a user