common: extract the grantRole function

This commit is contained in:
liushuyu 2023-04-28 14:29:51 +02:00
parent ffb697c323
commit c61fafdca0
2 changed files with 28 additions and 22 deletions

View File

@ -1,6 +1,6 @@
import state from '../state';
import logger from '../logging'; import logger from '../logging';
import * as discord from 'discord.js'; import * as discord from 'discord.js';
import { grantRole } from '../common';
export const roles = ['Admins', 'Moderators', 'CitraBot']; export const roles = ['Admins', 'Moderators', 'CitraBot'];
export async function command (message: discord.Message) { export async function command (message: discord.Message) {
@ -12,25 +12,8 @@ export async function command (message: discord.Message) {
} }
return Promise.all(message.mentions.users.map(async (user) => { return Promise.all(message.mentions.users.map(async (user) => {
return message.guild?.members.fetch(user).then((member) => { return message.guild?.members.fetch(user).then((member) => grantRole(member, role, message.channel))
const alreadyJoined = member.roles.cache.has(role); .catch(async () => {
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.`); await message.channel.send(`User ${user.toString()} was not found in the channel.`);
}); });
})); }));

View File

@ -18,3 +18,26 @@ export async function ban (user: discord.User, moderator: discord.User, guild: d
data.flushBans(); 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...`);
});
}