discord-addon.js
Version:
A addon for a discord bot made in discord.js
126 lines (117 loc) • 4.73 kB
JavaScript
const authors = [];
var warned = [];
var banned = [];
var messagelog = [];
/**
*
* @typedef {Object} Options
* @property {Number} [warnBuffer=3] Maximum amount of messages allowed to send in the interval time before getting warned.
* @property {Number} [maxBuffer=5] Maximum amount of messages allowed to send in the interval time before getting banned.
* @property {Number} [interval=1000] Amount of time in ms users can send a maximum of the maxBuffer variable before getting banned.
* @property {String} [warningMessage="stop spamming"] Warning message send to the user indicating they are going to fast.
* @property {String} [banMessage="has been banned for spamming, anyone else?"] Ban message, always tags the banned user in front of it.
* @property {Number} [maxDuplicatesWarning=7] Maximum amount of duplicate messages a user can send in a timespan before getting warned
* @property {Number} [maxDuplicatesBan=10] Maximum amount of duplicate messages a user can send in a timespan before getting banned
* @property {Number} [softban=false] After a ban, the user gets an unban if this option is true
*
*
*/
/**
*
* @param {Discord.Client} client The client object
* @param {Options} [options] The options that you can set
*
*/
module.exports = async (client, options = {}) => {
const warnBuffer = (options && options.prefix) || 3;
const maxBuffer = (options && options.prefix) || 5;
const interval = (options && options.interval) || 1000;
const warningMessage = (options && options.warningMessage) || "stop spamming...";
const banMessage = (options && options.banMessage) || "has been banned for spamming, anyone else?";
const maxDuplicatesWarning = (options && options.duplicates || 7);
const maxDuplicatesBan = (options && options.duplicates || 10);
const softbanAfterSpam = (options && options.softban || false)
client.on('message', msg => {
if (msg.author.bot) return;
var now = Math.floor(Date.now());
authors.push({
"time": now,
"author": msg.author.id
});
messagelog.push({
"message": msg.content,
"author": msg.author.id
});
var msgMatch = 0;
for (var i = 0; i < messagelog.length; i++) {
if (messagelog[i].message == msg.content && (messagelog[i].author == msg.author.id) && (msg.author.id !== client.user.id)) {
msgMatch++;
}
}
// Check matched count
if (msgMatch == maxDuplicatesWarning && !warned.includes(msg.author.id)) {
warn(msg, msg.author.id);
}
if (msgMatch == maxDuplicatesBan && !banned.includes(msg.author.id)) {
ban(msg, msg.author.id);
}
let matched = 0;
for (var i = 0; i < authors.length; i++) {
if (authors[i].time > now - interval) {
matched++;
if (matched == warnBuffer && !warned.includes(msg.author.id)) {
warn(msg, msg.author.id, warningMessage);
}
else if (matched == maxBuffer) {
if (!banned.includes(msg.author.id)) {
ban(msg, msg.author.id, banMessage);
if (softbanAfterSpam == true) {
msg.guild.unban(msg.author.id);
}
}
}
}
else if (authors[i].time < now - interval) {
authors.splice(i);
warned.splice(warned.indexOf(authors[i]));
banned.splice(warned.indexOf(authors[i]));
}
if (messagelog.length >= 200) {
messagelog.shift();
}
}
});
}
/**
* Warn a user
* @param {Object} msg
* @param {string} userid userid
*/
function warn(msg, userid, warningMessage) {
warned.push(msg.author.id);
msg.channel.send(msg.author + " " + warningMessage);
}
/**
* Ban a user by the user id
* @param {Object} msg
* @param {string} userid userid
* @return {boolean} True or False
*/
function ban(msg, userid, banMessage) {
for (var i = 0; i < messagelog.length; i++) {
if (messagelog[i].author == msg.author.id) {
messagelog.splice(i);
}
}
banned.push(msg.author.id);
var user = msg.channel.guild.members.get(userid);
if (user) {
user.ban().then((member) => {
msg.channel.send(msg.author + " " + banMessage);
return true;
}).catch(() => {
msg.channel.send("Insufficient permission to ban " + msg.author + " for spamming.");
return false;
});
}
}