Skip to content

Instantly share code, notes, and snippets.

@Zwiterrion
Created March 10, 2024 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zwiterrion/3b67fe0880dcfad01214f6a824cf48e3 to your computer and use it in GitHub Desktop.
Save Zwiterrion/3b67fe0880dcfad01214f6a824cf48e3 to your computer and use it in GitHub Desktop.
const saveMessage = (message) => {
const existingMessage = messages[message];
let occurrences = 1;
if (existingMessage) {
occurrences = existingMessage.occurrences + 1;
}
if (Date.now() - messages[message]?.last_update > copyPastaIntervalDetectionTime) {
occurrences = 1;
}
messages[message] = {
occurrences,
last_update: occurrences < maxOccurrences ? Date.now() : messages[message].last_update
}
}
const cleanupOldMessages = () => {
messages = Object.fromEntries(Object.entries(messages)
.filter(entry => {
if (entry[1].occurrences < maxOccurrences) {
return Date.now() - entry[1].last_update < copyPastaIntervalDetectionTime;
} else {
return Date.now() - entry[1].last_update < (copyPastaIntervalDetectionTime + copyPastaPrisonTime);
}
}))
}
function isCopyPasta(rawMessage) {
const formattedMessage = rawMessage.trim();
saveMessage(formattedMessage);
cleanupOldMessages();
return messages[formattedMessage] && messages[formattedMessage].occurrences >= maxOccurrences;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment