Added Message Rate limits

pull/36/head
Trivernis 6 years ago
parent c2f3ae08a1
commit 5444089261

@ -22,6 +22,7 @@ class Bot {
this.maindb = null; this.maindb = null;
this.presences = []; this.presences = [];
this.guildHandlers = []; this.guildHandlers = [];
this.userRates = {};
logger.verbose('Verifying config'); logger.verbose('Verifying config');
@ -294,13 +295,24 @@ class Bot {
logger.verbose(`ME: ${msg.content}`); logger.verbose(`ME: ${msg.content}`);
return; return;
} }
logger.verbose(`<${msg.author.tag}>: ${msg.content}`); if (this.checkRate(msg.author.tag)) {
if (!msg.guild) { logger.verbose(`<${msg.author.tag}>: ${msg.content}`);
let reply = cmd.parseMessage(msg); if (!msg.guild) {
this.answerMessage(msg, reply); let reply = cmd.parseMessage(msg);
} else { await this.answerMessage(msg, reply);
let gh = await this.getGuildHandler(msg.guild, prefix); } else {
await gh.handleMessage(msg); let gh = await this.getGuildHandler(msg.guild, prefix);
await gh.handleMessage(msg);
}
if (((Date.now() - this.userRates[msg.author.tag].last)/1000) > (config.rateLimitTime || 10))
this.userRates[msg.author.tag].count = 0;
else
this.userRates[msg.author.tag].count++;
this.userRates[msg.author.tag].last = Date.now();
this.userRates[msg.author.tag].reached = false;
} else if (!this.userRates[msg.author.tag].reached) {
logger.verbose(`${msg.author.tag} reached it's rate limit.`);
this.userRates[msg.author.tag].reached = true;
} }
} catch (err) { } catch (err) {
logger.error(err.message); logger.error(err.message);
@ -320,6 +332,18 @@ class Bot {
}); });
} }
/**
* Returns true if the user has not reached it's rate limit.
* @param usertag
* @returns {boolean}
*/
checkRate(usertag) {
if (!this.userRates[usertag])
this.userRates[usertag] = {last: Date.now(), count: 0};
return ((Date.now() - this.userRates[usertag].last)/1000) > (config.rateLimitTime || 10) ||
this.userRates[usertag].count < (config.rateLimitCount || 5);
}
/** /**
* Sends the answer recieved from the commands callback. * Sends the answer recieved from the commands callback.
* Handles the sending differently depending on the type of the callback return * Handles the sending differently depending on the type of the callback return

@ -12,7 +12,8 @@
"description": "Saves a sequence of commands under a new name. ~save [cmdsequence] [cmdname]. Semicoli must be escaped with \\ (Backslash)", "description": "Saves a sequence of commands under a new name. ~save [cmdsequence] [cmdname]. Semicoli must be escaped with \\ (Backslash)",
"category": "Utility", "category": "Utility",
"response": { "response": {
"no_recursion": "You are **not** allowed to execute another saved command in this sequence. This is a safety measure to avoid endlessly recursive calls." "no_recursion": "You are **not** allowed to execute another saved command in this sequence. This is a safety measure to avoid endlessly recursive calls.",
"sequence_too_long": "This command sequence is too long!"
} }
}, },
"savedcmd": { "savedcmd": {

@ -155,7 +155,7 @@ exports.Servant = class {
let commands = content.split(/(?<!\\);/).map(x => x.replace(/^ +/, '')); let commands = content.split(/(?<!\\);/).map(x => x.replace(/^ +/, ''));
if (commands.length === 1) { if (commands.length === 1) {
return this.processCommand(msg, globResult, content); return this.processCommand(msg, globResult, content);
} else { } else if (commands.length < (config.maxCmdSequenceLength || 5)) {
let answers = []; let answers = [];
let previousCommand = (commands[0].match(/^.\w+/) || [])[0];; let previousCommand = (commands[0].match(/^.\w+/) || [])[0];;
for (let i = 0; i < commands.length; i++) { for (let i = 0; i < commands.length; i++) {
@ -166,6 +166,8 @@ exports.Servant = class {
} }
return answers; return answers;
} else {
return 'This command sequence is too long!';
} }
} }
@ -272,7 +274,7 @@ function parseGlobalCommand(msg) {
return false; return false;
logger.debug(`Permission <${cmd.role}> granted for command ${command} for user <${msg.author.tag}>`); logger.debug(`Permission <${cmd.role}> granted for command ${command} for user <${msg.author.tag}>`);
return processCommand(cmd, msg, content); return processCommand(cmd, msg, content);
} else { } else if (commands.length < (config.maxCmdSequenceLength || 5)) {
let answers = []; let answers = [];
let previousCommand = ''; let previousCommand = '';
for (let commandPart of commands) { for (let commandPart of commands) {
@ -296,6 +298,8 @@ function parseGlobalCommand(msg) {
} }
} }
return answers; return answers;
} else {
return 'This command sequence is too long!';
} }
} }

@ -304,16 +304,18 @@ exports.GuildHandler = class {
// savecmd - saves a command sequence with a name // savecmd - saves a command sequence with a name
this.servant.createCommand(servercmd.utils.savecmd, async (msg, kwargs, argv) => { this.servant.createCommand(servercmd.utils.savecmd, async (msg, kwargs, argv) => {
let saveName = argv.pop(); let saveName = argv.pop();
let cmdsequence = argv.join(' ').replace(/\\/g, '');
if (argv.includes(this.prefix + servercmd.utils.execute.name)) { if (argv.includes(this.prefix + servercmd.utils.execute.name)) {
return servercmd.utils.savecmd.response.no_recursion; return servercmd.utils.savecmd.response.no_recursion;
} else { } else if (cmdsequence.split(';').length < (config.maxCmdSequenceLength || 5)){
let cmdsequence = argv.join(' ').replace(/\\/g, '');
let row = await this.db.get('SELECT COUNT(*) count FROM commands WHERE name = ?', [saveName]); let row = await this.db.get('SELECT COUNT(*) count FROM commands WHERE name = ?', [saveName]);
if (!row || row.count === 0) if (!row || row.count === 0)
await this.db.run('INSERT INTO commands (name, command) VALUES (?, ?)', [saveName, cmdsequence]); await this.db.run('INSERT INTO commands (name, command) VALUES (?, ?)', [saveName, cmdsequence]);
else else
await this.db.run('UPDATE commands SET sequence = ? WHERE name = ?', [cmdsequence, saveName]); await this.db.run('UPDATE commands SET sequence = ? WHERE name = ?', [cmdsequence, saveName]);
return `saved command sequence as ${saveName}`; return `saved command sequence as ${saveName}`;
} else {
return servercmd.utils.savecmd.response.sequence_too_long;
} }
}); });

Loading…
Cancel
Save