refraction/index.ts
Ryan Cao d5fcaece31
add a pretty "neat" feature
and remove banned slurs
2022-06-06 00:01:57 +08:00

121 lines
3 KiB
TypeScript

import { Client, Intents } from 'discord.js';
import { commands, aliases } from './commands';
import * as BuildConfig from './constants';
import { isBad } from './badLinks';
import { green, bold, blue, underline, yellow } from 'kleur/colors';
import urlRegex from 'url-regex';
import removeMarkdown from 'remove-markdown';
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_BANS,
],
});
client.login(process.env.DISCORD_TOKEN);
client.once('ready', async () => {
console.log(green('Discord bot ready!'));
if (process.env.NODE_ENV !== 'development')
console.warn(yellow(bold('Running in production mode!')));
console.log(
'Invite link:',
blue(
underline(
client.generateInvite({
scopes: ['bot'],
permissions: ['ADMINISTRATOR'],
})
)
)
);
const POLYMC_GUILD = await client.guilds.fetch(BuildConfig.GUILD_ID);
const DEBUG_CHANNEL = POLYMC_GUILD.channels.cache.get(
BuildConfig.DEBUG_CHANNEL_ID
);
if (!DEBUG_CHANNEL || !DEBUG_CHANNEL.isText()) throw new Error();
DEBUG_CHANNEL.send({
embeds: [
{
title: 'Started!',
description: new Date().toISOString(),
color: 'AQUA',
},
],
});
client.on('messageCreate', async (e) => {
if (!e.content) return;
if (!e.channel.isText()) return;
if (e.author === client.user) return;
if (
process.env.NODE_ENV === 'development' &&
e.channelId !== BuildConfig.DEBUG_CHANNEL_ID
) {
return;
} else if (
process.env.NODE_ENV !== 'development' &&
e.channelId === BuildConfig.DEBUG_CHANNEL_ID
) {
return;
}
// phishing link filter
{
const urlMatches = [...e.content.matchAll(urlRegex())];
if (urlMatches.length) {
console.log('Found links in message from', e.author.tag);
for (const match of urlMatches) {
console.log('[link]', match[0]);
if (await isBad(match[0])) {
await e.reply({
embeds: [
{
title: 'Hold on!',
description:
'There seems to be a phishing / malware link in your message.',
color: 'RED',
},
],
});
return;
}
}
}
}
// neat
{
const cleanContent = removeMarkdown(e.content).toLowerCase();
if (cleanContent.split(' ').includes('neat')) {
console.log('[neat]', cleanContent);
await e.reply('Neat is a mod by Vazkii.');
}
}
const cmd = e.content.split(' ')[0];
if (!cmd.startsWith('!')) return;
let func = commands[cmd];
func = func ?? commands[aliases[cmd]];
if (func !== undefined) {
await func(client, e);
}
});
});