Skip to content

Commit

Permalink
Refactor 'crosspost_cmp()' helper function in 'anti_crosspost' extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Mega-JC committed Jul 26, 2024
1 parent ee17a7d commit 009ced2
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pcbot/exts/anti_crosspost.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ def crosspost_cmp(message: discord.Message, other: discord.Message) -> bool:
bool: True if the messages are similar enough to be considered
duplicates, otherwise False.
"""
if not message.content or not other.content:
return False

hamming_score = sum(x != y for x, y in zip(message.content, other.content)) / max(
len(message.content), len(other.content)
)
similarity_score = 0.0
matching_attachments = False
if message.content and other.content:
hamming_score = sum(
x != y for x, y in zip(message.content, other.content)
) / max(len(message.content), len(other.content))
similarity_score = min(max(0, 1 - hamming_score), 1)

elif message.attachments and other.attachments:
matching_attachments = all(
att1.filename == att2.filename and att1.size == att2.size
for att1, att2 in zip(
sorted(message.attachments, key=lambda x: (x.filename, x.size)),
sorted(other.attachments, key=lambda x: (x.filename, x.size)),
)
)

return hamming_score < 0.20 or any(
att1.filename == att2.filename and att1.size == att2.size
for att1, att2 in zip(message.attachments, other.attachments)
)
return similarity_score > 0.80 or matching_attachments


class UserCrosspostCache(TypedDict):
Expand Down

0 comments on commit 009ced2

Please sign in to comment.