Skip to content

Commit 505f8e1

Browse files
Techbot121Meta Construct
authored andcommitted
add_ -> set_ since you can have only one (normally)
1 parent bae53e8 commit 505f8e1

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

app/services/discord/modules/commands/Role.ts

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import axios from "axios";
66
const ROLE_IDENTIFIER = "\u2063";
77
const IMG_TYPES = ["image/png", "image/gif", "image/jpeg"];
88

9+
const NO_ROLE_RESPONSE =
10+
"You don't have a custom role yet! Get one with </role add:1140685420565385256>";
11+
912
const removeRoleIcon = async (ctx: Discord.ChatInputCommandInteraction) => {
1013
const role = (ctx.member as Discord.GuildMember).getCustomRole;
1114
if (!role) {
12-
await ctx.followUp(EphemeralResponse("You don't have a custom role yet!"));
15+
await ctx.followUp(EphemeralResponse(NO_ROLE_RESPONSE));
1316
return;
1417
}
1518
if (!role.icon && !role.unicodeEmoji) {
@@ -21,7 +24,7 @@ const removeRoleIcon = async (ctx: Discord.ChatInputCommandInteraction) => {
2124
await ctx.followUp(EphemeralResponse("Your icon is now gone."));
2225
};
2326

24-
const addRoleColorSpecial = async (ctx: Discord.ChatInputCommandInteraction) => {
27+
const setRoleColorSpecial = async (ctx: Discord.ChatInputCommandInteraction) => {
2528
const possible = ctx.guild && ctx.guild.features.includes("ENHANCED_ROLE_COLORS" as any);
2629
if (!possible) {
2730
await ctx.followUp(
@@ -31,7 +34,7 @@ const addRoleColorSpecial = async (ctx: Discord.ChatInputCommandInteraction) =>
3134
}
3235
const role = (ctx.member as Discord.GuildMember).getCustomRole;
3336
if (!role) {
34-
await ctx.followUp(EphemeralResponse("You don't have a custom role yet!"));
37+
await ctx.followUp(EphemeralResponse(NO_ROLE_RESPONSE));
3538
return;
3639
}
3740
try {
@@ -60,23 +63,23 @@ const addRoleColorSpecial = async (ctx: Discord.ChatInputCommandInteraction) =>
6063
}
6164
};
6265

63-
const addRoleIcon = async (ctx: Discord.ChatInputCommandInteraction, download: boolean) => {
66+
const setRoleIcon = async (ctx: Discord.ChatInputCommandInteraction, download: boolean) => {
6467
const premiumTier = ctx.guild?.premiumTier;
6568
if (premiumTier && premiumTier < 2) {
6669
await ctx.followUp(
67-
EphemeralResponse("Sorry we need Server Boost Tier 2 to use this feature...")
70+
EphemeralResponse("Sorry we need Server Boost Level 2 to use this feature...")
6871
);
6972
return;
7073
}
7174
const role = (ctx.member as Discord.GuildMember).getCustomRole;
7275
if (!role) {
73-
await ctx.followUp(EphemeralResponse("You don't have a custom role yet!"));
76+
await ctx.followUp(EphemeralResponse(NO_ROLE_RESPONSE));
7477
return;
7578
}
7679
if (download) {
7780
return await uploadIcon(ctx, role);
7881
}
79-
return await addEmoji(ctx, ctx.options.getString("emoji", true), role);
82+
return await setEmoji(ctx, ctx.options.getString("emoji", true), role);
8083
};
8184

8285
const uploadIcon = async (ctx: Discord.ChatInputCommandInteraction, role: Discord.Role) => {
@@ -121,7 +124,7 @@ const uploadIcon = async (ctx: Discord.ChatInputCommandInteraction, role: Discor
121124

122125
await ctx.followUp(EphemeralResponse("missing file or invalid url"));
123126
};
124-
const addEmoji = async (
127+
const setEmoji = async (
125128
ctx: Discord.ChatInputCommandInteraction,
126129
emoji: string,
127130
role: Discord.Role
@@ -176,11 +179,9 @@ const removeRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any
176179
: await ctx.followUp(EphemeralResponse("You don't have a custom role..."));
177180
};
178181

179-
const addRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any> => {
180-
const roleName = ctx.options.getString("name", true) + ROLE_IDENTIFIER;
181-
182-
const roleColor =
183-
parseInt(ctx.options.getString("hex", true).replace(/^#+/, ""), 16) ?? "Random";
182+
const setRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any> => {
183+
const roleName = ctx.options.getString("name") + ROLE_IDENTIFIER;
184+
const hex = ctx.options.getString("hex");
184185

185186
const guild = ctx.guild;
186187
if (!guild) {
@@ -190,14 +191,22 @@ const addRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any> =
190191

191192
const roles = guild.roles;
192193
const member = ctx.member as Discord.GuildMember;
194+
const customRole = member.getCustomRole;
195+
196+
// if a colour is defined replace it. Otherwise fall back to the existing one (to change the name only) or get a random one
197+
const roleColor = hex
198+
? parseInt(hex.replace(/^#+/, ""), 16)
199+
: customRole
200+
? customRole.color
201+
: "Random";
202+
193203
let targetRole = roles.cache.find((r: { name: string }) => r.name === roleName);
194204
if (!targetRole) {
195205
// if we have an another existing role, remove it
196-
const existingRole = (ctx.member as Discord.GuildMember).getCustomRole;
197-
if (existingRole) {
198-
await member.roles.remove(existingRole);
199-
if (existingRole.members.size === 0) {
200-
await existingRole.delete();
206+
if (customRole) {
207+
await member.roles.remove(customRole);
208+
if (customRole.members.size === 0) {
209+
await customRole.delete();
201210
}
202211
}
203212

@@ -241,23 +250,22 @@ export const SlashRoleCommand: SlashCommand = {
241250
description: "The name of your role",
242251
required: true,
243252
},
253+
{
254+
type: Discord.ApplicationCommandOptionType.String,
255+
name: "hex",
256+
description: "Hex color for the role for example #465f83",
257+
},
244258
],
245259
},
246260
{
247261
type: Discord.ApplicationCommandOptionType.Subcommand,
248-
name: "add_hex",
249-
description: "Adds a custom role with a hex color",
262+
name: "set_color",
263+
description: "Changes the custom role color",
250264
options: [
251-
{
252-
type: Discord.ApplicationCommandOptionType.String,
253-
name: "name",
254-
description: "The name of your role",
255-
required: true,
256-
},
257265
{
258266
type: Discord.ApplicationCommandOptionType.String,
259267
name: "hex",
260-
description: "Hex color value for example #465f83",
268+
description: "Hex color for the role example #465f83",
261269
required: true,
262270
},
263271
],
@@ -269,25 +277,25 @@ export const SlashRoleCommand: SlashCommand = {
269277
},
270278
{
271279
type: Discord.ApplicationCommandOptionType.Subcommand,
272-
name: "add_gradient",
280+
name: "set_gradient",
273281
description: "Adds a secondary color to make a gradient, if it's unlocked.",
274282
options: [
275283
{
276284
type: Discord.ApplicationCommandOptionType.String,
277285
name: "secondary_color",
278-
description: "Hex color value for the tail example #465f83",
286+
description: "Hex color for the end example #465f83",
279287
required: true,
280288
},
281289
{
282290
type: Discord.ApplicationCommandOptionType.String,
283291
name: "primary_color",
284-
description: "Hex color value for the primary example #465f83",
292+
description: "Hex color for the start example #465f83",
285293
},
286294
],
287295
},
288296
{
289297
type: Discord.ApplicationCommandOptionType.Subcommand,
290-
name: "add_icon",
298+
name: "set_icon",
291299
description: "adds a custom icon to your role",
292300
options: [
293301
{
@@ -304,7 +312,7 @@ export const SlashRoleCommand: SlashCommand = {
304312
},
305313
{
306314
type: Discord.ApplicationCommandOptionType.Subcommand,
307-
name: "add_emoji",
315+
name: "set_emoji",
308316
description: "adds an emoji to your role",
309317
options: [
310318
{
@@ -328,17 +336,17 @@ export const SlashRoleCommand: SlashCommand = {
328336
try {
329337
switch (cmd) {
330338
case "add":
331-
case "add_hex":
332-
await addRole(ctx);
339+
case "set_color":
340+
await setRole(ctx);
333341
break;
334-
case "add_gradient":
335-
await addRoleColorSpecial(ctx);
342+
case "set_gradient":
343+
await setRoleColorSpecial(ctx);
336344
break;
337-
case "add_emoji":
338-
await addRoleIcon(ctx, false);
345+
case "set_emoji":
346+
await setRoleIcon(ctx, false);
339347
break;
340-
case "add_icon":
341-
await addRoleIcon(ctx, true);
348+
case "set_icon":
349+
await setRoleIcon(ctx, true);
342350
break;
343351
case "remove":
344352
await removeRole(ctx);

extensions/discord.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ declare module "discord.js" {
3636
interface GuildMember {
3737
mention: string;
3838
hasCustomRole: boolean;
39-
getCustomRole: Discord.Role;
39+
getCustomRole: Discord.Role | undefined;
4040
}
4141
}
4242

0 commit comments

Comments
 (0)