Merge pull request #14 from mdiluz/add-cancel

Add /cancel
This commit is contained in:
Marc Di Luzio 2024-08-17 00:15:39 +01:00 committed by GitHub
commit b4df026efb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 37 deletions

View file

@ -18,8 +18,9 @@ Matchy supports a bunch of user, `matcher` and bot owner commands. `/` commands
| /leave | user | Leaves the matchee list | | /leave | user | Leaves the matchee list |
| /pause | user | Pauses the user for `days: int` days | | /pause | user | Pauses the user for `days: int` days |
| /list | user | Lists the current matchees and scheduled matches | | /list | user | Lists the current matchees and scheduled matches |
| /match | user | Shares a preview of the matchee groups of size `group_min: int` with the user, offers a button to post the match to `matcher` users | | /match | user* | Shares a preview of the matchee groups of size `group_min: int` with the user. *Offers a button to post the match to `matcher` users |
| /schedule | `matcher` | Shedules a match every week with `group_min: int` users on `weekday: int` day and at `hour: int` hour. Can pass `cancel: True` to stop the schedule | | /schedule | `matcher` | Schedules a match every week with `group_min: int` users on `weekday: int` day and at `hour: int` hour. Can pass `cancel: True` to stop the schedule |
| /cancel | `matcher` | Cancels any scheduled matches in this channel |
| $sync | bot owner | Syncs bot command data with the discord servers | | $sync | bot owner | Syncs bot command data with the discord servers |
| $close | bot owner | Closes the bot connection so the bot quits safely | | $close | bot owner | Closes the bot connection so the bot quits safely |
| $grant | bot owner | Grants matcher to a given user (ID) | | $grant | bot owner | Grants matcher to a given user (ID) |

View file

@ -89,7 +89,7 @@ class MatcherCog(commands.Cog):
for (day, hour, min) in tasks: for (day, hour, min) in tasks:
next_run = util.get_next_datetime(day, hour) next_run = util.get_next_datetime(day, hour)
date_str = util.datetime_as_discord_time(next_run) date_str = util.datetime_as_discord_time(next_run)
msg += f"\nNext scheduled for {date_str}" msg += f"\nNext scheduled at {date_str}"
msg += f" with {min} members per group" msg += f" with {min} members per group"
await interaction.response.send_message(msg, ephemeral=True, silent=True) await interaction.response.send_message(msg, ephemeral=True, silent=True)
@ -98,14 +98,12 @@ class MatcherCog(commands.Cog):
@commands.guild_only() @commands.guild_only()
@app_commands.describe(members_min="Minimum matchees per match (defaults to 3)", @app_commands.describe(members_min="Minimum matchees per match (defaults to 3)",
weekday="Day of the week to run this (defaults 0, Monday)", weekday="Day of the week to run this (defaults 0, Monday)",
hour="Hour in the day (defaults to 9 utc)", hour="Hour in the day (defaults to 9 utc)")
cancel="Cancel the scheduled match at this time")
async def schedule(self, async def schedule(self,
interaction: discord.Interaction, interaction: discord.Interaction,
members_min: int | None = None, members_min: int | None = None,
weekday: int | None = None, weekday: int | None = None,
hour: int | None = None, hour: int | None = None):
cancel: bool = False):
"""Schedule a match using the input parameters""" """Schedule a match using the input parameters"""
# Set all the defaults # Set all the defaults
@ -124,30 +122,36 @@ class MatcherCog(commands.Cog):
return return
# Add the scheduled task and save # Add the scheduled task and save
success = self.state.set_channel_match_task( self.state.set_channel_match_task(
channel_id, members_min, weekday, hour, not cancel) channel_id, members_min, weekday, hour)
# Let the user know what happened # Let the user know what happened
if not cancel:
logger.info("Scheduled new match task in %s with min %s weekday %s hour %s", logger.info("Scheduled new match task in %s with min %s weekday %s hour %s",
channel_id, members_min, weekday, hour) channel_id, members_min, weekday, hour)
next_run = util.get_next_datetime(weekday, hour) next_run = util.get_next_datetime(weekday, hour)
date_str = util.datetime_as_discord_time(next_run) date_str = util.datetime_as_discord_time(next_run)
await interaction.response.send_message( await interaction.response.send_message(
f"Done :) Next run will be on {date_str}\n" f"Done :) Next run will be at {date_str}",
+ "Cancel this by re-sending the command with cancel=True",
ephemeral=True, silent=True) ephemeral=True, silent=True)
elif success: @app_commands.command(description="Cancel all scheduled matches in this channel")
logger.info("Removed task in %s on weekday %s hour %s", @commands.guild_only()
channel_id, weekday, hour) async def cancel(self, interaction: discord.Interaction):
await interaction.response.send_message( """Cancel scheduled matches in this channel"""
f"Done :) Schedule on day {weekday} and hour {hour} removed!", ephemeral=True, silent=True) # Bail if not a matcher
if not self.state.get_user_has_scope(interaction.user.id, AuthScope.MATCHER):
await interaction.response.send_message("You'll need the 'matcher' scope to remove scheduled matches",
ephemeral=True, silent=True)
return
# Add the scheduled task and save
channel_id = str(interaction.channel.id)
self.state.remove_channel_match_tasks(channel_id)
else:
await interaction.response.send_message( await interaction.response.send_message(
f"No schedule for this channel on day {weekday} and hour {hour} found :(", ephemeral=True, silent=True) "Done, all scheduled matches cleared in this channel!",
ephemeral=True, silent=True)
@app_commands.command(description="Match up matchees") @app_commands.command(description="Match up matchees")
@commands.guild_only() @commands.guild_only()

