-
Notifications
You must be signed in to change notification settings - Fork 3
feat: moderation tool migration into webdev-bot #85
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
Open
michal-skraburski
wants to merge
14
commits into
r-webdev:main
Choose a base branch
from
michal-skraburski:moderation-migrate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b4f364a
mig: add message-cache from moderation-tool and rename both cache fil…
michal-skraburski e2bbe93
feat: spam-detection, see desc
michal-skraburski d25aad9
fix: stray console log rm
michal-skraburski 614653a
mig: add reportMessage feat from moderation tool
michal-skraburski 7867113
feat: move report-spam into report-message
michal-skraburski cc77fca
fix: enable spam-detection & reportMessage
michal-skraburski aa13e3d
Merge branch 'main' into moderation-migrate
michal-skraburski 8c5bdfd
Update .env.test
michal-skraburski c964088
Update .env.production
michal-skraburski c7859d5
fix: rename moderation/cache-messages to features/cache-messages
michal-skraburski 4052f88
fix: remove redudant comments
michal-skraburski 0ea9ab7
Update src/util/cache/channel-prefetch.ts
michal-skraburski d19e559
fix: merge 3 commits, rm redudant comments, rename moderation/cache-m…
michal-skraburski 13fc65d
fix: rename channel-prefetch one level down
michal-skraburski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { createMessageContextMenuCommand } from '@/common/commands/create-commands.js'; | ||
| import { config } from '@/env.js'; | ||
| import { ChannelType, Colors, EmbedBuilder, MessageFlags } from 'discord.js'; | ||
|
|
||
| export const reportMessage = createMessageContextMenuCommand({ | ||
| data: { | ||
| name: 'Report to Moderators', | ||
| }, | ||
| execute: async (interaction) => { | ||
| await interaction.deferReply({ | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
|
|
||
| const guild = interaction.guild; | ||
|
|
||
| if (!guild) { | ||
| await interaction.editReply({ | ||
| content: 'This can only be used in a server.', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const targetMessage = interaction.targetMessage; | ||
| const reporter = interaction.user; | ||
| const channelId = config.channelIds.spamDetection; | ||
| const channel = guild.channels.cache.get(channelId); | ||
|
|
||
| try { | ||
| if (!channel || channel.type !== ChannelType.GuildText) { | ||
| await interaction.editReply({ | ||
| content: 'Moderator channel not found or is not a text channel.', | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const jumpLink = targetMessage.url; | ||
| const authorTag = targetMessage.author.tag ?? 'Unknown'; | ||
| const authorId = targetMessage.author.id ?? 'Unknown'; | ||
|
|
||
| const embed = new EmbedBuilder() | ||
| .setTitle('🚩 Message Report') | ||
| .setColor(Colors.DarkOrange) | ||
| .setTimestamp() | ||
| .setURL(jumpLink) | ||
| .addFields( | ||
| { name: 'Reporter', value: `<@${reporter.id}>`, inline: true }, | ||
| { | ||
| name: 'Message Link', | ||
| value: `[Jump to message](${jumpLink})`, | ||
| inline: true, | ||
| }, | ||
| { name: 'Message ID', value: targetMessage.id, inline: true }, | ||
| { name: 'Username', value: authorTag, inline: true }, | ||
| { name: 'User ID', value: authorId, inline: true }, | ||
| { name: 'Linked User', value: `<@${authorId}>`, inline: true } | ||
| ); | ||
|
|
||
| await channel.send({ embeds: [embed] }); | ||
|
|
||
| await interaction.editReply({ | ||
| content: 'Thanks. The message was reported to moderators.', | ||
| }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| await interaction.editReply({ | ||
| content: 'Failed to report the message.', | ||
| }); | ||
| } | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import type { Channel, Message } from 'discord.js'; | ||
| import { cachedMessages } from '@/util/cache/recent-message-store.js'; | ||
| import { DAY, HOUR } from '../../constants/time.js'; | ||
| import { defaultLogFunction, type LogFunction } from './logs.js'; | ||
| import type { Rule } from './rules-config.js'; | ||
|
|
||
| type ActionConfig = { | ||
| reason: string; | ||
| deleteMessages?: boolean; | ||
| muteDuration?: number; | ||
| log?: LogFunction; | ||
| }; | ||
|
|
||
| const handleBulkDeleteMessages = async (messages: Message[]) => { | ||
| const messagesByChannel = new Map<string, string[]>(); | ||
| for (const message of messages) { | ||
| if (!message.deletable || !message.inGuild()) { | ||
| continue; | ||
| } | ||
| if (!messagesByChannel.has(message.channelId)) { | ||
| messagesByChannel.set(message.channelId, [message.id]); | ||
| } else { | ||
| messagesByChannel.get(message.channelId)!.push(message.id); | ||
| } | ||
| } | ||
|
|
||
| let deletedMessagesCount = 0; | ||
|
|
||
| await Promise.allSettled( | ||
| Array.from(messagesByChannel.entries()).map(([channelId, messageIds]) => { | ||
| const channel = messages.find( | ||
| (message) => message.channelId === channelId | ||
| )?.channel; | ||
| if (!channel || channel.isDMBased()) { | ||
| return Promise.resolve(); | ||
| } | ||
| cachedMessages.bulkDeleteByIds(messageIds); | ||
| deletedMessagesCount += messageIds.length; | ||
| return channel.bulkDelete(messageIds, true); | ||
| }) | ||
| ); | ||
|
|
||
| return deletedMessagesCount; | ||
| }; | ||
|
|
||
| const handleAction = (config: ActionConfig) => { | ||
| return async (messages: Message[], rule: Rule, logChannel?: Channel) => { | ||
| const firstMessage = messages[0]; | ||
| const author = firstMessage.author; | ||
|
|
||
| if (author === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| let muted = false; | ||
| if (config.muteDuration) { | ||
| try { | ||
| const guildMember = await firstMessage.guild?.members.fetch(author.id); | ||
| if (guildMember?.moderatable) { | ||
| await guildMember.timeout(config.muteDuration, config.reason); | ||
| muted = true; | ||
| } | ||
| } catch (error) { | ||
| console.error('Failed to mute user:', error); | ||
| } | ||
| } | ||
|
|
||
| let deletedMessagesCount = 0; | ||
|
|
||
| if (config.deleteMessages) { | ||
| deletedMessagesCount = await handleBulkDeleteMessages(messages); | ||
| } | ||
|
|
||
| // Use custom log function if provided, otherwise use default | ||
| const logFunction = config.log || defaultLogFunction; | ||
| await logFunction({ | ||
| messages, | ||
| reason: config.reason, | ||
| logChannel, | ||
| deletedMessagesCount, | ||
| muteDuration: muted ? config.muteDuration : undefined, | ||
| rule, | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| export const handleBannedTagsAction = handleAction({ | ||
| reason: 'Banned Tag', | ||
| deleteMessages: true, | ||
| muteDuration: 1 * DAY, | ||
| }); | ||
|
|
||
| export const handleDiscordInvitesAction = handleAction({ | ||
| reason: 'Discord Invite Link', | ||
| deleteMessages: true, | ||
| muteDuration: 1 * DAY, | ||
| }); | ||
|
|
||
| export const handleSpoilerHackAction = handleAction({ | ||
| reason: 'Spoiler Tag Hack', | ||
| deleteMessages: true, | ||
| muteDuration: 12 * HOUR, | ||
| }); | ||
|
|
||
| export const handleCrossPostingAction = handleAction({ | ||
| reason: 'Cross-posting', | ||
| deleteMessages: true, | ||
| muteDuration: 12 * HOUR, | ||
| }); | ||
|
|
||
| export const handleHighFrequencyAction = handleAction({ | ||
| reason: 'High Frequency Messaging', | ||
| deleteMessages: true, | ||
| muteDuration: 12 * HOUR, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { rules } from './rules-config.js'; | ||
|
|
||
| export const MAX_RULE_TIMEFRAME = Math.max( | ||
| ...rules | ||
| .filter((rule) => rule.type !== 'contentBased') | ||
| .map((rule) => rule.timeframe) | ||
| ); | ||
|
|
||
| export const MESSAGE_SIMILARITY_THRESHOLD = 0.8; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import type { Message } from 'discord.js'; | ||
| import { | ||
| jaccardSimilarity, | ||
| replaceSpoilerHack, | ||
| stripCode, | ||
| } from '@/util/messages.js'; | ||
| import { MESSAGE_SIMILARITY_THRESHOLD } from './constants.js'; | ||
|
|
||
| export const containsLink = (message: Message): boolean => { | ||
| const withoutCode = stripCode(message.content); | ||
| return withoutCode.includes('http://') || withoutCode.includes('https://'); | ||
| }; | ||
|
|
||
| export const containsBannedTag = (message: Message): boolean => { | ||
| const withoutCode = stripCode(message.content); | ||
| return withoutCode.includes('@everyone') || withoutCode.includes('@here'); | ||
| }; | ||
|
|
||
| export const containsDiscordInvite = (message: Message): boolean => { | ||
| const withoutCode = stripCode(message.content); | ||
| const keywords = [ | ||
| 'discord.gg/', | ||
| 'discord.com/invite/', | ||
| 'discordapp.com/invite/', | ||
| ]; | ||
| return keywords.some((keyword) => withoutCode.includes(keyword)); | ||
| }; | ||
|
|
||
| export const containsSpoilerHack = (message: Message) => { | ||
| const withoutCode = stripCode(message.content); | ||
|
|
||
| return withoutCode !== replaceSpoilerHack(withoutCode, ''); | ||
| }; | ||
|
|
||
| export const isDuplicate = (message: Message, oldMessage: Message) => { | ||
| // cheaper comparison first | ||
| const a = message.content.toLowerCase().trim(); | ||
| const b = oldMessage.content.toLowerCase().trim(); | ||
| if (a === b) { | ||
| return true; | ||
| } | ||
| // followed by jaccard for catching reordered/slightly altered messages with high similarity | ||
| return jaccardSimilarity(a, b) > MESSAGE_SIMILARITY_THRESHOLD; | ||
| }; | ||
|
|
||
| export const isCrossPost = (message: Message, oldMessage: Message) => { | ||
| return ( | ||
| message.channelId !== oldMessage.channelId && | ||
| isDuplicate(message, oldMessage) | ||
| ); | ||
| }; | ||
|
|
||
| export const anyMessage = () => true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { Events } from 'discord.js'; | ||
| import { cachedMessages } from '@/util/cache/recent-message-store.js'; | ||
| import { config } from '@/env.js'; | ||
| import { createEvent } from '@/common/events/create-event.js'; | ||
| import { checkRules } from './rules.js'; | ||
| import { isNormalUserMessage } from '@/util/messages.js'; | ||
|
|
||
| export const spamDetection = createEvent( | ||
| { | ||
| name: Events.MessageCreate, | ||
| }, | ||
| async (message) => { | ||
| if (!isNormalUserMessage(message)) { | ||
| return; | ||
| } | ||
| const regularRole = message.guild?.roles.cache.get(config.roleIds.regular); | ||
| if ( | ||
| regularRole === undefined || | ||
| message.member === null || | ||
| message.member.roles.highest.position >= regularRole.position | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| cachedMessages.add(message); | ||
| await checkRules(message); | ||
| } | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.