Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed previous message deletion logic #123

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ When creating the schedule Snorlax will check that it has the correct permission

![CloseAndOpen](/screenshots/CloseAndOpen.png)

**Note**: If there is no activity in the channel since the last opening then the bot will self-tidy the opening and close messages in the channel to avoid clutter.
**Note**: The bot self-tidies the messages, i.e. when an opening occurs it will remove the previous close message and vice versa.

### Roles Not Affected By Schedule

Expand Down Expand Up @@ -311,6 +311,9 @@ This can be done with the commands:

**Manual opening and closing only works on channels with an active schedule!**

**Warning**: Manual opening and closing may leave residual open and close messages if a channel has multiple schedules.
This will be fixed in a future update.

### Schedule Global Settings

The following schedules settings apply to all schedules created on the server:
Expand Down
37 changes: 24 additions & 13 deletions cogs/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ async def manualClose(
return

# Grab the schedule row
# TODO: Poor logic when multiple schedules on a single channel
row = schedule_db[schedule_db['channel'] == channel.id].iloc[0]

guild_db = await snorlax_db.load_guild_db()
Expand Down Expand Up @@ -1297,21 +1298,19 @@ async def close_channel(
time_format_fill
)

if not silent:
# Check for the previous messages to see if no one has said anything since the last open.
# If the last two messages are the previous open and close messages then these are removed
# to avoid clutter in the channel.
messages = [message async for message in channel.history(limit=2)]
messages_ids = [message.id for message in messages]

last_close_message = await snorlax_db.get_schedule_last_close_message(rowid)
last_open_message = await snorlax_db.get_schedule_last_open_message(rowid)
last_open_message = await snorlax_db.get_schedule_last_open_message(rowid)

if last_close_message in messages_ids and last_open_message in messages_ids:
logger.info(f"Removing previous open and close messages for schedule: {rowid}.")
for message in messages:
await message.delete()
# Remove the previous close message if present.
if last_open_message is not None:
try:
last_open_message = await channel.fetch_message(last_open_message)
except Exception as e:
logger.warning(f"Last open message not found, skipping deletion (error: {e}).")
else:
logger.info(f"Deleting previous open message in {channel.name} in {channel.guild.name}.")
await last_open_message.delete()

if not silent:
# Send the new one
close_message = await channel.send(embed=close_embed)

Expand Down Expand Up @@ -1397,6 +1396,18 @@ async def open_channel(
time_format_fill
)

last_close_message = await snorlax_db.get_schedule_last_close_message(rowid)

# Remove the previous close message if present.
if last_close_message is not None:
try:
last_close_message = await channel.fetch_message(last_close_message)
except Exception as e:
logger.warning(f"Last close message not found, skipping deletion (error: {e}).")
else:
logger.info(f"Deleting previous close message in {channel.name} in {channel.guild.name}.")
await last_close_message.delete()

if not silent:
open_message = await channel.send(embed=open_embed)
logger.debug(f"Updating last open message for schedule {rowid} to {open_message.id}.")
Expand Down