Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

feat: log removed messages to admin channel #793

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/database/connectDb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs';
import { connect } from 'mongoose';
import { ExtendedClient } from '../interfaces/ExtendedClient';
import { errorHandler } from '../utils/errorHandler';
Expand Down
12 changes: 10 additions & 2 deletions src/events/onMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBuilder, Message } from 'discord.js';
import { EmbedBuilder, Message, TextChannel } from 'discord.js';
import { checkContent } from '../alexjs/checkContent';
import { checkBannedWords } from '../alexjs/checkBannedWords';
import { stripSpecialCharacters } from '../alexjs/stripSpecialCharacters';
Expand All @@ -15,7 +15,15 @@
return;
}

await checkLinks(bot, message);
const linkMessage = await checkLinks(bot, message);
if (linkMessage) {
const adminChannel = bot.channels.cache.get(
process.env.ADMIN_CHANNEL!,

Check warning on line 21 in src/events/onMessage.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion

Check warning on line 21 in src/events/onMessage.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
) as TextChannel;
await adminChannel.send({
embeds: [linkMessage],
});
}

const cleaned = await sentenceTypoFixer(
stripSpecialCharacters(message.content),
Expand Down
18 changes: 12 additions & 6 deletions src/links/checkLinks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, PartialMessage } from 'discord.js';
import { EmbedBuilder, Message, PartialMessage } from 'discord.js';

import { ExtendedClient } from '../interfaces/ExtendedClient';
import { errorHandler } from '../utils/errorHandler';
Expand All @@ -9,26 +9,32 @@ const urlPattern = /(http|https):\/\/([\w-]+(\.[\w-]+)+)(\/[\w-./?%&=]*)?/;
export const checkLinks = async (
bot: ExtendedClient,
message: Message | PartialMessage,
): Promise<void> => {
): Promise<EmbedBuilder | null> => {
const content = message.content;

try {
const urlMatch = content?.match(urlPattern);
if (!urlMatch) {
return;
return null;
}

if (urlMatch) {
if (allowedLinks.every((link) => urlMatch[0].includes(link))) {
return;
return null;
}
}

await message.delete();

return;
const embed = new EmbedBuilder();
embed.setTitle(
`The user "${message.author?.username}" message contains a link`,
);
embed.setDescription(message.content);

return embed;
} catch (error) {
await errorHandler(bot, error, 'link checking');
return;
return null;
}
};
Loading