Skip to content

Commit

Permalink
Add migrate-members-with-role command (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
RafDevX authored Sep 13, 2022
1 parent f0a7685 commit 0656b35
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/modules/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
},
];
}

Expand Down Expand Up @@ -184,3 +211,38 @@ export async function handleJustAskCommand(
await interaction.editReply(utils.XEmoji + "Something went wrong.");
}
}

export async function handleMigrateMembersWithRole(
interaction: Discord.CommandInteraction
): Promise<void> {
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.");
}
}

0 comments on commit 0656b35

Please sign in to comment.