Skip to content

Commit ba87517

Browse files
feat: timezone roles (#40)
* init * small things * tested and fixed bugs
1 parent 8c13bea commit ba87517

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

src/commands/RoleCommands.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
SlashCommandSubcommandsOnlyBuilder,
66
} from "discord.js";
77
import { hasAdminPerms } from "../util/misc/permissions";
8-
import { colorPrompt, pronounPrompt } from "../util/roleCommand/rolePrompts";
8+
import { colorPrompt, pronounPrompt, timezonePrompt } from "../util/roleCommand/rolePrompts";
99
import { deleteRoles } from "../util/roleCommand/roleUtil";
1010

1111
class RoleCommand extends Command {
@@ -31,6 +31,9 @@ class RoleCommand extends Command {
3131
} else if (subcommand === "color-prompt") {
3232
await interaction.deferReply();
3333
await colorPrompt(interaction);
34+
} else if (subcommand === "timezone-prompt") {
35+
await interaction.deferReply();
36+
await timezonePrompt(interaction);
3437
} else if (subcommand === "delete") {
3538
await interaction.deferReply({ ephemeral: true });
3639
await deleteRoles(interaction);
@@ -59,6 +62,9 @@ class RoleCommand extends Command {
5962
.setMaxValue(4)
6063
)
6164
)
65+
.addSubcommand((option) =>
66+
option.setName("timezone-prompt").setDescription("Creates the timezone role prompt.")
67+
)
6268
.addSubcommand((option) =>
6369
option
6470
.setName("delete")

src/config.ts

+29
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@ export const config: BotConfig = {
2929
["Medium Aquamarine", "#6cd9a9"],
3030
["Sea Serpent", "#5db9cf"],
3131
],
32+
timezoneRoles: [
33+
"UTC-12",
34+
"UTC-11",
35+
"UTC-10",
36+
"UTC-9",
37+
"UTC-8",
38+
"UTC-7",
39+
"UTC-6",
40+
"UTC-5",
41+
"UTC-4",
42+
"UTC-3",
43+
"UTC-2",
44+
"UTC-1",
45+
"UTC",
46+
"UTC+1",
47+
"UTC+2",
48+
"UTC+3",
49+
"UTC+4",
50+
"UTC+5",
51+
"UTC+6",
52+
"UTC+7",
53+
"UTC+8",
54+
"UTC+9",
55+
"UTC+10",
56+
"UTC+11",
57+
"UTC+12",
58+
],
3259
color: "#F0A5AC",
3360
serverDescription:
3461
"We're a group of young and mostly queer people having a game jam/hackathon server together. We're a very friendly and welcoming community and are happy to have you join us! \nCheck out <#1022874504525008997> for more information!",
@@ -46,6 +73,8 @@ interface BotConfig {
4673
pronounRoles: [string, ComponentEmojiResolvable | null][];
4774
// Max 25 color roles to pick from. First argument is the name of the role, second argument is the color.
4875
colorRoles: [string, ColorResolvable][];
76+
// Timezones users can pick from.
77+
timezoneRoles: string[];
4978
// The main color the bot will use.
5079
color: ColorResolvable;
5180
// A description of the server that will be displayed in the welcome message.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { SelectMenu } from "../interactionClasses";
2+
import { inlineCode, SelectMenuInteraction } from "discord.js";
3+
import { logger } from "../../logger";
4+
5+
class RoleSelectMenu extends SelectMenu {
6+
constructor() {
7+
super("role");
8+
}
9+
10+
async execute(interaction: SelectMenuInteraction, subcommand: string[]): Promise<void> {
11+
if (subcommand[0] == "timezone") {
12+
const timezone = interaction.values[0] ?? "None";
13+
const guildRoles = await interaction.guild?.roles.fetch();
14+
const member = await interaction.guild?.members.fetch(interaction.user.id);
15+
const role = guildRoles?.find((r) => r.name === timezone);
16+
if (!role || !member) {
17+
await interaction.reply("Can't find the role or you (the member)...");
18+
logger.warn("Couldn't find a role or member. Probably is an old prompt still open.");
19+
logger.error(role);
20+
logger.error(member);
21+
return;
22+
}
23+
24+
const currentTimezoneRoles = member.roles.cache.filter((r) => r.name.startsWith("UTC"));
25+
26+
for (const currentRole of currentTimezoneRoles) {
27+
await member.roles.remove(currentRole);
28+
}
29+
30+
if (role) {
31+
await member.roles.add(role);
32+
await interaction.reply({ content: `Gave you the ${inlineCode(timezone)} role.`, ephemeral: true });
33+
} else {
34+
await interaction.reply({
35+
content: `Removed your ${currentTimezoneRoles.map((c) => inlineCode(c.name)).join(" ")} role.`,
36+
ephemeral: true,
37+
});
38+
}
39+
}
40+
}
41+
}
42+
43+
export default new RoleSelectMenu();

src/util/roleCommand/rolePrompts.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import {
66
ButtonStyle,
77
ChatInputCommandInteraction,
88
EmbedBuilder,
9+
SelectMenuBuilder,
910
} from "discord.js";
1011
import { addDefaultEmbedFooter } from "../misc/embeds";
1112
import { createCanvas } from "@napi-rs/canvas";
1213
import { bringIntoButtonGrid, setUpRoles } from "./roleUtil";
1314

1415
export async function pronounPrompt(interaction: ChatInputCommandInteraction): Promise<void> {
1516
const prompt: EmbedBuilder = new EmbedBuilder()
16-
.setTitle("Pronoun roles 🌈🏳‍⚧️⚧️")
17+
.setTitle("Pronoun roles 🏳‍⚧️⚧️")
1718
.setDescription(
1819
"Select the pronouns you want others to use when referring to you :)\nIf you don't understand why: https://pronouns.org/what-and-why"
1920
);
@@ -127,3 +128,35 @@ export async function colorPrompt(interaction: ChatInputCommandInteraction): Pro
127128
logger.error("Failed to set up color roles.");
128129
}
129130
}
131+
132+
export async function timezonePrompt(interaction: ChatInputCommandInteraction): Promise<void> {
133+
const prompt: EmbedBuilder = new EmbedBuilder()
134+
.setTitle("Timezone roles 🌍")
135+
.setDescription(
136+
"Select the timezone you life in. That way we can schedule events at times that work for most of us :)"
137+
);
138+
139+
const actionRow: ActionRowBuilder<SelectMenuBuilder> =
140+
new ActionRowBuilder<SelectMenuBuilder>().setComponents(
141+
new SelectMenuBuilder()
142+
.setCustomId("role.timezone")
143+
.setPlaceholder("Select your timezone!")
144+
.setMinValues(1)
145+
.setMaxValues(1)
146+
.setOptions(...config.timezoneRoles.map((t) => ({ label: t, value: t })))
147+
);
148+
149+
if (
150+
await setUpRoles(
151+
interaction.guild,
152+
config.timezoneRoles.map((r) => [r]),
153+
"- StartTimezoneRoles -",
154+
"- EndTimezoneRoles -"
155+
)
156+
) {
157+
await interaction.editReply({ embeds: [addDefaultEmbedFooter(prompt)], components: [actionRow] });
158+
} else {
159+
await interaction.editReply({ content: "Couldn't set up the roles..." });
160+
logger.error("Failed to set up timezone roles.");
161+
}
162+
}

0 commit comments

Comments
 (0)