Skip to content

Commit

Permalink
Merge pull request #40 from EvilBabyDemon/main
Browse files Browse the repository at this point in the history
remove message if author edits and removes tracking
  • Loading branch information
jackra1n authored Sep 26, 2024
2 parents c2bfcff + f84510e commit e015f76
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions extensions/url_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from utils.url_rules import DEFAULT_RULES

logger = logging.getLogger(__name__)

# Save messages with tracking parameters to delete if user removes them
save_message = {}

class _URLCleaner:
def __init__(self, rules: list[str]):
Expand Down Expand Up @@ -140,7 +141,20 @@ async def on_message(self, message: discord.Message):
cleaned_urls_str = "\n".join(cleaned_urls)
response += f"\n Here's the link without trackers:\n{cleaned_urls_str}"
embed.description = response
await message.reply(embed=embed)
reply = await message.reply(embed=embed)
# Save reply to hash table with message id as key
save_message[message.id] = reply

# On edit message event, recheck the message for tracking parameters
@commands.Cog.listener()
async def on_message_edit(self, before: discord.Message, after: discord.Message):
# check if message saved
if after.id in save_message:
#no need to check if enabled as else we would not have saved the message
_, removed_trackers = self.cleaner.clean_message_urls(after.content)
if not removed_trackers:
# delete message
await save_message.pop(after.id).delete()

@commands.hybrid_command()
async def urls_cleaner(self, ctx: commands.Context, enable: bool):
Expand Down

0 comments on commit e015f76

Please sign in to comment.