Improved and fixed embed generation command

This commit is contained in:
Lily Wonhalf 2021-07-11 22:29:11 -04:00
parent bbdb5f249f
commit 30daa4627e
7 changed files with 93 additions and 26 deletions

1
bot.js
View file

@ -55,6 +55,7 @@ const botProcess = () => {
const { Client } = require('discord.js'); const { Client } = require('discord.js');
global.bot = new Client({ fetchAllMembers: true }); global.bot = new Client({ fetchAllMembers: true });
global.isRightGuild = (guildSnowflake) => guildSnowflake === Config.guild;
const Config = require('./config.json'); const Config = require('./config.json');
const Command = require('./model/command'); const Command = require('./model/command');

View file

@ -4,7 +4,8 @@
"guild": "", "guild": "",
"mom": "", "mom": "",
"roles": { "roles": {
"mod": "" "mod": "",
"helper": ""
}, },
"channels": { "channels": {
"joins": "" "joins": ""

View file

@ -1,5 +1,7 @@
const Guild = require('../../model/guild'); const Guild = require('../../model/guild');
module.exports = async (member) => { module.exports = async (member) => {
Guild.guildMemberAddHandler(member); if ((member.guild === null || isRightGuild(member.guild.id)) && !member.user.bot) {
Guild.guildMemberAddHandler(member);
}
}; };

View file

@ -6,7 +6,7 @@ const Command = require('../../model/command');
module.exports = async (message) => { module.exports = async (message) => {
const user = message.author; const user = message.author;
if (!user.bot) { if ((message.guild === null || isRightGuild(message.guild.id)) && !user.bot) {
await Command.parseMessage(message); await Command.parseMessage(message);
} }
}; };

View file

@ -22,6 +22,16 @@ const CommandPermission = {
return member.id === Config.mom || await Guild.isMemberMod(member); return member.id === Config.mom || await Guild.isMemberMod(member);
}, },
/**
* @param {Message} message
* @returns {Promise.<boolean>}
*/
isMemberModOrHelper: async (message) => {
const member = await Guild.getMemberFromMessage(message);
return await CommandPermission.isMemberMod(message) || await Guild.isMemberHelper(member);
},
/** /**
* @param {Message} message * @param {Message} message
* @returns {Promise.<boolean>} * @returns {Promise.<boolean>}

View file

@ -15,6 +15,12 @@ class EmbedDialog
this.channel = message.channel; this.channel = message.channel;
this.destinationChannel = null; this.destinationChannel = null;
this.prompt = (question, hideSkip = false) => {
return this.channel.send(`${question}\n*${!hideSkip ? '`skip` to skip, ' : ''}\`cancel\` to cancel*`);
};
this.isMessageSkip = message => message.content.toLowerCase() === 'skip';
this.messageFilter = testedMessage => { this.messageFilter = testedMessage => {
const byAuthor = testedMessage.author.id === message.author.id; const byAuthor = testedMessage.author.id === message.author.id;
const hasContent = testedMessage.cleanContent.trim().length > 0; const hasContent = testedMessage.cleanContent.trim().length > 0;
@ -23,7 +29,8 @@ class EmbedDialog
} }
this.channelMessageFilter = testedMessage => { this.channelMessageFilter = testedMessage => {
return this.messageFilter(testedMessage) && testedMessage.mentions.channels.size > 0; return this.messageFilter(testedMessage)
&& (this.isMessageSkip(testedMessage) || testedMessage.mentions.channels.size > 0);
} }
this.returnFirstOfCollection = collection => collection && collection.size ? collection.first() : null; this.returnFirstOfCollection = collection => collection && collection.size ? collection.first() : null;
@ -53,47 +60,61 @@ class EmbedDialog
} }
async execute() { async execute() {
await this.channel.send('#️⃣ In which **channel** would you like this embed to be posted?'); let confirmation = true;
await this.prompt('#️⃣ In which **channel** would you like this embed to be posted?');
const channelMessage = await this.awaitMessage(this.channelMessageFilter); const channelMessage = await this.awaitMessage(this.channelMessageFilter);
if (!channelMessage) { if (!channelMessage) {
return null; return null;
} }
this.destinationChannel = channelMessage.mentions.channels.first(); if (this.isMessageSkip(channelMessage)) {
await this.channel.send('📰 What do you want the **title** of this embed to be (you can also ping someone so it appears as they are saying what is going to be the description)?'); this.destinationChannel = this.channel;
confirmation = false;
} else {
this.destinationChannel = channelMessage.mentions.channels.first();
}
await this.prompt('📰 What do you want the **title** of this embed to be (you can also ping someone so it appears as they are saying what is going to be the description)?');
const titleMessage = await this.awaitMessage(this.messageFilter); const titleMessage = await this.awaitMessage(this.messageFilter);
if (!titleMessage) { if (!titleMessage) {
return null; return null;
} }
if (titleMessage.mentions.members.size > 0) { if (!this.isMessageSkip(titleMessage)) {
const member = titleMessage.mentions.members.first(); if (titleMessage.mentions.members.size > 0) {
this.embed.setAuthor(member.displayName, member.user.displayAvatarURL({ dynamic: true })); const member = titleMessage.mentions.members.first();
} else { this.embed.setAuthor(member.displayName, member.user.displayAvatarURL({dynamic: true}));
this.embed.setTitle(titleMessage.content); } else {
this.embed.setTitle(titleMessage.content);
}
} }
await this.channel.send('🎨 What do you want the **colour** of this embed to be?'); await this.prompt('🎨 What do you want the **colour** of this embed to be?');
const colourMessage = await this.awaitMessage(this.messageFilter); const colourMessage = await this.awaitMessage(this.messageFilter);
if (!colourMessage) { if (!colourMessage) {
return null; return null;
} }
if (colourMessage.content.startsWith('#')) { if (this.isMessageSkip(colourMessage)) {
this.embed.setColor(parseInt(colourMessage.content.substr(1), 16)); this.embed.setColor(APP_MAIN_COLOUR);
} else if (colourMessage.content.startsWith('0x')) {
this.embed.setColor(parseInt(colourMessage.content.substr(2), 16));
} else if (RGB_REGEX.test(colourMessage.content)) {
const [, red, green, blue] = colourMessage.content.match(RGB_REGEX);
this.embed.setColor([parseInt(red), parseInt(green), parseInt(blue)]);
} else { } else {
this.embed.setColor(colourMessage.content.toUpperCase().replace(/[^A-Z]+/gu, '_')); if (colourMessage.content.startsWith('#')) {
this.embed.setColor(parseInt(colourMessage.content.substr(1), 16));
} else if (colourMessage.content.startsWith('0x')) {
this.embed.setColor(parseInt(colourMessage.content.substr(2), 16));
} else if (RGB_REGEX.test(colourMessage.content)) {
const [, red, green, blue] = colourMessage.content.match(RGB_REGEX);
this.embed.setColor([parseInt(red), parseInt(green), parseInt(blue)]);
} else {
this.embed.setColor(colourMessage.content.toUpperCase().replace(/[^A-Z]+/gu, '_'));
}
} }
await this.channel.send('💬 What do you want the **description** (contents) of this embed to be?'); await this.prompt('💬 What do you want the **description** (contents) of this embed to be?', true);
const descriptionMessage = await this.awaitMessage(this.messageFilter); const descriptionMessage = await this.awaitMessage(this.messageFilter);
if (!descriptionMessage) { if (!descriptionMessage) {
@ -103,7 +124,9 @@ class EmbedDialog
this.embed.setDescription(descriptionMessage.content); this.embed.setDescription(descriptionMessage.content);
await this.destinationChannel.send(this.embed); await this.destinationChannel.send(this.embed);
return this.channel.send(`✅ The embed has been posted in ${this.destinationChannel}.`); if (confirmation) {
return this.channel.send(`✅ The embed has been posted in ${this.destinationChannel}.`);
}
} }
} }
@ -118,7 +141,7 @@ class Embed
this.aliases = []; this.aliases = [];
this.category = CommandCategory.RESOURCE; this.category = CommandCategory.RESOURCE;
this.isAllowedForContext = CommandPermission.isMemberMod; this.isAllowedForContext = CommandPermission.isMemberModOrHelper;
this.description = 'Allows to post an embed'; this.description = 'Allows to post an embed';
} }
@ -127,9 +150,32 @@ class Embed
* @param {Array} args * @param {Array} args
*/ */
async process(message, args) { async process(message, args) {
const dialog = new EmbedDialog(message); if (args.length > 0) {
let destinationChannel = message.channel;
let deleteMessage = true;
return dialog.execute(); if (/<#\d+>/u.test(args[0])) {
destinationChannel = message.mentions.channels.first();
args.shift();
deleteMessage = false;
}
const embed = new MessageEmbed();
embed.setColor(APP_MAIN_COLOUR);
embed.setDescription(args.join(' '));
await destinationChannel.send(embed);
if (deleteMessage) {
await message.delete();
} else {
await message.react('✅');
}
} else {
const dialog = new EmbedDialog(message);
return dialog.execute();
}
} }
} }

View file

@ -44,6 +44,13 @@ const Guild = {
return member !== undefined && member !== null && member.roles.cache.has(Config.roles.mod); return member !== undefined && member !== null && member.roles.cache.has(Config.roles.mod);
}, },
/**
* @param {GuildMember} member
*/
isMemberHelper: (member) => {
return member !== undefined && member !== null && member.roles.cache.has(Config.roles.helper);
},
/** /**
* @param {string} roleName * @param {string} roleName
* @returns {Role|null} * @returns {Role|null}