Skip to content

Commit 9b3cf65

Browse files
authored
Merge pull request #75 from mkevenaar/improvement/74-border-colors
(#74) Add border color option
2 parents d210418 + bd33411 commit 9b3cf65

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

src/commands/admin/reactions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export const data = new SlashCommandBuilder()
6060
.setName('description')
6161
.setDescription('Description of the role category (optional)')
6262
.setRequired(false);
63+
})
64+
.addStringOption((color) => {
65+
return color
66+
.setName('color')
67+
.setDescription('Hexadecimal color of the role category (optional)')
68+
.setRequired(false);
6369
});
6470
})
6571
.addSubcommand((edit) => {
@@ -83,6 +89,12 @@ export const data = new SlashCommandBuilder()
8389
.setName('new-name')
8490
.setDescription('New name of the role category you want to edit (optional)')
8591
.setRequired(false);
92+
})
93+
.addStringOption((color) => {
94+
return color
95+
.setName('color')
96+
.setDescription('Hexadecimal color of the role category (optional)')
97+
.setRequired(false);
8698
});
8799
})
88100
.addSubcommand((remove) => {

src/database/models/reaction.category.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const ReactionCategorySchema = new mongoose.Schema({
55
registeredAt: { type: Number, default: Date.now },
66
name: { type: String, required: true },
77
description: { type: String },
8+
color: { type: String },
89
messageId: { type: String },
910
roles: [
1011
{

src/database/reaction.service.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class ReactionService {
2929
return ReactionCategoryModel.findOneAndDelete(filter);
3030
}
3131

32-
static async create(guildId, categoryName, description = '') {
32+
static async create(guildId, categoryName, description = '', color = '') {
3333
let existingCategory;
3434

3535
try {
@@ -54,6 +54,10 @@ export class ReactionService {
5454
categoryEntry.description = description;
5555
}
5656

57+
if (!!color?.length) {
58+
categoryEntry.color = color;
59+
}
60+
5761
await categoryEntry.save();
5862
await GuildService.addCategory(guildId, categoryEntry);
5963
return categoryEntry;
@@ -78,6 +82,16 @@ export class ReactionService {
7882
return categoryEntry;
7983
}
8084

85+
static async updateColor(guildId, categoryName, color) {
86+
let categoryEntry = await this.get(guildId, categoryName);
87+
if (!!color?.length) {
88+
categoryEntry.color = color;
89+
}
90+
91+
await categoryEntry.save();
92+
return categoryEntry;
93+
}
94+
8195
static async updateName(guildId, categoryName, newName) {
8296
let categoryEntry = await this.get(guildId, categoryName);
8397
if (!!newName?.length) {

src/exceptions/runtime.exceptions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ export class EmojiDoesNotExistException extends Error {
2222
}
2323
}
2424

25+
export class InvalidColorException extends Error {
26+
constructor(message, path) {
27+
super(message);
28+
this.name = InvalidColorException.name;
29+
this.path = path;
30+
}
31+
}
32+
2533
export class InvalidPermissionException extends Error {
2634
constructor(message, path) {
2735
super(message);

src/tools/reactions.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { MessageEmbed } from 'discord.js';
2-
import { EmojiDoesNotExistException } from '../exceptions/runtime.exceptions.js';
2+
import {
3+
EmojiDoesNotExistException,
4+
InvalidColorException,
5+
} from '../exceptions/runtime.exceptions.js';
6+
import { isHexColor } from './tools.js';
37

48
export class reactionTools {
59
static async configureReactions(interaction, client) {
@@ -23,10 +27,20 @@ export class reactionTools {
2327
static async addCategory(interaction, client) {
2428
const name = interaction.options.getString('name');
2529
const description = interaction.options.getString('description');
30+
const color = interaction.options.getString('color');
2631

2732
const reactionService = client.database.ReactionService;
2833

29-
await reactionService.create(interaction.guild.id, name, description);
34+
let colorValidation = isHexColor(color);
35+
36+
//Validation
37+
if (!colorValidation) {
38+
throw new InvalidColorException(
39+
'Color verification failed. Make sure you use an Hexadecimal color. e.g. #aa22cc or #a2c'
40+
);
41+
}
42+
43+
await reactionService.create(interaction.guild.id, name, description, color);
3044

3145
await this.updateRoleMessage(client, interaction.guild, name);
3246
}
@@ -35,6 +49,7 @@ export class reactionTools {
3549
const name = interaction.options.getString('name');
3650
const description = interaction.options.getString('description');
3751
const newName = interaction.options.getString('new-name');
52+
const color = interaction.options.getString('color');
3853

3954
const reactionService = client.database.ReactionService;
4055

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

62+
if (!!color?.length) {
63+
let colorValidation = isHexColor(color);
64+
console.log(color);
65+
//Validation
66+
if (!colorValidation) {
67+
throw new InvalidColorException(
68+
'Color verification failed. Make sure you use an Hexadecimal color. e.g. #aa22cc or #a2c'
69+
);
70+
}
71+
await reactionService.updateColor(interaction.guild.id, name, color);
72+
}
73+
4774
if (!!newName?.length) {
4875
await reactionService.updateName(interaction.guild.id, name, newName);
4976
updateName = newName;
@@ -79,12 +106,9 @@ export class reactionTools {
79106

80107
//Validation
81108
if (!emojiValidation) {
82-
await interaction.reply({
83-
content:
84-
'Emoji verification failed. Make sure you use an guild or unicode emoji. Emoji from other servers will not work.',
85-
ephemeral: true,
86-
});
87-
return;
109+
throw new EmojiDoesNotExistException(
110+
'Emoji verification failed. Make sure you use an guild or unicode emoji. Emoji from other servers will not work.'
111+
);
88112
}
89113

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

182+
if (category.color) {
183+
message.setColor(category.color);
184+
}
185+
158186
category.roles.forEach((role) => {
159187
let roleText = role.description ? role.description : '<@&' + role.id + '>';
160188
body.push(role.emoji + ' - ' + roleText);

src/tools/tools.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,17 @@ export function isURL(str) {
5454
var url = new RegExp(urlRegex, 'i');
5555
return str.length < 2083 && url.test(str);
5656
}
57+
58+
export function isHexColor(str) {
59+
if (str[0] != '#') {
60+
return false;
61+
}
62+
63+
if (!(str.length == 4 || str.length == 7)) {
64+
return false;
65+
}
66+
67+
var colorRegex = '^#([0-9a-f]{3}){1,2}$';
68+
var color = new RegExp(colorRegex, 'i');
69+
return color.test(str);
70+
}

0 commit comments

Comments
 (0)