Changed how delete time slot functions

This commit is contained in:
Vivek Santayana 2021-07-20 21:43:22 +01:00
parent c6113e31eb
commit f80a37f949
5 changed files with 72 additions and 38 deletions

View File

@ -62,7 +62,8 @@
- [x] Set up timeslots - [x] Set up timeslots
- [ ] Delete timeslots - [ ] Delete timeslots
> - [x] Base command > - [x] Base command
> - [ ] Delete all games with the timeslot > - [ ] ~~Delete all games with the timeslot~~
Do the opposite: block deleting timeslots with existing games.
- [x] List timeslots - [x] List timeslots
- [ ] Set up command permissions - [ ] Set up command permissions
> - [ ] Slash Commands > - [ ] Slash Commands
@ -88,3 +89,4 @@
> - [ ] CHANGELOG.md > - [ ] CHANGELOG.md
> - [ ] COMMANDS.md > - [ ] COMMANDS.md
> - [ ] resources.md > - [ ] resources.md
- [ ] Make sure to document **not using discord_components and staying with discord-py-slash-commands library alone**.

View File

@ -7,7 +7,7 @@ import discord # Main Lib
from discord.ext import commands # Commands module from discord.ext import commands # Commands module
from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash Command Library 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 discord_slash.utils.manage_commands import create_choice, create_option # Slash Command features
import discord_components # Additional Discord functions: buttons, menus, etc # import discord_components # Additional Discord functions: buttons, menus, etc
from deepdiff import DeepDiff from deepdiff import DeepDiff
import logging import logging

View File

@ -5,6 +5,7 @@ import discord # Main Lib
from discord.ext import commands # Commands module from discord.ext import commands # Commands module
from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash Command Library 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 discord_slash.utils.manage_commands import create_choice, create_option # Slash Command features
# import discord_components
import logging import logging
# logger and handler # logger and handler
from bot import checkConfig, clearConfig, configFile, parseConfigCheck, setConfig, yaml_dump, yaml_load, loadCogs, unloadCogs from bot import checkConfig, clearConfig, configFile, parseConfigCheck, setConfig, yaml_dump, yaml_load, loadCogs, unloadCogs
@ -17,6 +18,8 @@ class on_ready(commands.Cog, name='On Ready Events'):
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
# discord_components.DiscordComponents(self.client)
#### Create any missing config entries for guilds #### Create any missing config entries for guilds
for guild in self.client.guilds: for guild in self.client.guilds:
setConfig(guild) setConfig(guild)

View File