View file

@ -338,7 +338,7 @@ class State():
yield (task[_Key.WEEKDAY], task[_Key.HOUR], task[_Key.MEMBERS_MIN]) yield (task[_Key.WEEKDAY], task[_Key.HOUR], task[_Key.MEMBERS_MIN])
@safe_write @safe_write
def set_channel_match_task(self, channel_id: str, members_min: int, weekday: int, hour: int, set: bool) -> bool: def set_channel_match_task(self, channel_id: str, members_min: int, weekday: int, hour: int):
"""Set up a match task on a channel""" """Set up a match task on a channel"""
channel = self._tasks.setdefault(str(channel_id), {}) channel = self._tasks.setdefault(str(channel_id), {})
matches = channel.setdefault(_Key.MATCH_TASKS, []) matches = channel.setdefault(_Key.MATCH_TASKS, [])
@ -348,26 +348,24 @@ class State():
# Specifically check for the combination of weekday and hour # Specifically check for the combination of weekday and hour
if match[_Key.WEEKDAY] == weekday and match[_Key.HOUR] == hour: if match[_Key.WEEKDAY] == weekday and match[_Key.HOUR] == hour:
found = True found = True
if set:
match[_Key.MEMBERS_MIN] = members_min match[_Key.MEMBERS_MIN] = members_min
else:
matches.remove(match)
# Return true as we've successfully changed the data in place # Return true as we've successfully changed the data in place
return True return True
# If we didn't find it, add it to the schedule # If we didn't find it, add it to the schedule
if not found and set: if not found:
matches.append({ matches.append({
_Key.MEMBERS_MIN: members_min, _Key.MEMBERS_MIN: members_min,
_Key.WEEKDAY: weekday, _Key.WEEKDAY: weekday,
_Key.HOUR: hour, _Key.HOUR: hour,
}) })
return True @safe_write
def remove_channel_match_tasks(self, channel_id: str):
# We did not manage to remove the schedule (or add it? though that should be impossible) """Simply delete the match tasks list"""
return False channel = self._tasks.setdefault(str(channel_id), {})
if _Key.MATCH_TASKS in channel:
del channel[_Key.MATCH_TASKS]
@property @property
def _users(self) -> dict[str]: def _users(self) -> dict[str]: