From e62342bc9aacb706642dc61a1b914a969afe8a28 Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Sun, 18 Jul 2021 23:16:58 +0100 Subject: [PATCH] Finished Config command group --- TODO.md | 15 +- app/bot.py | 33 +++-- app/cogs/controlcommands/control.py | 43 ------ app/cogs/devcommands/dev.py | 58 -------- .../{ => secondary}/on_slash_command_error.py | 0 app/cogs/slashcommands/config.py | 117 ++++++++++++++- app/cogs/slashcommands/game.py | 18 +++ .../secondary/edit_membership.py | 95 ++++++++++++ .../secondary/manipulate_timeslots.py | 135 ++++++++++++++++++ app/data/config.yml | 20 +-- app/data/config_blueprint.yml | 5 +- app/debug/debug.py | 135 ++++++++++++++---- 12 files changed, 508 insertions(+), 166 deletions(-) delete mode 100644 app/cogs/devcommands/dev.py rename app/cogs/events/{ => secondary}/on_slash_command_error.py (100%) create mode 100644 app/cogs/slashcommands/game.py create mode 100644 app/cogs/slashcommands/secondary/edit_membership.py create mode 100644 app/cogs/slashcommands/secondary/manipulate_timeslots.py diff --git a/TODO.md b/TODO.md index 6d53b06..04e1e20 100644 --- a/TODO.md +++ b/TODO.md @@ -7,11 +7,11 @@ > - [x] Correct references to data in existing cogs. - [x] Setup minimally functioning configs of guild on startup - [x] Synchronise core configuration `/commands` on startup -- [ ] Synchronise secondary `/commands` on complete configuration **(see below)** +- [ ] ~~Synchronise secondary `/commands` on complete configuration **(see below)**~~ ## Bot Functionality and Processes -- [ ] 'Delete Commands' Function -- [ ] 'Register Commands' Function +- [ ] ~~'Delete Commands' Function~~ +- [ ] ~~'Register Commands' Function~~ - [x] Infer Permissions from Config - [x] Dynamic Command Prefixes - [ ] Infer Games from Server Structure @@ -19,7 +19,7 @@ - [x] Delete Dev/Test Functions - [x] Error handlers - [ ] Debug Features -> - [ ] Command Installer/Uninstaller +> - [ ] ~~Command Installer/Uninstaller~~ - [x] Help Channel Event Listener > - [x] Add Config key for Help Channel - [ ] Slash Command Buttons or @@ -33,6 +33,8 @@ > - [ ] Message Receive Listener > - [ ] Membership Validation Listener - [ ] Re-synchronise commands after any relevant config changes **(see above)** +> - [ ] Role Delete (member, admin) +> - [ ] Channel delete (notifications, logs) - [x] Flag for checking completeness of configuration for a guild. > - [x] Function for checking configs for completeness ## Event Listeners @@ -54,8 +56,9 @@ > - [x] student role (role group) > - [x] help notifications (notification group) > - [x] signup notifications (notification group) -- [ ] Set up timeslots -- [ ] Delete timeslots +- [x] Set up timeslots +- [x] Delete timeslots +- [x] List timeslots - [ ] Set up command permissions > - [ ] Slash Commands >> - [x] Admin Commands diff --git a/app/bot.py b/app/bot.py index 7c2f09f..f0d326c 100644 --- a/app/bot.py +++ b/app/bot.py @@ -91,8 +91,8 @@ def setConfig(guild:discord.Guild): cDict['mod'] = guild.system_channel.id if 'configured' not in gDict or type(gDict['configured']) is not bool: gDict['configured'] = False - if 'membership' not in gDict or type(gDict['membership']) is not dict or None in gDict['membership']: - gDict['membership'] = {} + if 'membership' not in gDict or type(gDict['membership']) is not list or None in gDict['membership']: + gDict['membership'] = [] if 'name' not in gDict or gDict['name'] != guild.name: gDict['name'] = guild.name if 'owner' not in gDict or gDict['owner'] != guild.owner_id: @@ -169,7 +169,7 @@ def checkConfig(guild:discord.Guild): return conf[guildStr]['configured'], output def parseConfigCheck(missingKeys: list): - output = 'Configuration values for the following have not been defined:\n\n' + output = 'Configuration values for the following mandatory parameters have not been defined:\n\n' for entry in missingKeys: if '.' in entry: e1, e2 = entry.split('.') @@ -217,23 +217,40 @@ def unloadCog(filepath:str): path[-1] = path[-1][:-3] client.unload_extension('.'.join(path)) -def loadCogs(cogClass:str = 'all'): +def reloadCog(filepath:str): + path = os.path.normpath(filepath).split(os.path.sep) + if path[-1].endswith('.py'): + path[-1] = path[-1][:-3] + client.reload_extension('.'.join(path)) + +def loadCogs(cogClass:str = '--all'): for category in os.listdir(f'./{cogsDir}'): - if cogClass == 'all' or cogClass == category: + if cogClass == '--all' or cogClass == category: for cogfile in os.listdir(f'./{cogsDir}/{category}'): if cogfile.endswith('.py'): loadCog(f'./{cogsDir}/{category}/{cogfile}') -def unloadCogs(cogClass:str = 'all'): +def unloadCogs(cogClass:str = '--all'): for category in os.listdir(f'./{cogsDir}'): - if cogClass == 'all' or cogClass == category: + if cogClass == '--all' or cogClass == category: for cogfile in os.listdir(f'./{cogsDir}/{category}'): if cogfile.endswith('.py'): unloadCog(f'./{cogsDir}/{category}/{cogfile}') -loadCogs('devcommands') +def reloadCogs(cogClass:str = '--all'): + for category in os.listdir(f'./{cogsDir}'): + if cogClass == '--all' or cogClass == category: + for cogfile in os.listdir(f'./{cogsDir}/{category}'): + if cogfile.endswith('.py'): + reloadCog(f'./{cogsDir}/{category}/{cogfile}') + +loadCogs('controlcommands') loadCogs('events') loadCogs('botcommands') loadCogs('slashcommands') +if all([len(yaml_load(configFile)[x]['timeslots']) > 0 for x in yaml_load(configFile)]): + loadCog(f'./{cogsDir}/slashcommands/secondary/manipulate_timeslots.py') +if all([len(yaml_load(configFile)[x]['membership']) > 0 for x in yaml_load(configFile)]): + loadCog(f'./{cogsDir}/slashcommands/secondary/edit_membership.py') client.run(os.getenv('TEST_3_TOKEN')) \ No newline at end of file diff --git a/app/cogs/controlcommands/control.py b/app/cogs/controlcommands/control.py index 74570e0..fd6f1c6 100644 --- a/app/cogs/controlcommands/control.py +++ b/app/cogs/controlcommands/control.py @@ -54,48 +54,5 @@ class Control(commands.Cog, name='Cog Control Commands'): 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)) \ No newline at end of file diff --git a/app/cogs/devcommands/dev.py b/app/cogs/devcommands/dev.py deleted file mode 100644 index 6db72e8..0000000 --- a/app/cogs/devcommands/dev.py +++ /dev/null @@ -1,58 +0,0 @@ -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, checkConfig, parseConfigCheck, yaml_load, configFile - -##### Dev Cog -class Dev(commands.Cog, name='Developer Commands'): - def __init__(self, client): - self.client = client - - #### Check if user is an administrator - async def cog_check(self, ctx): - 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, 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): - 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.```") - -def setup(client): - client.add_cog(Dev(client)) \ No newline at end of file diff --git a/app/cogs/events/on_slash_command_error.py b/app/cogs/events/secondary/on_slash_command_error.py similarity index 100% rename from app/cogs/events/on_slash_command_error.py rename to app/cogs/events/secondary/on_slash_command_error.py diff --git a/app/cogs/slashcommands/config.py b/app/cogs/slashcommands/config.py index e4da52c..8942a77 100644 --- a/app/cogs/slashcommands/config.py +++ b/app/cogs/slashcommands/config.py @@ -6,11 +6,12 @@ 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, create_permission # Slash Command features from discord_slash.model import SlashCommandPermissionType +import re -from bot import configFile, yaml_load, yaml_dump +from bot import configFile, yaml_load, yaml_dump, loadCog, unloadCog, reloadCog, cogsDir, slash ##### Configuration Cog -class Configuration(commands.Cog): +class Configuration(commands.Cog, name='Configuration Commands'): def __init__(self, client): self.client = client @@ -168,5 +169,117 @@ class Configuration(commands.Cog): yaml_dump(conf, configFile) await ctx.send(f'```Notifications for posts in the `{channel}` channel for the guild `{ctx.guild.name}` have been set to `{notifications}`.```') + @cog_ext.cog_subcommand( + base='config', + subcommand_group='timeslots', + name='add', + description='Add a timeslot at which the Guild will host games.', + # base_description='Commands for configuring the various parameters of the Guild', + # base_default_permission=False, + # base_permissions=permissions, + subcommand_group_description='Manages timeslots available for games on the guild.', + guild_ids=guild_ids, + options=[ + create_option( + name='key', + description='Alphanumeric time code 10 chars max.', + option_type=3, + required=True, + ), + create_option( + name='name', + description='A longer, descriptive name of when the timeslot is', + option_type=3, + required=True + ) + ] + ) + async def _config_timeslots_add(self, ctx:SlashContext, key:str, name:str): + sanitisedKey = re.sub(r"\W+",'', key[:9].lower()) + if not key.isalnum(): + await ctx.send(f'```Key value {key} is not a valid alphanumeric time code. Sanitising to `{sanitisedKey}`.```') + conf = yaml_load(configFile) + if 'timeslots' not in conf[str(ctx.guild.id)]: + conf[str(ctx.guild.id)]['timeslots'] = {} + if sanitisedKey in conf[str(ctx.guild.id)]['timeslots']: + await ctx.send(f'```Key value {sanitisedKey} has already been defined for guild `{ctx.guild.name}` for `{conf[str(ctx.guild.id)]["timeslots"][sanitisedKey]}`. Please use the `remove` or `modify` sub-commands to amend it.```') + return + conf[str(ctx.guild.id)]['timeslots'][sanitisedKey] = name + yaml_dump(conf, configFile) + await ctx.send(f'```Timeslot `{name}` with the key `{sanitisedKey}` has been added for the guild `{ctx.guild.name}`.```') + if all([len(yaml_load(configFile)[x]['timeslots']) > 0 for x in yaml_load(configFile)]): + if self.client.get_cog('Manipulate Timeslots') is None: + loadCog(f'./{cogsDir}/slashcommands/secondary/manipulate_timeslots.py') + await self.client.slash.sync_all_commands() + + @cog_ext.cog_subcommand( + base='config', + subcommand_group='membership', + name='add', + description='Add a membership type for the Guild.', + # base_description='Commands for configuring the various parameters of the Guild', + # base_default_permission=False, + # base_permissions=permissions, + subcommand_group_description='Manages the different categories of membership available to the Guild.', + guild_ids=guild_ids, + options=[ + create_option( + name='name', + description='Name of membership type.', + option_type=3, + required=True, + ), + create_option( + name='role_exists', + description='Does the role for this member type already exist?', + option_type=5, + required=True + ), + create_option( + name='role', + description='Assign the role if it already exists.', + option_type=8, + required=False + ) + ] + ) + async def _config_membership_add(self, ctx:SlashContext, name:str, role_exists:bool, role:discord.Role=None): + if role_exists and role is None: + await ctx.send(f'```If the role for membership type `{name}` already exists, you must assign it. If it has not been assigned, the Bot will create one.```') + return + if not role_exists and role is not None: + await ctx.send(f'```You have specified a role for `{name}` does not already exist but have also specified a role to assign. Please either assign a role if it exists, or leave it blank if does not.```') + return + conf = yaml_load(configFile) + if 'membership' not in conf[str(ctx.guild.id)]: + conf[str(ctx.guild.id)]['membership'] = [] + if role is not None: + if role.id in conf[str(ctx.guild.id)]['membership']: + await ctx.send(f'```The role {name} has already been assigned to a membership type for guild `{ctx.guild.name}`. Please use the `remove` sub-command to delete it or assign a different role.```') + return + if any([ctx.guild.get_role(m).name == name for m in conf[str(ctx.guild.id)]['membership']]): + await ctx.send(f'```The membership type {name} has already been assigned a role for guild `{ctx.guild.name}`. Please use the `remove` sub-command to delete the role or assign a different membership type.```') + return + if not role_exists: + r = await ctx.guild.create_role( + name=name, + permissions=discord.Permissions(read_messages=True,use_slash_commands=True), + mentionable=False + ) + if role is not None: + await role.edit( + name=name, + permissions=discord.Permissions(read_messages=True,use_slash_commands=True), + mentionable=False, + reason=f'`/config membership add` command issued by {ctx.author.display_name}' + ) + conf[str(ctx.guild.id)]['membership'].append(role.id) if role is not None else conf[str(ctx.guild.id)]['membership'].append(r.id) + yaml_dump(conf, configFile) + await ctx.send(f'```Membership type `{role.name if role is not None else r.name}` has been registered for the guild `{ctx.guild.name}`.```') + if all([len(yaml_load(configFile)[x]['membership']) > 0 for x in yaml_load(configFile)]): + if self.client.get_cog('Edit Membership') is None: + loadCog(f'./{cogsDir}/slashcommands/secondary/edit_membership.py') + await self.client.slash.sync_all_commands() + def setup(client): client.add_cog(Configuration(client)) \ No newline at end of file diff --git a/app/cogs/slashcommands/game.py b/app/cogs/slashcommands/game.py new file mode 100644 index 0000000..bfceebf --- /dev/null +++ b/app/cogs/slashcommands/game.py @@ -0,0 +1,18 @@ +import os # OS Locations +import yaml # YAML parser for Bot config files +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, create_permission # Slash Command features +from discord_slash.model import SlashCommandPermissionType + +from bot import configFile, yaml_load, yaml_dump + +##### Game Management Commands +class Game_Management(commands.Cog, name='Game Management Commands'): + def __init__(self, client): + self.client = client + +def setup(client): + client.add_cog(Game_Management(client)) \ No newline at end of file diff --git a/app/cogs/slashcommands/secondary/edit_membership.py b/app/cogs/slashcommands/secondary/edit_membership.py new file mode 100644 index 0000000..15b0d92 --- /dev/null +++ b/app/cogs/slashcommands/secondary/edit_membership.py @@ -0,0 +1,95 @@ +import os # OS Locations +import yaml # YAML parser for Bot config files +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, create_permission # Slash Command features +from discord_slash.model import SlashCommandPermissionType +from discord_slash.client import SlashCommand + +from bot import configFile, yaml_load, yaml_dump, reloadCog, cogsDir, unloadCog + + #### Separate cog to remove and modify membership registrations that is reloaded if timeslots are added or removed + +class EditMembership(commands.Cog, name='Edit Membership'): + def __init__(self, client): + self.client = client + + #### Only emable for guilds with registered membership types + #### N.B.: if there are no guilds with any membership types, then this will throw an exception. + #### The solution I have implemented is that this will be classed as a 'secondary' cog: it will not be loaded by default, and will only be loaded if at least one guild has a membership role registered. + #### If the deletion of membership roles removes memberships from all guilds, it will unload the cog and delete the commands until a new membership role is defined. + guild_ids=[int(guildKey) for guildKey in yaml_load(configFile) if len(yaml_load(configFile)[guildKey]['membership']) > 0] + conf = yaml_load(configFile) + permissions = {} + for guildID in guild_ids: + permissions[guildID] = [] + permissions[guildID].append(create_permission(id=conf[str(guildID)]['owner'],id_type=SlashCommandPermissionType.USER,permission=True)) + for admin in conf[str(guildID)]['roles']['admin']: + permissions[guildID].append(create_permission(id=admin,id_type=SlashCommandPermissionType.ROLE,permission=True)) + + @cog_ext.cog_subcommand( + base='config', + subcommand_group='membership', + name='remove', + description='Remove a registered membership role.', + # base_description='Commands for configuring the various parameters of the Guild', + # base_default_permission=False, + # base_permissions=permissions, + # subcommand_group_description='Adds a time slot available to the channel for games.', + guild_ids=guild_ids, + options=[ + create_option( + name='role', + description='The role of the membership type you want to delete.', + option_type=8, + required=True + ) + ] + ) + async def _config_membership_remove(self, ctx:SlashContext, role:discord.Role): + conf = yaml_load(configFile) + if 'membership' not in conf[str(ctx.guild.id)]: + conf[str(ctx.guild.id)]['timeslots'] = {} + if role.id in conf[str(ctx.guild.id)]['membership']: + conf[str(ctx.guild.id)]['membership'].remove(role.id) + yaml_dump(conf, configFile) + await ctx.send(f'```Membership type {role.name} has been deleted for the guild `{ctx.guild.name}`.```') + await role.delete(reason=f'`/config membership remove` command issued by `{ctx.author.display_name}`.') + if not all([len(yaml_load(configFile)[x]['membership']) > 0 for x in yaml_load(configFile)]): + unloadCog(f'./{cogsDir}/slashcommands/secondary/edit_membership.py') + await self.client.slash.sync_all_commands() + elif len(conf[str(ctx.guild.id)]['membership']) > 0: + output = f'Role `{role.name}` is not a registered membership role in the guild `{ctx.guild.name}`. Please select a valid membership role.\n\n Eligible roles are:\n' + for m in conf[str(ctx.guild.id)]['membership']: + output = ''.join([output, f'\n{ctx.guild.get_role(m).name}']) + await ctx.send(''.join(['```',output,'```'])) + else: + await ctx.send(f'```No roles have been registered as membership types for the guild `{ctx.guild.name}`.```') + + @cog_ext.cog_subcommand( + base='config', + subcommand_group='membership', + name='list', + description='List the existing game memberships on the server.', + # base_description='Commands for configuring the various parameters of the Guild', + # base_default_permission=False, + # base_permissions=permissions, + # subcommand_group_description='Adds a time slot available to the channel for games.', + guild_ids=guild_ids, + ) + async def _config_timeslots_list(self, ctx:SlashContext): + conf = yaml_load(configFile) + if 'membership' not in conf[str(ctx.guild.id)]: + conf[str(ctx.guild.id)]['membership'] = {} + if len(conf[str(ctx.guild.id)]['membership']) > 0: + output = f'The following membership types have been registered for the guild {ctx.guild.name}:\n' + for m in conf[str(ctx.guild.id)]['membership']: + output = ''.join([output, f'\n{ctx.guild.get_role(m).name}']) + await ctx.send(''.join(['```',output,'```'])) + else: + await ctx.send(f'```No roles have been registered as membership types for the guild `{ctx.guild.name}`.```') + +def setup(client): + client.add_cog(EditMembership(client)) \ No newline at end of file diff --git a/app/cogs/slashcommands/secondary/manipulate_timeslots.py b/app/cogs/slashcommands/secondary/manipulate_timeslots.py new file mode 100644 index 0000000..0cd03a3 --- /dev/null +++ b/app/cogs/slashcommands/secondary/manipulate_timeslots.py @@ -0,0 +1,135 @@ +import os # OS Locations +import yaml # YAML parser for Bot config files +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, create_permission # Slash Command features +from discord_slash.model import SlashCommandPermissionType +from discord_slash.client import SlashCommand + +from bot import configFile, yaml_load, yaml_dump, reloadCog, cogsDir, unloadCog + + #### Separate cog to remove and modify timeslots that is reloaded if timeslots are added or removed + +class ManipulateTimeslots(commands.Cog, name='Manipulate Timeslots'): + def __init__(self, client): + self.client = client + + #### Only emable for guilds with timeslots + #### N.B.: if there are no guilds with any timeslots, then this will throw an exception. + #### The solution I have implemented is that this will be classed as a 'secondary' cog: it will not be loaded by default, and will only be loaded if at least one guild has a timeslot configured. + #### If the deletion of timeslots removes timeslots from all guilds, it will unload the cog and delete the commands until a new timeslot is defined. + guild_ids=[int(guildKey) for guildKey in yaml_load(configFile) if len(yaml_load(configFile)[guildKey]['timeslots']) > 0] + conf = yaml_load(configFile) + permissions = {} + for guildID in guild_ids: + permissions[guildID] = [] + permissions[guildID].append(create_permission(id=conf[str(guildID)]['owner'],id_type=SlashCommandPermissionType.USER,permission=True)) + for admin in conf[str(guildID)]['roles']['admin']: + permissions[guildID].append(create_permission(id=admin,id_type=SlashCommandPermissionType.ROLE,permission=True)) + + @cog_ext.cog_subcommand( + base='config', + subcommand_group='timeslots', + name='remove', + description='Remove a configured game timeslot.', + # base_description='Commands for configuring the various parameters of the Guild', + # base_default_permission=False, + # base_permissions=permissions, + # subcommand_group_description='Adds a time slot available to the channel for games.', + guild_ids=guild_ids, + options=[ + create_option( + name='timeslot', + description='The timeslot you wish to delete.', + option_type=3, + required=True + ) + ] + ) + async def _config_timeslots_remove(self, ctx:SlashContext, timeslot:str): + conf = yaml_load(configFile) + if 'timeslots' not in conf[str(ctx.guild.id)]: + conf[str(ctx.guild.id)]['timeslots'] = {} + if timeslot in conf[str(ctx.guild.id)]['timeslots']: + await ctx.send(f'```Timeslot {conf[str(ctx.guild.id)]["timeslots"][timeslot]} with the key `{timeslot}` has been deleted for the guild `{ctx.guild.name}`.```') + conf[str(ctx.guild.id)]['timeslots'].pop(timeslot, None) + yaml_dump(conf, configFile) + if not all([len(yaml_load(configFile)[x]['timeslots']) > 0 for x in yaml_load(configFile)]): + unloadCog(f'./{cogsDir}/slashcommands/secondary/manipulate_timeslots.py') + await self.client.slash.sync_all_commands() + elif len(conf[str(ctx.guild.id)]['timeslots']) > 0: + output = f'```Timeslot `{timeslot}` was not found in the guild `{ctx.guild.name}`. Please enter a valid key.\n\n Available timeslots are:\n(key): (timeslot name)' + for c in conf[str(ctx.guild.id)]['timeslots']: + output = ''.join([output, f'\n {c}: {conf[str(ctx.guild.id)]["timeslots"][c]}']) + await ctx.send(''.join([output,'```'])) + else: + await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```') + + @cog_ext.cog_subcommand( + base='config', + subcommand_group='timeslots', + name='modify', + description='Modify the value of a configured gametime slot.', + # base_description='Commands for configuring the various parameters of the Guild', + # base_default_permission=False, + # base_permissions=permissions, + # subcommand_group_description='Adds a time slot available to the channel for games.', + guild_ids=guild_ids, + options=[ + create_option( + name='key', + description='Key of timeslot being modified', + option_type=3, + required=True, + ), + create_option( + name='name', + description='New value for timeslot name', + option_type=3, + required=True + ) + ] + ) + async def _config_timeslots_modify(self, ctx:SlashContext, key:str, name:str): + conf = yaml_load(configFile) + if 'timeslots' not in conf[str(ctx.guild.id)]: + conf[str(ctx.guild.id)]['timeslots'] = {} + if key in conf[str(ctx.guild.id)]['timeslots']: + await ctx.send(f'```Timeslot {conf[str(ctx.guild.id)]["timeslots"][key]} with the key `{key}` has been renamed to {name} for the guild `{ctx.guild.name}`.```') + conf[str(ctx.guild.id)]['timeslots'][key] = name + yaml_dump(conf, configFile) + elif len(conf[str(ctx.guild.id)]['timeslots']) > 0: + output = f'```Timeslot `{key}` was not found in the guild `{ctx.guild.name}`. Please enter a valid key.\n\n Available timeslots are:\n(key): (timeslot name)' + for c in conf[str(ctx.guild.id)]['timeslots']: + output = ''.join([output, f'\n {c}: {conf[str(ctx.guild.id)]["timeslots"][c]}']) + await ctx.send(''.join([output,'```'])) + else: + await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```') + + @cog_ext.cog_subcommand( + base='config', + subcommand_group='timeslots', + name='list', + description='List the existing game timeslots on the server.', + # base_description='Commands for configuring the various parameters of the Guild', + # base_default_permission=False, + # base_permissions=permissions, + # subcommand_group_description='Adds a time slot available to the channel for games.', + guild_ids=guild_ids, + ) + async def _config_timeslots_list(self, ctx:SlashContext): + conf = yaml_load(configFile) + if 'timeslots' not in conf[str(ctx.guild.id)]: + conf[str(ctx.guild.id)]['timeslots'] = {} + if len(conf[str(ctx.guild.id)]['timeslots']) > 0: + output = f'```The following timeslots have been configured for the guild {ctx.guild.name}:\n(key): (timeslot name)' + for c in conf[str(ctx.guild.id)]['timeslots']: + output = ''.join([output, f'\n {c}: {conf[str(ctx.guild.id)]["timeslots"][c]}']) + await ctx.send(''.join([output,'```'])) + else: + await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```') + +def setup(client): + client.add_cog(ManipulateTimeslots(client)) \ No newline at end of file diff --git a/app/data/config.yml b/app/data/config.yml index 9a2d40f..9e26dfe 100644 --- a/app/data/config.yml +++ b/app/data/config.yml @@ -1,19 +1 @@ -'864651943820525609': - channels: - help: 866110872584978443 - mod: 865662560225067018 - signup: 866110421592965171 - configured: false - membership: {} - name: Test - notifications: - help: true - signup: true - owner: 493694762210033664 - prefix: '-' - roles: - admin: - - 864661232005939280 - bot: 864661167297527830 - committee: 864661232005939280 - timeslots: [] +{} \ No newline at end of file diff --git a/app/data/config_blueprint.yml b/app/data/config_blueprint.yml index fd64759..6ed75bc 100644 --- a/app/data/config_blueprint.yml +++ b/app/data/config_blueprint.yml @@ -4,7 +4,7 @@ guild_id_string: mod: 0 signup: 0 configured: false - membership: {} # Dictionary + membership: [0] # List of integers # For membership, at least one kind needs to be defined. But no key is mandatory. name: string owner: 0 @@ -18,8 +18,7 @@ guild_id_string: # newcomer role is optional # returning player role is optional # student role is optional - timeslots: - - string # List + timeslots: {} # Dictionary # At least one needs to be defined. meta: strict: diff --git a/app/debug/debug.py b/app/debug/debug.py index 44ccfe8..7a331c4 100644 --- a/app/debug/debug.py +++ b/app/debug/debug.py @@ -6,8 +6,9 @@ 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 pprint import pprint -from bot import clearConfig, configFile, loadCog, loadCogs, setConfig, unloadCog, unloadCogs, yaml_dump, yaml_load +from bot import clearConfig, configFile, loadCog, loadCogs, setConfig, unloadCog, unloadCogs, yaml_dump, yaml_load, reloadCog, reloadCogs ##### Debug Cog class Debug(commands.Cog, name='Debug Commands'): @@ -23,9 +24,8 @@ class Debug(commands.Cog, name='Debug Commands'): description='Reloads cogs within the specified category, or provide `all` for all cogs. Default: `all`.', brief='Reload multiple cogs by category.' ) - async def _reload(self, ctx, cog_category: str='all'): - unloadCogs(cog_category) - loadCogs(cog_category) + async def _reload(self, ctx, cog_category: str='--all'): + reloadCogs(cog_category) await ctx.reply(f'````{cog_category}` cogs have been reloaded.```') @commands.command( @@ -33,7 +33,7 @@ class Debug(commands.Cog, name='Debug Commands'): description='Unload cogs within the specified category, or provide `all` for all cogs. Default: `all`.', brief='Unload multiple cogs by category.' ) - async def _unloadcogs(self, ctx, cog_category: str='all'): + async def _unloadcogs(self, ctx, cog_category: str='--all'): unloadCogs(cog_category) loadCogs(cog_category) await ctx.reply(f'````{cog_category}` cogs have been unloaded.```') @@ -43,30 +43,11 @@ class Debug(commands.Cog, name='Debug Commands'): description='Load cogs within the specified category, or provide `all` for all cogs. Default: `all`.', brief='Load multiple cogs by category.' ) - async def _loadcogs(self, ctx, cog_category: str='all'): + async def _loadcogs(self, ctx, cog_category: str='--all'): unloadCogs(cog_category) loadCogs(cog_category) await ctx.reply(f'````{cog_category}` cogs have been loaded.```') - @commands.command( - name='deletecommands', - aliases=['delallcommands','deleteslashcommands','clearcommands','clearslashcommands'], - description='Deletes all the public and guild slash commands registered by the bot.', - brief='Delets all slash commands' - ) - async def _deleteAll(self, ctx): - await utils.manage_commands.remove_all_commands( - bot_id=self.client.user.id, - bot_token=os.getenv('TEST_3_TOKEN'), - guild_ids=None - ) - await utils.manage_commands.remove_all_commands( - bot_id=self.client.user.id, - bot_token=os.getenv('TEST_3_TOKEN'), - guild_ids=[ int(g) for g in yaml_load(configFile)] - ) - await ctx.reply('```All slash commands have been deleted.```') - @commands.command( name='retrievecommands', aliases=['slashcommands','retrieveslashcommands'], @@ -74,10 +55,74 @@ class Debug(commands.Cog, name='Debug Commands'): brief='Retrieves registered slash commands to console.' ) async def _retrievecommands(self, ctx:commands.Context): - c = await utils.manage_commands.get_all_commands(self.client.user.id,os.getenv('TEST_3_TOKEN'),guild_id=ctx.guild.id) - print(c) + c = await utils.manage_commands.get_all_commands( + bot_id=self.client.user.id, + bot_token=os.getenv('TEST_3_TOKEN'), + guild_id=ctx.guild.id + ) + pprint(c) await ctx.reply(f'```All registered `/commands` have been fetched and sent to the Python console.```') + @commands.command( + name='deletecommand', + aliases=['removecommand','delcommand','removeslashcommand', 'clearcommand', 'clearslashcommand'], + description='Debugging command that deletes a specified slash command. Key parameters `--all` for all commands in guild and `--global` for all commands globally', + brief='Deletes slash command. Default: all local commands.' + ) + async def _deleteCommand(self, ctx:commands.Context, command: str='--all'): + if command == '--all' or command == '-a': + await utils.manage_commands.remove_all_commands( + bot_id=self.client.user.id, + bot_token=os.getenv('TEST_3_TOKEN'), + guild_ids=[ ctx.guild.id ] + ) + await ctx.reply(f'```All slash commands have been deleted for the guold {ctx.guild.name}.```') + elif command == '--global' or command == '-g': + await utils.manage_commands.remove_all_commands( + bot_id=self.client.user.id, + bot_token=os.getenv('TEST_3_TOKEN'), + guild_ids=None + ) + await utils.manage_commands.remove_all_commands( + bot_id=self.client.user.id, + bot_token=os.getenv('TEST_3_TOKEN'), + guild_ids=[ int(g) for g in yaml_load(configFile)] + ) + await ctx.reply('```All slash commands have been deleted globally.```') + else: + c = await utils.manage_commands.get_all_commands( + bot_id=self.client.user.id, + bot_token=os.getenv('TEST_3_TOKEN'), + guild_id=ctx.guild.id + ) + # try: + target = list(filter(lambda t: t['name'] == command, c))[0]['id'] + await utils.manage_commands.remove_slash_command( + bot_id=self.client.user.id, + bot_token=os.getenv('TEST_3_TOKEN'), + guild_id=ctx.guild.id, + cmd_id=target + ) + await ctx.reply(f'```Slash command {command} has been deleted for the guild {ctx.guild.name}.```') + # except: + # raise commands.CommandError(message=f'The command `/{command}` was not found.') + + @commands.command( + name='addcommand', + aliases=['installcommand','addslashcommand'], + description='Adds a slash command to the guild. Use keyword `--global` to add command globally.', + brief='Adds slash command' + ) + async def _addCommand(self, ctx:commands.Context, command:str, key:str=''): + await utils.manage_commands.add_slash_command( + bot_id=self.client.user.id, + bot_token=os.getenv('TEST_3_TOKEN'), + guild_id= None if key == '--global' or key == '-g' else ctx.guild.id, + cmd_name=command, + description='No Description' + ) + await ctx.reply(f'```The command /{command} has been added for the guild {ctx.guild.name}.```') + @commands.command( name='clearconfig', aliases=['configclear'], @@ -100,5 +145,41 @@ class Debug(commands.Cog, name='Debug Commands'): setConfig(ctx.guild) await ctx.reply(f'```Config entry has been added for guild `{ctx.guild.name}`.```') + @commands.command( + name='synccommands', + aliases=['syncallcommands','syncslashcommands','resynccommands','sync','resync','syncall','resyncall'], + description='Syncs all slash commands between the bot and the Server.', + brief='Resyncs slash commands.' + ) + async def _synccommands(self, ctx:commands.Context): + await self.client.slash.sync_all_commands() + await ctx.reply(f'```All slash commands have been synced with the Server.```') + + # @commands.command( + # name='removecogcommands', + # aliases=['clearcogcommands'], + # description='Removes /commands defined in a particular cog.', + # brief='Remove /command by cog.' + # ) + # async def _removecogcommands(self, ctx:commands.Context, cogName:str): + # try: + # SlashCommand.remove_cog_commands(self.client, cogName) + # await ctx.reply(f'```All commands from cog `{cogName}` have been removed.```') + # except Exception as e: + # raise commands.CommandError(e) + + # @commands.command( + # name='getcogcommands', + # aliases=['addcogcommands'], + # description='Adds /commands defined in a particular cog.', + # brief='Add /command by cog.' + # ) + # async def _getcogcommands(self, ctx:commands.Context, cogName:str): + # try: + # SlashCommand.get_cog_commands(cogName) + # await ctx.reply(f'```All commands from cog `{cogName}` have been removed.```') + # except Exception as e: + # raise commands.CommandError(e) + def setup(client): client.add_cog(Debug(client)) \ No newline at end of file