Skip to content

Commit

Permalink
Merge pull request #75 from mkevenaar/improvement/74-border-colors
Browse files Browse the repository at this point in the history
(#74) Add border color option
  • Loading branch information
mkevenaar authored Jun 25, 2022
2 parents d210418 + bd33411 commit 9b3cf65
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/commands/admin/reactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export const data = new SlashCommandBuilder()
.setName('description')
.setDescription('Description of the role category (optional)')
.setRequired(false);
})
.addStringOption((color) => {
return color
.setName('color')
.setDescription('Hexadecimal color of the role category (optional)')
.setRequired(false);
});
})
.addSubcommand((edit) => {
Expand All @@ -83,6 +89,12 @@ export const data = new SlashCommandBuilder()
.setName('new-name')
.setDescription('New name of the role category you want to edit (optional)')
.setRequired(false);
})
.addStringOption((color) => {
return color
.setName('color')
.setDescription('Hexadecimal color of the role category (optional)')
.setRequired(false);
});
})
.addSubcommand((remove) => {
Expand Down
1 change: 1 addition & 0 deletions src/database/models/reaction.category.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ReactionCategorySchema = new mongoose.Schema({
registeredAt: { type: Number, default: Date.now },
name: { type: String, required: true },
description: { type: String },
color: { type: String },
messageId: { type: String },
roles: [
{
Expand Down
16 changes: 15 additions & 1 deletion src/database/reaction.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ReactionService {
return ReactionCategoryModel.findOneAndDelete(filter);
}

static async create(guildId, categoryName, description = '') {
static async create(guildId, categoryName, description = '', color = '') {
let existingCategory;

try {
Expand All @@ -54,6 +54,10 @@ export class ReactionService {
categoryEntry.description = description;
}

if (!!color?.length) {
categoryEntry.color = color;
}

await categoryEntry.save();
await GuildService.addCategory(guildId, categoryEntry);
return categoryEntry;
Expand All @@ -78,6 +82,16 @@ export class ReactionService {
return categoryEntry;
}

static async updateColor(guildId, categoryName, color) {
let categoryEntry = await this.get(guildId, categoryName);
if (!!color?.length) {
categoryEntry.color = color;
}

await categoryEntry.save();
return categoryEntry;
}

static async updateName(guildId, categoryName, newName) {
let categoryEntry = await this.get(guildId, categoryName);
if (!!newName?.length) {
Expand Down
8 changes: 8 additions & 0 deletions src/exceptions/runtime.exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export class EmojiDoesNotExistException extends Error {
}
}

export class InvalidColorException extends Error {
constructor(message, path) {
super(message);
this.name = InvalidColorException.name;
this.path = path;
}
}

export class InvalidPermissionException extends Error {
constructor(message, path) {
super(message);
Expand Down
44 changes: 36 additions & 8 deletions src/tools/reactions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { MessageEmbed } from 'discord.js';
import { EmojiDoesNotExistException } from '../exceptions/runtime.exceptions.js';
import {
EmojiDoesNotExistException,
InvalidColorException,
} from '../exceptions/runtime.exceptions.js';
import { isHexColor } from './tools.js';

export class reactionTools {
static async configureReactions(interaction, client) {
Expand All @@ -23,10 +27,20 @@ export class reactionTools {
static async addCategory(interaction, client) {
const name = interaction.options.getString('name');
const description = interaction.options.getString('description');
const color = interaction.options.getString('color');

const reactionService = client.database.ReactionService;

await reactionService.create(interaction.guild.id, name, description);
let colorValidation = isHexColor(color);

//Validation
if (!colorValidation) {
throw new InvalidColorException(
'Color verification failed. Make sure you use an Hexadecimal color. e.g. #aa22cc or #a2c'
);
}

await reactionService.create(interaction.guild.id, name, description, color);

await this.updateRoleMessage(client, interaction.guild, name);
}
Expand All @@ -35,6 +49,7 @@ export class reactionTools {
const name = interaction.options.getString('name');
const description = interaction.options.getString('description');
const newName = interaction.options.getString('new-name');
const color = interaction.options.getString('color');

const reactionService = client.database.ReactionService;

Expand All @@ -44,6 +59,18 @@ export class reactionTools {
await reactionService.updateDescription(interaction.guild.id, name, description);
}

if (!!color?.length) {
let colorValidation = isHexColor(color);
console.log(color);
//Validation
if (!colorValidation) {
throw new InvalidColorException(
'Color verification failed. Make sure you use an Hexadecimal color. e.g. #aa22cc or #a2c'
);
}
await reactionService.updateColor(interaction.guild.id, name, color);
}

if (!!newName?.length) {
await reactionService.updateName(interaction.guild.id, name, newName);
updateName = newName;
Expand Down Expand Up @@ -79,12 +106,9 @@ export class reactionTools {

//Validation
if (!emojiValidation) {
await interaction.reply({
content:
'Emoji verification failed. Make sure you use an guild or unicode emoji. Emoji from other servers will not work.',
ephemeral: true,
});
return;
throw new EmojiDoesNotExistException(
'Emoji verification failed. Make sure you use an guild or unicode emoji. Emoji from other servers will not work.'
);
}

await reactionRoleService.create(interaction.guild.id, category, role, description, emoji);
Expand Down Expand Up @@ -155,6 +179,10 @@ export class reactionTools {
body.push('');
}

if (category.color) {
message.setColor(category.color);
}

category.roles.forEach((role) => {
let roleText = role.description ? role.description : '<@&' + role.id + '>';
body.push(role.emoji + ' - ' + roleText);
Expand Down
14 changes: 14 additions & 0 deletions src/tools/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ export function isURL(str) {
var url = new RegExp(urlRegex, 'i');
return str.length < 2083 && url.test(str);
}

export function isHexColor(str) {
if (str[0] != '#') {
return false;
}

if (!(str.length == 4 || str.length == 7)) {
return false;
}

var colorRegex = '^#([0-9a-f]{3}){1,2}$';
var color = new RegExp(colorRegex, 'i');
return color.test(str);
}

0 comments on commit 9b3cf65

Please sign in to comment.