Skip to content

Commit

Permalink
Merge pull request #318 from aternosorg/autoresponses
Browse files Browse the repository at this point in the history
refactor auto response command
  • Loading branch information
JulianVennen authored Jul 4, 2021
2 parents f2685d2 + 1a7e3bf commit a756303
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 285 deletions.
187 changes: 132 additions & 55 deletions src/AutoResponse.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,144 @@
const ChatTriggeredFeature = require('./ChatTriggeredFeature');
const Discord = require('discord.js');
const util = require('./util');

/**
* Class representing an auto response
*/
class AutoResponse extends ChatTriggeredFeature {

static tableName = 'responses';

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

/**
* constructor - create an auto response
* @param {module:"discord.js".Snowflake} gid guild ID
* @param {Object} json options
* @param {Trigger} json.trigger filter that triggers the response
* @param {String} json.response message to send to the channel
* @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 {AutoResponse} the auto response
*/
constructor(gid, json, id) {
super(id, json.trigger);
this.gid = gid;

if (json) {
this.response = json.response;
this.global = json.global;
this.channels = json.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(',')];
}

/**
* generate an Embed displaying the info of this response
* @param {String} title
* @param {Number} color
* @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)}
]);
}
static tableName = 'responses';

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

/**
* constructor - create an auto response
* @param {module:"discord.js".Snowflake} gid guild ID
* @param {Object} json options
* @param {Trigger} json.trigger filter that triggers the response
* @param {String} json.response message to send to the channel
* @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 {AutoResponse} the auto response
*/
constructor(gid, json, id) {
super(id, json.trigger);
this.gid = gid;

if (json) {
this.response = json.response;
this.global = json.global;
this.channels = json.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(',')];
}

/**
* generate an Embed displaying the info of this response
* @param {String} title
* @param {Number} color
* @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)
}
]);
}

/**
* create a new response
* @param {Snowflake} guildID
* @param {boolean} global
* @param {Snowflake[]|null} channels
* @param {String} triggerType
* @param {String} triggerContent
* @param {String} responseText
* @returns {Promise<{success:boolean, response: AutoResponse, message: String}>}
*/
static async new(guildID, global, channels, triggerType, triggerContent, responseText) {
let trigger = this.getTrigger(triggerType, triggerContent);
if (!trigger.success) return trigger;

const response = new AutoResponse(guildID, {
trigger: trigger.trigger,
global,
channels,
response: responseText
});
await response.save();
return {success: true, response: response};
}

/**
* edit this auto-response
* @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 'channels': {
if (args[0].toLowerCase() === 'global') {
this.global = true;
this.channels = [];
}
else {
let channels = await 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 auto-response global' : 'Successfully changed channels';
}

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

module.exports = AutoResponse;
2 changes: 1 addition & 1 deletion src/BadWord.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class BadWord extends ChatTriggeredFeature {
this.channels = [];
}
else {
let channels = util.channelMentions(guild, args);
let channels = await util.channelMentions(guild, args);
if (!channels) return 'No valid channels specified';
this.global = false;
this.channels = channels;
Expand Down
52 changes: 0 additions & 52 deletions src/commands/legacy/autoresponse.js

This file was deleted.

89 changes: 0 additions & 89 deletions src/commands/legacy/autoresponse/add.js

This file was deleted.

22 changes: 0 additions & 22 deletions src/commands/legacy/autoresponse/info.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/commands/legacy/autoresponse/list.js

This file was deleted.

Loading

0 comments on commit a756303

Please sign in to comment.