Skip to content

Commit

Permalink
Merge pull request #297 from aternosorg/badwords
Browse files Browse the repository at this point in the history
refactor badwords, add priority
  • Loading branch information
JulianVennen authored Jul 2, 2021
2 parents c6cdc33 + 0a0f438 commit 0e21ed8
Show file tree
Hide file tree
Showing 16 changed files with 548 additions and 404 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modbot",
"version": "0.4.0",
"version": "0.5.0",
"description": "Discord Bot for the Aternos Discord server",
"main": "index.js",
"scripts": {
Expand Down
41 changes: 20 additions & 21 deletions src/AutoResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,26 @@ class AutoResponse extends ChatTriggeredFeature {
* @return {AutoResponse} the auto response
*/
constructor(gid, json, id) {
super(id);
this.gid = gid;
super(id, json.trigger);
this.gid = gid;

if (json) {
this.trigger = json.trigger;
this.response = json.response;
this.global = json.global;
this.channels = json.channels;
}
if (json) {
this.response = json.response;
this.global = json.global;
this.channels = json.channels;
}

if (!this.channels) {
this.channels = [];
}
if (!this.channels) {
this.channels = [];
}
}

/**
* serialize the response
* @returns {(*|string)[]}
*/
serialize() {
return [this.gid, JSON.stringify(this.trigger), this.response, this.global, this.channels.join(',')];
return [this.gid, JSON.stringify(this.trigger), this.response, this.global, this.channels.join(',')];
}

/**
Expand All @@ -52,15 +51,15 @@ class AutoResponse extends ChatTriggeredFeature {
* @returns {module:"discord.js".MessageEmbed}
*/
embed(title, color) {
return new Discord.MessageEmbed()
.setTitle(title + ` [${this.id}]`)
.setColor(color)
.addFields(
/** @type {any} */[
{name: "Trigger", value: `${this.trigger.type}: \`${this.trigger.type === 'regex' ? '/' + this.trigger.content + '/' + this.trigger.flags : this.trigger.content}\``.substring(0, 1000)},
{name: "Response", value: this.response.substring(0,1000)},
{name: "Channels", value: this.global ? "global" : this.channels.map(c => `<#${c}>`).join(', ').substring(0, 1000)}
]);
return new Discord.MessageEmbed()
.setTitle(title + ` [${this.id}]`)
.setColor(color)
.addFields(
/** @type {any} */[
{name: 'Trigger', value: `${this.trigger.type}: \`${this.trigger.type === 'regex' ? '/' + this.trigger.content + '/' + this.trigger.flags : this.trigger.content}\``.substring(0, 1000)},
{name: 'Response', value: this.response.substring(0,1000)},
{name: 'Channels', value: this.global ? 'global' : this.channels.map(c => `<#${c}>`).join(', ').substring(0, 1000)}
]);
}

}
Expand Down
246 changes: 178 additions & 68 deletions src/BadWord.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,194 @@
const ChatTriggeredFeature = require('./ChatTriggeredFeature');
const Discord = require('discord.js');
const util = require('./util');

/**
* Class representing a bad word
*/
class BadWord extends ChatTriggeredFeature {

static punishmentTypes = ['none','ban','kick','mute','softban','strike','dm'];

static defaultResponse = 'Your message includes words/phrases that are not allowed here!';

static tableName = 'badWords';

static columns = ['guildid', 'trigger', 'punishment', 'response', 'global', 'channels'];

/**
* constructor - create a bad word
* @param {module:"discord.js".Snowflake} gid guild ID
* @param {Object} json options
* @param {Trigger} json.trigger filter that triggers the bad word
* @param {String|Punishment} json.punishment punishment for the members which trigger this
* @param {String} [json.response] a message that is send by this filter. It's automatically deleted after 5 seconds
* @param {Boolean} json.global does this apply to all channels in this guild
* @param {module:"discord.js".Snowflake[]} [json.channels] channels that this applies to
* @param {Number} [id] id in DB
* @return {BadWord}
*/
constructor(gid, json, id) {
super(id);
this.gid = gid;

if (json) {
this.trigger = json.trigger;
this.punishment = typeof(json.punishment) === 'string' ? JSON.parse(json.punishment) : json.punishment;
this.response = json.response;
if (this.punishment && this.punishment.action === 'dm' && this.response && this.response !== 'disabled') {
if (this.response === 'default') {
this.punishment.message = BadWord.defaultResponse;
} else {
this.punishment.message = this.response;
static punishmentTypes = ['none', 'ban', 'kick', 'mute', 'softban', 'strike', 'dm'];

static defaultResponse = 'Your message includes words/phrases that are not allowed here!';

static tableName = 'badWords';

static columns = ['guildid', 'trigger', 'punishment', 'response', 'global', 'channels', 'priority'];

/**
* constructor - create a bad word
* @param {module:"discord.js".Snowflake} gid guild ID
* @param {Object} json options
* @param {Trigger} json.trigger filter that triggers the bad word
* @param {String|Punishment} json.punishment punishment for the members which trigger this
* @param {String} [json.response] a message that is send by this filter. It's automatically deleted after 5 seconds
* @param {Boolean} json.global does this apply to all channels in this guild
* @param {module:"discord.js".Snowflake[]} [json.channels] channels that this applies to
* @param {Number} [json.priority] badword priority (higher -> more important)
* @param {Number} [id] id in DB
* @return {BadWord}
*/
constructor(gid, json, id) {
super(id, json.trigger);
this.gid = gid;

if (json) {
this.punishment = typeof (json.punishment) === 'string' ? JSON.parse(json.punishment) : json.punishment;
this.response = json.response;
if (this.punishment && this.punishment.action === 'dm' && this.response && this.response !== 'disabled') {
if (this.response === 'default') {
this.punishment.message = BadWord.defaultResponse;
} else {
this.punishment.message = this.response;
}
}
this.global = json.global;
this.channels = json.channels;
this.priority = json.priority || 0;
}
}
this.global = json.global;
this.channels = json.channels;

if (!this.channels) {
this.channels = [];
}
}

/**
* serialize the bad word
* @returns {(*|string)[]}
*/
serialize() {
return [this.gid, JSON.stringify(this.trigger), JSON.stringify(this.punishment), this.response, this.global, this.channels.join(','), this.priority];
}

if (!this.channels) {
this.channels = [];
/**
* generate an Embed displaying the info of this bad word
* @param {String} title
* @param {Number} color
* @returns {module:"discord.js".MessageEmbed}
*/
embed(title, color) {
const duration = this.punishment.duration, trigger = this.trigger;
return new Discord.MessageEmbed()
.setTitle(title + ` [${this.id}]`)
.setColor(color)
.addFields(
/** @type {any} */[
{
name: 'Trigger',
value: `${trigger.type}: \`${trigger.type === 'regex' ? '/' + trigger.content + '/' + trigger.flags : trigger.content}\``.substring(0, 1000),
inline: true
},
{
name: 'Response',
value: this.response === 'default' ? BadWord.defaultResponse : this.response.substring(0, 1000),
inline: true
},
{
name: 'Channels',
value: this.global ? 'global' : this.channels.map(c => `<#${c}>`).join(', ').substring(0, 1000),
inline: true
},
{
name: 'Punishment',
value: `${this.punishment.action} ${duration ? `for ${duration}` : ''}`,
inline: true
},
{
name: 'Priority',
value: this.priority,
inline: true
},
]);
}
}

/**
* serialize the bad word
* @returns {(*|string)[]}
*/
serialize() {
return [this.gid, JSON.stringify(this.trigger), JSON.stringify(this.punishment), this.response, this.global, this.channels.join(',')];
}

/**
* generate an Embed displaying the info of this bad word
* @param {String} title
* @param {Number} color
* @returns {module:"discord.js".MessageEmbed}
*/
embed(title, color) {
const embed = new Discord.MessageEmbed()
.setTitle(title + ` [${this.id}]`)
.setColor(color)
.addFields(
/** @type {any} */[
{name: "Trigger", value: `${this.trigger.type}: \`${this.trigger.type === 'regex' ? '/' + this.trigger.content + '/' + this.trigger.flags : this.trigger.content}\``.substring(0, 1000)},
{name: "Response", value: this.response === 'default' ? BadWord.defaultResponse :this.response.substring(0,1000)},
{name: "Channels", value: this.global ? "global" : this.channels.map(c => `<#${c}>`).join(', ').substring(0, 1000)}
]);
if (this.punishment.action) {
embed.addField("Punishment", `${this.punishment.action} ${this.punishment.duration ? `for ${this.punishment.duration}` : ''}`)

/**
* create a new bad word
* @param {Snowflake} guildID
* @param {boolean} global
* @param {Snowflake[]|null} channels
* @param {String} triggerType
* @param {String} triggerContent
* @returns {Promise<{success:boolean, badWord: BadWord, message: String}>}
*/
static async new(guildID, global, channels, triggerType, triggerContent) {

let trigger = this.getTrigger(triggerType, triggerContent);
if (!trigger.success) return trigger;

const badWord = new BadWord(guildID, {
trigger: trigger.trigger,
punishment: {action: 'none'},
global,
channels,
response: 'disabled'
});
await badWord.save();
return {success: true, badWord};
}

/**
* edit this badword
* @param {String} option option to change
* @param {String[]} args
* @param {module:"discord.js".Guild} guild
* @returns {Promise<String>} response message
*/
async edit(option, args, guild) {
switch (option) {
case 'trigger': {
let trigger = this.constructor.getTrigger(args.shift(), args.join(' '));
if (!trigger.success) return trigger.message;
this.trigger = trigger.trigger;
await this.save();
return 'Successfully changed trigger';
}

case 'response': {
let response = args.join(' ');
if (!response) response = 'disabled';

this.response = response;
await this.save();
return `Successfully ${response === 'disabled' ? 'disabled' : 'changed'} response`;
}

case 'punishment': {
let action = args.shift().toLowerCase(),
duration = args.join(' ');
if (!this.constructor.punishmentTypes.includes(action)) return 'Unknown punishment';
this.punishment = {action, duration};
await this.save();
return `Successfully ${action === 'none' ? 'disabled' : 'changed'} punishment`;
}

case 'priority': {
let priority = parseInt(args.shift());
if (Number.isNaN(priority)) return 'Invalid priority';
this.priority = priority;
await this.save();
return `Successfully changed priority to ${priority}`;
}

case 'channels': {
if (args[0].toLowerCase() === 'global') {
this.global = true;
this.channels = [];
}
else {
let channels = util.channelMentions(guild, args);
if (!channels) return 'No valid channels specified';
this.global = false;
this.channels = channels;
}
await this.save();
return global ? 'Successfully made this badword global' : 'Successfully changed channels';
}

default: {
return 'Unknown option';
}
}
}
return embed;
}
}

module.exports = BadWord;
Loading

0 comments on commit 0e21ed8

Please sign in to comment.