From c61fafdca03c459c42b38588ed2a24c625ec1dcb Mon Sep 17 00:00:00 2001 From: liushuyu Date: Fri, 28 Apr 2023 14:29:51 +0200 Subject: [PATCH] common: extract the grantRole function --- src/commands/grantDeveloper.ts | 27 +++++---------------------- src/common.ts | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/commands/grantDeveloper.ts b/src/commands/grantDeveloper.ts index d111190..847b88d 100644 --- a/src/commands/grantDeveloper.ts +++ b/src/commands/grantDeveloper.ts @@ -1,6 +1,6 @@ -import state from '../state'; import logger from '../logging'; import * as discord from 'discord.js'; +import { grantRole } from '../common'; export const roles = ['Admins', 'Moderators', 'CitraBot']; export async function command (message: discord.Message) { @@ -12,26 +12,9 @@ export async function command (message: discord.Message) { } return Promise.all(message.mentions.users.map(async (user) => { - return message.guild?.members.fetch(user).then((member) => { - const alreadyJoined = member.roles.cache.has(role); - - if (alreadyJoined) { - member.roles.remove(role).then(async () => { - await message.channel.send(`${user.toString()}'s speech has been revoked in the #development channel.`); - }).catch(async () => { - await state.logChannel?.send(`Error revoking ${user.toString()}'s developer speech...`); - logger.error(`Error revoking ${user.toString()} ${user.username}'s developer speech...`); - }); - } else { - member.roles.add(role).then(async () => { - await message.channel.send(`${user.toString()} has been granted speech in the #development channel.`); - }).catch(async () => { - await state.logChannel?.send(`Error granting ${user.toString()}'s developer speech...`); - logger.error(`Error granting ${user.toString()} ${user.username}'s developer speech...`); - }); - } - }).catch(async () => { - await message.channel.send(`User ${user.toString()} was not found in the channel.`); - }); + return message.guild?.members.fetch(user).then((member) => grantRole(member, role, message.channel)) + .catch(async () => { + await message.channel.send(`User ${user.toString()} was not found in the channel.`); + }); })); } diff --git a/src/common.ts b/src/common.ts index a125e03..6c00d16 100644 --- a/src/common.ts +++ b/src/common.ts @@ -18,3 +18,26 @@ export async function ban (user: discord.User, moderator: discord.User, guild: d data.flushBans(); } + +export async function grantRole (member: discord.GuildMember, role: string, channel: discord.TextBasedChannel) { + const user = member.user; + const roleDisplayName = role.toLowerCase(); + + const alreadyJoined = member.roles.cache.has(role); + if (alreadyJoined) { + member.roles.remove(role).then(async () => { + await channel.send(`${user.toString()}'s ${roleDisplayName} role has been revoked.`); + }).catch(async () => { + await state.logChannel?.send(`Error revoking ${user.toString()}'s ${roleDisplayName} speech...`); + logger.error(`Error revoking ${user.toString()} ${user.username}'s ${roleDisplayName} speech...`); + }); + return; + } + + member.roles.add(role).then(async () => { + await channel.send(`${user.toString()} has been granted ${roleDisplayName} speech.`); + }).catch(async () => { + await state.logChannel?.send(`Error granting ${user.toString()}'s ${roleDisplayName} speech...`); + logger.error(`Error granting ${user.toString()} ${user.username}'s ${roleDisplayName} speech...`); + }); +}