geas-bot/app/cogs/controlcommands/control.py

101 lines
4.6 KiB
Python

import os
from dotenv import load_dotenv # Import OS variables from Dotenv file.
load_dotenv() # Load Dotenv. Delete this for production
import asyncio # Discord Py Dependency
import discord # Main Lib
from discord.ext import commands # Commands module
from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash Command Library
from discord_slash.utils.manage_commands import create_choice, create_option # Slash Command features
from deepdiff import DeepDiff
from pprint import pprint
from bot import loadCog, unloadCog, cogsDir, checkConfig, parseConfigCheck, yaml_load, configFile
##### Control Cog
class Control(commands.Cog, name='Cog Control Commands'):
def __init__(self, client):
self.client = client
#### Check if user is an administrator
async def cog_check(self, ctx:commands.Context):
for role in ctx.author.roles:
if role.permissions.administrator:
return True
return ctx.author.guild_permissions.administrator
@commands.command(
name='debug',
description='Toggles debug feature for the guild. Enter either `on` or `off`.',
brief='Toggle debug features.'
)
async def _debug(self, ctx:commands.Context, toggle:str):
if toggle.lower() == 'on':
loadCog(f'./debug/debug.py')
await ctx.reply(f'```Debug commands enabled. Use them carefully.```')
elif toggle.lower() == 'off':
unloadCog(f'./debug/debug.py')
await ctx.reply(f'```Debug commands disabled.```')
else:
raise commands.CommandError(message='Invalid argument.')
# await ctx.reply(f'```Invalid argument.```')
@commands.command(
name='testconfig',
description='Tests the completeness of the configuration values of the current guild by comparing it to a configuration blueprint.',
brief='Tests config values for current guild.',
aliases=['configtest']
)
async def _testconfig(self, ctx:commands.Context):
checkConfig(ctx.guild)
status, output = checkConfig(ctx.guild)
conf = yaml_load(configFile)
if not status:
await ctx.reply(f"```The Bot's configurations are incomplete for the guild {ctx.guild.name}. Some limited functions will still be available, but most features cannot be used until the configurations are complete.\n{parseConfigCheck(output)}\nYou can set these configuration values using the `/config` command.```")
elif status:
await ctx.reply(f"```The Bot's configurations for the guild {ctx.guild.name} are in order. The Bot is ready to interact with the guild.```")
@commands.command(
name='lockconfig',
description='Administrator command that locks the /config commands, preventing any accidental changes to configurations.',
brief='Toggle locking /config command.',
aliases=['configlock','lock','lockdownconfig']
)
async def _lockconfig(self, ctx:commands.Context, toggle:str):
if toggle == 'on':
o = ''
if self.client.get_cog('Configuration Commands') is not None:
unloadCog(f'./{cogsDir}/slashcommands/config.py')
o = ''.join([o,'Configuration Lock turned on. `/config` command has been disabled.'])
else:
o = ''.join([o,'`/config` command has already been disabled.'])
if self.client.get_cog('Manipulate Timeslots') is not None:
unloadCog(f'./{cogsDir}/slashcommands/secondary/manipulate_timeslots.py')
o = ''.join([o,'\nTimeslot configuration sub-commands have been disabled.'])
else:
o = ''.join([o,'\nTimeslot configuration sub-commands have already been disabled.'])
await ctx.reply(f'```{o}```')
await self.client.slash.sync_all_commands()
elif toggle == 'off':
o = ''
if self.client.get_cog('Configuration Commands') is None:
loadCog(f'./{cogsDir}/slashcommands/config.py')
await self.client.slash.sync_all_commands()
o = ''.join([o,'Configuration Lock turned off. `/config` command has been re-enabled.'])
else:
o = ''.join([o,'`/config` command has already been enabled.'])
if self.client.get_cog('Manipulate Timeslots') is None:
if all([len(yaml_load(configFile)[x]['timeslots']) > 0 for x in yaml_load(configFile)]):
loadCog(f'./{cogsDir}/slashcommands/secondary/manipulate_timeslots.py')
await self.client.slash.sync_all_commands()
o = ''.join([o,'\nTimeslot configuration sub-commands have been re-enabled.'])
else:
o = ''.join([o,'\nTimeslot configuration sub-commands are not re-enabled because there are no configured timeslots for the guild.'])
else:
o = ''.join([o,'\nTimeslot configuration sub-commands have already been enabled.'])
await ctx.reply(f'```{o}```')
await self.client.slash.sync_all_commands()
else:
raise commands.CommandError('Invalid argument. `lockconfig` command only accepts the arguments `on` or `off`.')
def setup(client):
client.add_cog(Control(client))