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

Edit ephemeral rather than resend messages #225

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 5 additions & 24 deletions cogs/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import contextlib
import logging
from logging import Logger
from typing import Final

import discord
from discord.ui import View
Expand All @@ -22,31 +21,14 @@
class ConfirmKillView(View):
"""A discord.View containing two buttons to confirm shutting down TeX-Bot."""

@classmethod
async def _delete_message(cls, response: discord.InteractionResponse) -> None:
message_not_found_error: discord.NotFound
try:
await response.edit_message(delete_after=0)
except discord.NotFound as message_not_found_error:
MESSAGE_WAS_ALREADY_DELETED: Final[bool] = (
message_not_found_error.code == 10008
or (
"unknown" in message_not_found_error.text.lower()
and "message" in message_not_found_error.text.lower()
)
)
if not MESSAGE_WAS_ALREADY_DELETED:
raise message_not_found_error from message_not_found_error

@discord.ui.button( # type: ignore[misc]
label="SHUTDOWN",
style=discord.ButtonStyle.red,
custom_id="shutdown_confirm",
)
async def confirm_shutdown_button_callback(self, _: discord.Button, interaction: discord.Interaction) -> None: # noqa: E501
"""When the shutdown button is pressed, delete the message."""
# noinspection PyTypeChecker
await self._delete_message(interaction.response)
logger.debug("Confirm button presseed.\n%s", interaction)
MattyTheHacker marked this conversation as resolved.
Show resolved Hide resolved

@discord.ui.button( # type: ignore[misc]
label="CANCEL",
Expand All @@ -55,8 +37,7 @@ async def confirm_shutdown_button_callback(self, _: discord.Button, interaction:
)
async def cancel_shutdown_button_callback(self, _: discord.Button, interaction: discord.Interaction) -> None: # noqa: E501
"""When the cancel button is pressed, delete the message."""
# noinspection PyTypeChecker
await self._delete_message(interaction.response)
logger.debug("Cancel button pressed.\n%s", interaction)
MattyTheHacker marked this conversation as resolved.
Show resolved Hide resolved


class KillCommandCog(TeXBotBaseCog):
Expand Down Expand Up @@ -107,17 +88,17 @@ async def kill(self, ctx: TeXBotApplicationContext) -> None:
),
)

await confirmation_message.delete()
await confirmation_message.edit(view=None)

