multiple changes...

* re-purposed trigger system
* added "ping bomb" banning system
This commit is contained in:
liushuyu 2020-05-24 00:22:06 -06:00
parent 354074089d
commit 5fb0d41c48
3 changed files with 56 additions and 2 deletions

View File

@ -24,5 +24,11 @@ export interface IResponses {
export interface IModule { export interface IModule {
readonly roles?: string[], readonly roles?: string[],
command: (message: Message, args: string) => void | Promise<void> command: (message: Message, args?: string) => void | Promise<void>
}
export interface ITrigger {
readonly roles?: string[],
trigger: (message: Message, args?: string) => boolean
execute: (message: Message, args?: string) => void | Promise<void>
} }

View File

@ -9,14 +9,16 @@ import fs = require('fs');
import logger from './logging'; import logger from './logging';
import state from './state'; import state from './state';
import * as data from './data'; import * as data from './data';
import { IModule, ITrigger } from './models/interfaces';
state.responses = require('./responses.json'); state.responses = require('./responses.json');
interface IModuleMap { interface IModuleMap {
[name: string]: any; [name: string]: IModule;
} }
let cachedModules: IModuleMap = {}; let cachedModules: IModuleMap = {};
let cachedTriggers: ITrigger[] = [];
const client = new discord.Client(); const client = new discord.Client();
const rulesTrigger = process.env.DISCORD_RULES_TRIGGER; const rulesTrigger = process.env.DISCORD_RULES_TRIGGER;
const rluesRole = process.env.DISCORD_RULES_ROLE; const rluesRole = process.env.DISCORD_RULES_ROLE;
@ -198,6 +200,18 @@ client.on('message', message => {
} }
} catch (err) { logger.error(err); } } catch (err) { logger.error(err); }
} else if (message.author.bot === false) {
// This is a normal channel message.
cachedTriggers.forEach(function (trigger) {
if (!trigger.roles || authorRoles && findArray(authorRoles, trigger.roles)) {
if (trigger.trigger(message) === true) {
logger.debug(`${message.author.username} ${message.author} [Channel: ${message.channel}] triggered: ${message.content}`);
try {
trigger.execute(message);
} catch (err) { logger.error(err); }
}
}
});
} }
}); });
@ -216,6 +230,25 @@ fs.readdirSync('./commands/').forEach(function (file) {
} }
}); });
// Cache all triggers.
cachedTriggers = [];
fs.readdirSync('./triggers/').forEach(function (file) {
// Load the module if it's a script.
if (path.extname(file) === '.js') {
if (file.includes('.disabled')) {
logger.info(`Did not load disabled trigger: ${file}`);
} else {
const moduleName = path.basename(file, '.js').toLowerCase();
logger.info(`Loaded trigger: ${moduleName} from ${file}`);
try {
cachedTriggers.push(require(`./triggers/${file}`));
} catch (e) {
logger.error(`Could not load trigger ${moduleName}: ${e}`);
}
}
}
});
data.readWarnings(); data.readWarnings();
data.readBans(); data.readBans();

15
src/triggers/pingBomb.ts Normal file
View File

@ -0,0 +1,15 @@
import { ban } from '../common';
import state from '../state';
import logger from '../logging';
import discord = require('discord.js');
export function trigger(message: discord.Message) {
return message.mentions.users.array().length > 10;
}
export function execute(message: discord.Message) {
const count = message.mentions.users.array().length;
logger.info(`${message.author.toString()} tagged ${count} users in ${message.channel.toString()}`);
state.logChannel.send(`Ping bomb detected in ${message.channel.toString()} by ${message.author.toString()}`);
ban(message.author, message.author, message.guild);
};