From 1d2ae0e5a18959691696b44b204ac8d4ce2ddade Mon Sep 17 00:00:00 2001 From: Naomi Carrigan Date: Sun, 7 Jul 2024 12:28:03 -0700 Subject: [PATCH] feat: handle join roles --- prisma/schema.prisma | 1 + src/commands/config.ts | 50 +++++++++++-------- src/events/member/onMemberAdd.ts | 4 ++ .../subcommands/config/handleJoinRole.ts | 34 +++++++++++++ 4 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 src/modules/subcommands/config/handleJoinRole.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 62a52ef..be7c7c9 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -59,6 +59,7 @@ model configs { modLogChannel String @default("") eventLogChannel String @default("") messageReportChannel String @default("") + joinRole String @default("") @@unique([serverId], map: "serverId") } diff --git a/src/commands/config.ts b/src/commands/config.ts index 71c14cc..fa1e9f5 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -8,14 +8,25 @@ import { import { logChannelChoices } from "../config/LogChannelChoices"; import { Command } from "../interfaces/Command"; +import { CommandHandler } from "../interfaces/CommandHandler"; import { getConfig } from "../modules/data/getConfig"; import { handleAppealLink } from "../modules/subcommands/config/handleAppealLink"; import { handleInviteLink } from "../modules/subcommands/config/handleInviteLink"; +import { handleJoinRole } from "../modules/subcommands/config/handleJoinRole"; import { handleList } from "../modules/subcommands/config/handleList"; import { handleLogging } from "../modules/subcommands/config/handleLogging"; import { handleRole } from "../modules/subcommands/config/handleRole"; import { errorHandler } from "../utils/errorHandler"; +const handlers: { [key: string]: CommandHandler } = { + list: handleList, + "invite-link": handleInviteLink, + "appeal-link": handleAppealLink, + logging: handleLogging, + role: handleRole, + "join-role": handleJoinRole +}; + export const config: Command = { data: new SlashCommandBuilder() .setName("config") @@ -79,6 +90,17 @@ export const config: Command = { .setDescription("The role to toggle.") .setRequired(true) ) + ) + .addSubcommand( + new SlashCommandSubcommandBuilder() + .setName("join-role") + .setDescription("Configure a role to be assigned when a member joins.") + .addRoleOption((o) => + o + .setName("role") + .setDescription("The role to assign.") + .setRequired(true) + ) ), run: async (bot, interaction) => { try { @@ -105,27 +127,15 @@ export const config: Command = { const subcommand = interaction.options.getSubcommand(); - switch (subcommand) { - case "list": - await handleList(bot, interaction, config); - break; - case "logging": - await handleLogging(bot, interaction, config); - break; - case "invite-link": - await handleInviteLink(bot, interaction, config); - break; - case "appeal-link": - await handleAppealLink(bot, interaction, config); - break; - case "roles": - await handleRole(bot, interaction, config); - break; - default: - await interaction.editReply({ - content: "This is an invalid subcommand. Please contact Naomi." - }); + const handler = handlers[subcommand]; + + if (handler) { + await handler(bot, interaction, config); + return; } + await interaction.editReply({ + content: "This is an invalid subcommand. Please contact Naomi." + }); } catch (err) { const id = await errorHandler(bot, "config command", err); await interaction.editReply({ diff --git a/src/events/member/onMemberAdd.ts b/src/events/member/onMemberAdd.ts index e94fc54..258425a 100644 --- a/src/events/member/onMemberAdd.ts +++ b/src/events/member/onMemberAdd.ts @@ -17,6 +17,10 @@ export const onMemberAdd = async (bot: ExtendedClient, member: GuildMember) => { const config = await getConfig(bot, guild.id); + if (config.joinRole) { + await member.roles.add(config.joinRole).catch(() => null); + } + if (!config.eventLogChannel) { return; } diff --git a/src/modules/subcommands/config/handleJoinRole.ts b/src/modules/subcommands/config/handleJoinRole.ts new file mode 100644 index 0000000..a9915e9 --- /dev/null +++ b/src/modules/subcommands/config/handleJoinRole.ts @@ -0,0 +1,34 @@ +import { CommandHandler } from "../../../interfaces/CommandHandler"; +import { errorHandler } from "../../../utils/errorHandler"; +import { setConfig } from "../../data/setConfig"; + +/** + * Sets the role to be assigned when a member joins. + */ +export const handleJoinRole: CommandHandler = async (bot, interaction) => { + try { + const role = interaction.options.getRole("role", true); + + const success = await setConfig( + bot, + interaction.guild.id, + "joinRole", + role.id + ); + + if (success) { + await interaction.editReply({ + content: `Members will be given ${role.toString()} when they join..` + }); + return; + } + await interaction.editReply({ + content: "Failed to set the settings." + }); + } catch (err) { + const id = await errorHandler(bot, "config join-role subcommand", err); + await interaction.editReply({ + content: `Something went wrong. Please [join our support server](https://chat.naomi.lgbt) and provide this ID: \`${id}\`` + }); + } +};