Skip to content

Commit

Permalink
Refactor 'crosspost_cmp()' to use better message content comparison f…
Browse files Browse the repository at this point in the history
…unction, split 'message_length_threshold' into same and cross-channel cases
  • Loading branch information
Mega-JC committed Jul 28, 2024
1 parent 60c3bd3 commit f1716d7
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions pcbot/exts/anti_crosspost.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ async def on_message(self, message: discord.Message):
):
return

logger.debug(f"Received noteworthy message from {message.author.name}: {message.jump_url}")
logger.debug(
f"Received noteworthy message from {message.author.name}: {message.jump_url}"
)

# Attempt to enforce the cache size limit
for user_id in list(self.crossposting_cache.keys()):
Expand All @@ -216,18 +218,21 @@ async def on_message(self, message: discord.Message):
# Check for crossposts or duplicates in existing message groups
for messages in user_cache["message_groups"]:
for existing_message in messages:
if (
message.channel.id == existing_message.channel.id
and len(message.content)
< self.same_channel_message_length_threshold
) or (
message.channel.id != existing_message.channel.id
and len(message.content)
< self.cross_channel_message_length_threshold
if message.content and (
(
message.channel.id == existing_message.channel.id
and len(message.content)
< self.same_channel_message_length_threshold
)
or (
message.channel.id != existing_message.channel.id
and len(message.content)
< self.cross_channel_message_length_threshold
)
):
# enforce same-channel and cross-channel message length thresholds in order for them to be considered crossposts
continue

elif (
await crosspost_cmp(message, existing_message)
and message.created_at.timestamp()
Expand All @@ -239,18 +244,20 @@ async def on_message(self, message: discord.Message):
f"Found crosspost for user {message.author.name}, message URL {message.jump_url}!!!!!!!!!!"
)

# try:
# alert_message = await message.reply(
# "This message is a recent crosspost/duplicate among the following messages: "
# + ", ".join([m.jump_url for m in messages])
# + ".\n\nPlease delete all duplicate messages."
# )
# user_cache["message_to_alert"][
# message.id
# ] = alert_message.id
# logger.debug(f"Sent alert message for crosspost URL {message.jump_url}")
# except discord.HTTPException as e:
# logger.debug(f"Failed to send alert message: {e}")
try:
alert_message = await message.reply(
"This message is a recent crosspost/duplicate among the following messages: "
+ ", ".join([m.jump_url for m in messages])
+ ".\n\nPlease delete all duplicate messages."
)
user_cache["message_to_alert"][
message.id
] = alert_message.id
logger.debug(
f"Sent alert message for crosspost URL {message.jump_url}"
)
except discord.HTTPException as e:
logger.debug(f"Failed to send alert message: {e}")
break
else:
continue
Expand Down

0 comments on commit f1716d7

Please sign in to comment.