if button_interaction.data["custom_id"] == "shutdown_confirm": # type: ignore[index, typeddict-item]
await button_interaction.respond(
await confirmation_message.edit(
content="My battery is low and it's getting dark...",
)
await self.bot.perform_kill_and_close(initiated_by_user=ctx.interaction.user)
return

if button_interaction.data["custom_id"] == "shutdown_cancel": # type: ignore[index, typeddict-item]
await button_interaction.respond(
await confirmation_message.edit(
content="Shutdown has been cancelled.",
)
logger.info("Manual shutdown cancelled by %s.", ctx.interaction.user)
Expand Down
42 changes: 19 additions & 23 deletions cogs/strike.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async def yes_strike_member_button_callback(self, _: discord.Button, interaction
The actual handling of the event is done by the command that sent the view,
so all that is required is to delete the original message that sent this view.
"""
await interaction.response.edit_message(delete_after=0)
await interaction.response.edit_message(view=None)

@discord.ui.button( # type: ignore[misc]
label="No",
Expand All @@ -118,7 +118,7 @@ async def no_strike_member_button_callback(self, _: discord.Button, interaction:
The actual handling of the event is done by the command that sent the view,
so all that is required is to delete the original message that sent this view.
"""
await interaction.response.edit_message(delete_after=0)
await interaction.response.edit_message(view=None)


class ConfirmManualModerationView(View):
Expand All @@ -139,7 +139,7 @@ async def yes_manual_moderation_action_button_callback(self, _: discord.Button,
the manual moderation tracker subroutine that sent the view,
so all that is required is to delete the original message that sent this view.
"""
await interaction.response.edit_message(delete_after=0)
await interaction.response.edit_message(view=None)

@discord.ui.button( # type: ignore[misc]
label="No",
Expand All @@ -156,7 +156,7 @@ async def no_manual_moderation_action_button_callback(self, _: discord.Button, i
the manual moderation tracker subroutine that sent the view,
so all that is required is to delete the original message that sent this view.
"""
await interaction.response.edit_message(delete_after=0)
await interaction.response.edit_message(view=None)


class ConfirmStrikesOutOfSyncWithBanView(View):
Expand All @@ -177,7 +177,7 @@ async def yes_out_of_sync_ban_member_button_callback(self, _: discord.Button, in
the manual moderation tracker subroutine that sent the view,
so all that is required is to delete the original message that sent this view.
"""
await interaction.response.edit_message(delete_after=0)
await interaction.response.edit_message(view=None)

@discord.ui.button( # type: ignore[misc]
label="No",
Expand All @@ -194,7 +194,7 @@ async def no_out_of_sync_ban_member_button_callback(self, _: discord.Button, int
the manual moderation tracker subroutine that sent the view,
so all that is required is to delete the original message that sent this view.
"""
await interaction.response.edit_message(delete_after=0)
await interaction.response.edit_message(view=None)


class BaseStrikeCog(TeXBotBaseCog):
Expand Down Expand Up @@ -262,13 +262,12 @@ async def _confirm_perform_moderation_action(self, message_sender_component: Mes
)

if button_interaction.data["custom_id"] == "no_strike_member": # type: ignore[index, typeddict-item]
await button_interaction.respond(
(
await button_interaction.edit_original_response(
content=(
"Aborted performing "
f"{self.SUGGESTED_ACTIONS[actual_strike_amount]} action "
f"on {strike_user.mention}."
),
ephemeral=True,
)
return

Expand All @@ -279,12 +278,11 @@ async def _confirm_perform_moderation_action(self, message_sender_component: Mes
committee_member=interaction_user,
)

await button_interaction.respond(
(
await button_interaction.edit_original_response(
content=(
f"Successfully performed {self.SUGGESTED_ACTIONS[actual_strike_amount]} "
f"action on {strike_user.mention}."
),
ephemeral=True,
)
return

Expand Down Expand Up @@ -548,9 +546,8 @@ async def _confirm_manual_add_strike(self, strike_user: discord.User | discord.M
)

if out_of_sync_ban_button_interaction.data["custom_id"] == "no_out_of_sync_ban_member": # type: ignore[index, typeddict-item] # noqa: E501
await out_of_sync_ban_confirmation_message.delete()
aborted_out_of_sync_ban_message: discord.Message = await confirmation_message_channel.send( # noqa: E501
f"Aborted performing ban action upon {strike_user.mention}. "
await out_of_sync_ban_confirmation_message.edit(
content=f"Aborted performing ban action upon {strike_user.mention}. "
"(This manual moderation action has not been tracked.)\n"
"ᴛʜɪs ᴍᴇssᴀɢᴇ ᴡɪʟʟ ʙᴇ ᴅᴇʟᴇᴛᴇᴅ"
f"""{
Expand All @@ -561,7 +558,7 @@ async def _confirm_manual_add_strike(self, strike_user: discord.User | discord.M
}""",
)
await asyncio.sleep(118)
await aborted_out_of_sync_ban_message.delete()
await out_of_sync_ban_confirmation_message.delete()
return

if out_of_sync_ban_button_interaction.data["custom_id"] == "yes_out_of_sync_ban_member": # type: ignore[index, typeddict-item] # noqa: E501
Expand All @@ -573,8 +570,8 @@ async def _confirm_manual_add_strike(self, strike_user: discord.User | discord.M
"with number of strikes**"
),
)
success_out_of_sync_ban_message: discord.Message = await confirmation_message_channel.send( # noqa: E501
f"Successfully banned {strike_user.mention}.\n"
await out_of_sync_ban_confirmation_message.edit(
content=f"Successfully banned {strike_user.mention}.\n"
"**Please ensure you use the `/strike` command in future!**"
"\nᴛʜɪs ᴍᴇssᴀɢᴇ ᴡɪʟʟ ʙᴇ ᴅᴇʟᴇᴛᴇᴅ"
f"""{
Expand All @@ -585,7 +582,7 @@ async def _confirm_manual_add_strike(self, strike_user: discord.User | discord.M
}""",
)
await asyncio.sleep(118)
await success_out_of_sync_ban_message.delete()
await out_of_sync_ban_confirmation_message.delete()
return

raise ValueError
Expand Down Expand Up @@ -631,9 +628,8 @@ async def _confirm_manual_add_strike(self, strike_user: discord.User | discord.M
)

if button_interaction.data["custom_id"] == "no_manual_moderation_action": # type: ignore[index, typeddict-item]
await confirmation_message.delete()
aborted_strike_message: discord.Message = await confirmation_message_channel.send(
f"Aborted increasing {strike_user.mention}'s strikes "
await confirmation_message.edit(
content=f"Aborted increasing {strike_user.mention}'s strikes "
"& sending moderation alert message. "
"(This manual moderation action has not been tracked.)\n"
"ᴛʜɪs ᴍᴇssᴀɢᴇ ᴡɪʟʟ ʙᴇ ᴅᴇʟᴇᴛᴇᴅ"
Expand All @@ -645,7 +641,7 @@ async def _confirm_manual_add_strike(self, strike_user: discord.User | discord.M
}""",
)
await asyncio.sleep(118)
await aborted_strike_message.delete()
await confirmation_message.delete()
return

if button_interaction.data["custom_id"] == "yes_manual_moderation_action": # type: ignore[index, typeddict-item]
Expand Down