@ -7,8 +7,10 @@ from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash C
from discord_slash.utils.manage_commands import create_choice, create_option, create_permission # Slash Command features 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.model import SlashCommandPermissionType
from discord_slash.client import SlashCommand from discord_slash.client import SlashCommand
from discord_slash.utils.manage_components import create_select, create_select_option, create_actionrow, wait_for_component
# from discord_components import *
from bot import configFile, yaml_load, yaml_dump, reloadCog, cogsDir, unloadCog from bot import configFile, yaml_load, yaml_dump, reloadCog, cogsDir, unloadCog, dataFile
#### Separate cog to remove and modify timeslots that is reloaded if timeslots are added or removed #### Separate cog to remove and modify timeslots that is reloaded if timeslots are added or removed
@ -39,22 +41,53 @@ class ManipulateTimeslots(commands.Cog, name='Manipulate Timeslots'):
# base_permissions=permissions, # base_permissions=permissions,
# subcommand_group_description='Adds a time slot available to the channel for games.', # subcommand_group_description='Adds a time slot available to the channel for games.',
guild_ids=guild_ids, guild_ids=guild_ids,
options=[ # options=[
create_option( # create_option(
name='timeslot', # name='timeslot',
description='The timeslot you wish to delete.', # description='The timeslot you wish to delete.',
option_type=3, # option_type=3,
required=True # required=True
) # )
] # ]
) )
async def _config_timeslots_remove(self, ctx:SlashContext, timeslot:str): async def _config_timeslots_remove(self, ctx:SlashContext,):# timeslot:str):
conf = yaml_load(configFile) conf = yaml_load(configFile)
if 'timeslots' not in conf[str(ctx.guild.id)]: data = yaml_load(dataFile)
conf[str(ctx.guild.id)]['timeslots'] = {} guildStr = str(ctx.guild.id)
if timeslot in conf[str(ctx.guild.id)]['timeslots']: if 'timeslots' not in conf[guildStr]:
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[guildStr]['timeslots'] = {}
conf[str(ctx.guild.id)]['timeslots'].pop(timeslot, None) tsDict = conf[guildStr]['timeslots'].copy()
optionsList = [create_select_option(label=tsDict[x], value=x, description=x) for x in tsDict]
try:
m = await ctx.send(
content='```Select which time slot you would like to delete.```',
components=[
create_actionrow(
create_select(
placeholder='Time Slot',
options= optionsList,
min_values=1,
max_values=1
)
)
],
delete_after=5,
# hidden=True Can't have hidden responses for menus apparently.
)
while True:
select_ctx = await wait_for_component(self.client,messages=m, timeout=5)
if select_ctx.author != ctx.author:
await select_ctx.send(f'```Invalid response: you are not the person who issued the command.```', hidden=True)
else:
break
await m.delete()
[timeslot] = select_ctx.selected_options
except asyncio.TimeoutError:
await ctx.send(f'```Error: Command timed out.```', hidden=True)
return
if timeslot not in data[guildStr] or not data[guildStr][timeslot]:
await ctx.send(f'```Timeslot {conf[guildStr]["timeslots"][timeslot]} with the key `{timeslot}` has been deleted for the guild `{ctx.guild.name}`.```')
conf[guildStr]['timeslots'].pop(timeslot, None)
yaml_dump(conf, configFile) yaml_dump(conf, configFile)
if not all([x['timeslots'] for x in yaml_load(configFile).values()]): if not all([x['timeslots'] for x in yaml_load(configFile).values()]):
unloadCog(f'./{cogsDir}/slashcommands/secondary/manipulate_timeslots.py') unloadCog(f'./{cogsDir}/slashcommands/secondary/manipulate_timeslots.py')
@ -63,13 +96,8 @@ class ManipulateTimeslots(commands.Cog, name='Manipulate Timeslots'):
if self.client.get_cog('Game Setup') is not None: if self.client.get_cog('Game Setup') is not None:
unloadCog(f'./{cogsDir}/slashcommands/secondary/game_setup.py') unloadCog(f'./{cogsDir}/slashcommands/secondary/game_setup.py')
await self.client.slash.sync_all_commands() 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: else:
await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```') await ctx.send('```Error: You cannot delete a timeslot that has existing game entries. Please delete all games first.```')
@cog_ext.cog_subcommand( @cog_ext.cog_subcommand(
base='config', base='config',
@ -98,16 +126,17 @@ class ManipulateTimeslots(commands.Cog, name='Manipulate Timeslots'):
) )
async def _config_timeslots_modify(self, ctx:SlashContext, key:str, name:str): async def _config_timeslots_modify(self, ctx:SlashContext, key:str, name:str):
conf = yaml_load(configFile) conf = yaml_load(configFile)
if 'timeslots' not in conf[str(ctx.guild.id)]: guildStr = str(ctx.guild.id)
conf[str(ctx.guild.id)]['timeslots'] = {} if 'timeslots' not in conf[guildStr]:
if key in conf[str(ctx.guild.id)]['timeslots']: conf[guildStr]['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}`.```') if key in conf[guildStr]['timeslots']:
conf[str(ctx.guild.id)]['timeslots'][key] = name await ctx.send(f'```Timeslot {conf[guildStr]["timeslots"][key]} with the key `{key}` has been renamed to {name} for the guild `{ctx.guild.name}`.```')
conf[guildStr]['timeslots'][key] = name
yaml_dump(conf, configFile) yaml_dump(conf, configFile)
elif len(conf[str(ctx.guild.id)]['timeslots']) > 0: elif len(conf[guildStr]['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)' 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']: for c in conf[guildStr]['timeslots']:
output = ''.join([output, f'\n {c}: {conf[str(ctx.guild.id)]["timeslots"][c]}']) output = ''.join([output, f'\n {c}: {conf[guildStr]["timeslots"][c]}'])
await ctx.send(''.join([output,'```'])) await ctx.send(''.join([output,'```']))
else: else:
await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```') await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```')
@ -125,12 +154,13 @@ class ManipulateTimeslots(commands.Cog, name='Manipulate Timeslots'):
) )
async def _config_timeslots_list(self, ctx:SlashContext): async def _config_timeslots_list(self, ctx:SlashContext):
conf = yaml_load(configFile) conf = yaml_load(configFile)
if 'timeslots' not in conf[str(ctx.guild.id)]: guildStr = str(ctx.guild.id)
conf[str(ctx.guild.id)]['timeslots'] = {} if 'timeslots' not in conf[guildStr]:
if len(conf[str(ctx.guild.id)]['timeslots']) > 0: conf[guildStr]['timeslots'] = {}
if len(conf[guildStr]['timeslots']) > 0:
output = f'```The following timeslots have been configured for the guild {ctx.guild.name}:\n(key): (timeslot name)' 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']: for c in conf[guildStr]['timeslots']:
output = ''.join([output, f'\n {c}: {conf[str(ctx.guild.id)]["timeslots"][c]}']) output = ''.join([output, f'\n {c}: {conf[guildStr]["timeslots"][c]}'])
await ctx.send(''.join([output,'```'])) await ctx.send(''.join([output,'```']))
else: else:
await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```') await ctx.send(f'```No timeslots have been defined for the guild `{ctx.guild.name}`.```')

View File

@ -21,5 +21,4 @@
returning_player: 866645365524660224 returning_player: 866645365524660224
student: 866645394699714570 student: 866645394699714570
timeslots: timeslots:
sunaft: Sunday Afternoon 1 pm
suneve: Sunday Evening suneve: Sunday Evening