From 0656b3552fde3c21ed64b6e780c23483fa94efbb Mon Sep 17 00:00:00 2001 From: Rafael Oliveira <56204853+RafDevX@users.noreply.github.com> Date: Tue, 13 Sep 2022 18:13:53 +0100 Subject: [PATCH] Add `migrate-members-with-role` command (#95) --- src/modules/misc.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/modules/misc.ts b/src/modules/misc.ts index 5dde6c0..f1d95f5 100644 --- a/src/modules/misc.ts +++ b/src/modules/misc.ts @@ -43,6 +43,27 @@ export function provideCommands(): CommandDescriptor[] { .setDescription("Message ID in question") .setRequired(true) ); + const migrateMembersWithRole = new SlashCommandBuilder() + .setName("migrate-members-with-role") + .setDescription("Gives a role to everyone who has a certain role"); + migrateMembersWithRole.addRoleOption( + new Builders.SlashCommandRoleOption() + .setName("old-role") + .setDescription("The role to migrate members from") + .setRequired(true) + ); + migrateMembersWithRole.addRoleOption( + new Builders.SlashCommandRoleOption() + .setName("new-role") + .setDescription("The role to migrate members to") + .setRequired(true) + ); + migrateMembersWithRole.addBooleanOption( + new Builders.SlashCommandBooleanOption() + .setName("remove-old") + .setDescription("Whether to remove the old role") + .setRequired(false) + ); return [ { builder: new Builders.SlashCommandBuilder() @@ -62,10 +83,16 @@ export function provideCommands(): CommandDescriptor[] { { builder: new Builders.SlashCommandBuilder() .setName("just-ask") - .setDescription("Send a link to the \"Don't ask to ask\" website"), + .setDescription( + 'Send a link to the "Don\'t ask to ask" website' + ), handler: handleJustAskCommand, permission: CommandPermission.Public, }, + { + builder: migrateMembersWithRole, + handler: handleMigrateMembersWithRole, + }, ]; } @@ -184,3 +211,38 @@ export async function handleJustAskCommand( await interaction.editReply(utils.XEmoji + "Something went wrong."); } } + +export async function handleMigrateMembersWithRole( + interaction: Discord.CommandInteraction +): Promise { + try { + const oldRole = interaction.options.getRole( + "old-role", + true + ) as Discord.Role; + const newRole = interaction.options.getRole( + "new-role", + true + ) as Discord.Role; + const removeOld = interaction.options.getBoolean("remove-old", false); + + let count = 0; + oldRole.members.forEach((member) => { + member.roles.add(newRole); + + if (removeOld) { + member.roles.remove(oldRole); + } + + count++; + }); + + await interaction.editReply( + utils.CheckMarkEmoji + + `Migrated ${count} members from ${oldRole} to ${newRole}` + ); + } catch (e) { + console.error(e); + await interaction.editReply(utils.XEmoji + "Something went wrong."); + } +}