Skip to content

Commit bae53e8

Browse files
Techbot121Meta Construct
authored andcommitted
add gradients to role command
1 parent 6675448 commit bae53e8

File tree

1 file changed

+68
-53
lines changed
  • app/services/discord/modules/commands

1 file changed

+68
-53
lines changed

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

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const ROLE_IDENTIFIER = "\u2063";
77
const IMG_TYPES = ["image/png", "image/gif", "image/jpeg"];
88

99
const removeRoleIcon = async (ctx: Discord.ChatInputCommandInteraction) => {
10-
const role = (<Discord.GuildMember>ctx.member).getCustomRole;
10+
const role = (ctx.member as Discord.GuildMember).getCustomRole;
1111
if (!role) {
1212
await ctx.followUp(EphemeralResponse("You don't have a custom role yet!"));
1313
return;
@@ -21,6 +21,45 @@ const removeRoleIcon = async (ctx: Discord.ChatInputCommandInteraction) => {
2121
await ctx.followUp(EphemeralResponse("Your icon is now gone."));
2222
};
2323

24+
const addRoleColorSpecial = async (ctx: Discord.ChatInputCommandInteraction) => {
25+
const possible = ctx.guild && ctx.guild.features.includes("ENHANCED_ROLE_COLORS" as any);
26+
if (!possible) {
27+
await ctx.followUp(
28+
EphemeralResponse("Sorry we need enhanced role colors to use this feature...")
29+
);
30+
return;
31+
}
32+
const role = (ctx.member as Discord.GuildMember).getCustomRole;
33+
if (!role) {
34+
await ctx.followUp(EphemeralResponse("You don't have a custom role yet!"));
35+
return;
36+
}
37+
try {
38+
// super hacky but should work
39+
const primary = ctx.options.getString("primary_color");
40+
41+
const primaryColor = primary ? parseInt(primary.replace(/^#+/, ""), 16) : role.color;
42+
const secondaryColor = parseInt(
43+
ctx.options.getString("secondary_color", true).replace(/^#+/, ""),
44+
16
45+
);
46+
47+
await ctx.client.rest.patch(Discord.Routes.guildRole(ctx.guild.id, role.id), {
48+
body: {
49+
colors: {
50+
primary_color: primaryColor,
51+
secondary_color: secondaryColor,
52+
},
53+
},
54+
reason: "Added gradient via command",
55+
} as any);
56+
await ctx.followUp(EphemeralResponse("👍"));
57+
} catch (error) {
58+
console.error(error);
59+
await ctx.followUp(EphemeralResponse("Something went wrong trying to add the gradient :("));
60+
}
61+
};
62+
2463
const addRoleIcon = async (ctx: Discord.ChatInputCommandInteraction, download: boolean) => {
2564
const premiumTier = ctx.guild?.premiumTier;
2665
if (premiumTier && premiumTier < 2) {
@@ -29,7 +68,7 @@ const addRoleIcon = async (ctx: Discord.ChatInputCommandInteraction, download: b
2968
);
3069
return;
3170
}
32-
const role = (<Discord.GuildMember>ctx.member).getCustomRole;
71+
const role = (ctx.member as Discord.GuildMember).getCustomRole;
3372
if (!role) {
3473
await ctx.followUp(EphemeralResponse("You don't have a custom role yet!"));
3574
return;
@@ -46,8 +85,8 @@ const uploadIcon = async (ctx: Discord.ChatInputCommandInteraction, role: Discor
4685
const reqURL = attachment
4786
? attachment.url
4887
: url
49-
? url.match(/(https?:\/\/.*\.(?:png|jpg))/)?.[0]
50-
: undefined;
88+
? url.match(/(https?:\/\/.*\.(?:png|jpg))/)?.[0]
89+
: undefined;
5190

5291
if (reqURL) {
5392
try {
@@ -124,8 +163,8 @@ const addEmoji = async (
124163
};
125164

126165
const removeRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any> => {
127-
const role = (<Discord.GuildMember>ctx.member).getCustomRole;
128-
const member = <Discord.GuildMember>ctx.member;
166+
const role = (ctx.member as Discord.GuildMember).getCustomRole;
167+
const member = ctx.member as Discord.GuildMember;
129168
if (role && member) {
130169
await member.roles.remove(role, "Removed role via command");
131170
if (role.members.size === 0) {
@@ -138,17 +177,10 @@ const removeRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any
138177
};
139178

140179
const addRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any> => {
141-
const isRGB = ctx.options.getSubcommand() === "add_rgb";
142180
const roleName = ctx.options.getString("name", true) + ROLE_IDENTIFIER;
143-
const r = ctx.options.getInteger("red")?.toString() ?? "255",
144-
g = ctx.options.getInteger("green")?.toString() ?? "255",
145-
b = ctx.options.getInteger("blue")?.toString() ?? "255";
146181

147-
const roleColor = Discord.resolveColor(
148-
isRGB
149-
? [parseInt(r), parseInt(g), parseInt(b)]
150-
: <Discord.HexColorString>ctx.options.getString("hex") ?? "Random"
151-
);
182+
const roleColor =
183+
parseInt(ctx.options.getString("hex", true).replace(/^#+/, ""), 16) ?? "Random";
152184

153185
const guild = ctx.guild;
154186
if (!guild) {
@@ -157,11 +189,11 @@ const addRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any> =
157189
}
158190

159191
const roles = guild.roles;
160-
const member = <Discord.GuildMember>ctx.member;
192+
const member = ctx.member as Discord.GuildMember;
161193
let targetRole = roles.cache.find((r: { name: string }) => r.name === roleName);
162194
if (!targetRole) {
163195
// if we have an another existing role, remove it
164-
const existingRole = (<Discord.GuildMember>ctx.member).getCustomRole;
196+
const existingRole = (ctx.member as Discord.GuildMember).getCustomRole;
165197
if (existingRole) {
166198
await member.roles.remove(existingRole);
167199
if (existingRole.members.size === 0) {
@@ -213,8 +245,8 @@ export const SlashRoleCommand: SlashCommand = {
213245
},
214246
{
215247
type: Discord.ApplicationCommandOptionType.Subcommand,
216-
name: "add_rgb",
217-
description: "Adds a custom role with rgb colors",
248+
name: "add_hex",
249+
description: "Adds a custom role with a hex color",
218250
options: [
219251
{
220252
type: Discord.ApplicationCommandOptionType.String,
@@ -223,55 +255,36 @@ export const SlashRoleCommand: SlashCommand = {
223255
required: true,
224256
},
225257
{
226-
type: Discord.ApplicationCommandOptionType.Integer,
227-
name: "red",
228-
description: "The red component of your color 0 - 255",
229-
min_value: 0,
230-
max_value: 255,
231-
required: true,
232-
},
233-
{
234-
type: Discord.ApplicationCommandOptionType.Integer,
235-
name: "green",
236-
description: "The green component of your color 0 - 255",
237-
min_value: 0,
238-
max_value: 255,
239-
required: true,
240-
},
241-
{
242-
type: Discord.ApplicationCommandOptionType.Integer,
243-
name: "blue",
244-
description: "The blue component of your color 0 - 255",
245-
min_value: 0,
246-
max_value: 255,
258+
type: Discord.ApplicationCommandOptionType.String,
259+
name: "hex",
260+
description: "Hex color value for example #465f83",
247261
required: true,
248262
},
249263
],
250264
},
251265
{
252266
type: Discord.ApplicationCommandOptionType.Subcommand,
253-
name: "add_hex",
254-
description: "Adds a custom role with a hex color",
267+
name: "remove",
268+
description: "Removes your custom role",
269+
},
270+
{
271+
type: Discord.ApplicationCommandOptionType.Subcommand,
272+
name: "add_gradient",
273+
description: "Adds a secondary color to make a gradient, if it's unlocked.",
255274
options: [
256275
{
257276
type: Discord.ApplicationCommandOptionType.String,
258-
name: "name",
259-
description: "The name of your role",
277+
name: "secondary_color",
278+
description: "Hex color value for the tail example #465f83",
260279
required: true,
261280
},
262281
{
263282
type: Discord.ApplicationCommandOptionType.String,
264-
name: "hex",
265-
description: "Hex color value for example #465f83",
266-
required: true,
283+
name: "primary_color",
284+
description: "Hex color value for the primary example #465f83",
267285
},
268286
],
269287
},
270-
{
271-
type: Discord.ApplicationCommandOptionType.Subcommand,
272-
name: "remove",
273-
description: "Removes your custom role",
274-
},
275288
{
276289
type: Discord.ApplicationCommandOptionType.Subcommand,
277290
name: "add_icon",
@@ -315,10 +328,12 @@ export const SlashRoleCommand: SlashCommand = {
315328
try {
316329
switch (cmd) {
317330
case "add":
318-
case "add_rgb":
319331
case "add_hex":
320332
await addRole(ctx);
321333
break;
334+
case "add_gradient":
335+
await addRoleColorSpecial(ctx);
336+
break;
322337
case "add_emoji":
323338
await addRoleIcon(ctx, false);
324339
break;

0 commit comments

Comments
 (0)