2023-11-21 22:41:41 +01:00
|
|
|
import { Guild, MessageReaction, Role, User } from "discord.js";
|
2023-11-20 00:27:23 +01:00
|
|
|
import { GuildMember } from "discord.js";
|
2023-11-21 22:41:41 +01:00
|
|
|
import { logger } from "../logger";
|
|
|
|
import { config } from "../configuration";
|
|
|
|
import { Maybe } from "../interfaces";
|
2023-11-20 00:17:02 +01:00
|
|
|
|
|
|
|
export default class RoleController {
|
|
|
|
|
2023-11-22 19:51:35 +01:00
|
|
|
constructor() { }
|
2023-11-21 23:33:25 +01:00
|
|
|
private getAnnounceRoleIdForGuild(guildId: string): string {
|
|
|
|
const role = config.bot.announcement_role
|
|
|
|
if (!role) throw new Error(`No announcementRole defined for guild ${guildId}`)
|
|
|
|
return role
|
2023-11-20 00:17:02 +01:00
|
|
|
}
|
2023-11-20 00:27:23 +01:00
|
|
|
public async addRoleToUser(member: GuildMember, role: Role, guildId: string, requestId: string) {
|
2023-11-21 22:41:41 +01:00
|
|
|
logger.info(`Adding Role ${role.id} to user ${member.id}|${member.user.username}`, { requestId, guildId })
|
2023-11-20 00:27:23 +01:00
|
|
|
return await member.roles.add(role)
|
2023-11-20 00:17:02 +01:00
|
|
|
}
|
2023-11-21 22:41:41 +01:00
|
|
|
private async removeRoleFromUser(member: GuildMember, role: Role, guildId: string, requestId: string) {
|
|
|
|
logger.info(`Removing Role ${role.id} from user ${member.id}|${member.user.username}`, { requestId, guildId })
|
2023-11-20 00:27:23 +01:00
|
|
|
return await member.roles.remove(role)
|
2023-11-20 00:17:02 +01:00
|
|
|
}
|
|
|
|
|
2023-11-20 00:27:23 +01:00
|
|
|
public async addMediaRoleToUser(user: User, guild: Guild, requestId: string) {
|
2023-11-21 23:33:25 +01:00
|
|
|
const roleToAdd = await this.getAnnouncementRoleForGuild(guild, requestId)
|
|
|
|
if (!roleToAdd) throw new Error(`No announcementRole found to add to user`)
|
2023-11-20 00:27:23 +01:00
|
|
|
const guildMember = await guild.members.fetch(user)
|
|
|
|
return this.addRoleToUser(guildMember, roleToAdd, guild.id, requestId)
|
2023-11-20 00:17:02 +01:00
|
|
|
}
|
2023-11-20 00:27:23 +01:00
|
|
|
public async removeMediaRoleFromUser(user: User, guild: Guild, requestId: string) {
|
2023-11-21 23:33:25 +01:00
|
|
|
const roleToRemove = await this.getAnnouncementRoleForGuild(guild, requestId)
|
|
|
|
if (!roleToRemove) throw new Error(`No announcementRole found to remove from user`)
|
2023-11-20 00:27:23 +01:00
|
|
|
const guildMember = await guild.members.fetch(user)
|
|
|
|
return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId)
|
2023-11-20 00:17:02 +01:00
|
|
|
}
|
2023-11-21 22:41:41 +01:00
|
|
|
|
2023-11-25 20:05:37 +01:00
|
|
|
public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise<Role> {
|
2023-11-21 23:33:25 +01:00
|
|
|
const mediaRole = this.getAnnounceRoleIdForGuild(guild.id)
|
2023-11-25 20:05:37 +01:00
|
|
|
const announcement_role = await guild.roles.fetch()
|
2023-11-21 23:33:25 +01:00
|
|
|
.then(fetchedRoles => fetchedRoles.find(role => role.id === mediaRole))
|
2023-11-25 20:05:37 +01:00
|
|
|
.catch(error => {
|
|
|
|
logger.error(`Could not find announcement_role with id ${config.bot.announcement_role}. Error: ${error}`, { requestId, guildId: guild.id })
|
|
|
|
throw error
|
|
|
|
})
|
|
|
|
if (!announcement_role) throw new Error(`Could not find announcement_role with id ${config.bot.announcement_role}.`)
|
|
|
|
return announcement_role
|
2023-11-21 22:41:41 +01:00
|
|
|
}
|
|
|
|
|
2023-11-25 19:50:54 +01:00
|
|
|
public async assignAnnouncementRolesFromReaction(guild: Guild, reaction: MessageReaction, requestId: string) {
|
2023-11-21 22:41:41 +01:00
|
|
|
const guildId = guild.id
|
|
|
|
logger.info("Managing roles", { guildId, requestId })
|
|
|
|
|
|
|
|
const announcementRole = await this.getAnnouncementRoleForGuild(guild, requestId)
|
|
|
|
|
|
|
|
const usersWhoWantRole: User[] = (await reaction.users.fetch()).filter(user => !user.bot).map(user => user)
|
|
|
|
|
|
|
|
const allUsers = await guild.members.fetch()
|
|
|
|
|
|
|
|
const usersWhoHaveRole: GuildMember[] = allUsers
|
|
|
|
.filter(member => member.roles.cache
|
|
|
|
.find(role => role.id === announcementRole.id) !== undefined)
|
|
|
|
.map(member => member)
|
|
|
|
|
|
|
|
const usersWhoNeedRoleRevoked: GuildMember[] = usersWhoHaveRole
|
|
|
|
.filter(userWhoHas => !usersWhoWantRole.map(wanter => wanter.id).includes(userWhoHas.id))
|
|
|
|
|
|
|
|
const usersWhoDontHaveRole: GuildMember[] = allUsers
|
|
|
|
.filter(member => member.roles.cache
|
|
|
|
.find(role => role.id === announcementRole.id) === undefined)
|
|
|
|
.map(member => member)
|
|
|
|
|
|
|
|
const usersWhoNeedRole: GuildMember[] = usersWhoDontHaveRole
|
|
|
|
.filter(userWhoNeeds => usersWhoWantRole.map(wanter => wanter.id).includes(userWhoNeeds.id))
|
|
|
|
|
|
|
|
|
|
|
|
logger.debug(`Theses users will get the role removed: ${JSON.stringify(usersWhoNeedRoleRevoked)}`, { guildId, requestId })
|
|
|
|
logger.debug(`Theses users will get the role added: ${JSON.stringify(usersWhoNeedRole)}`, { guildId, requestId })
|
|
|
|
|
|
|
|
usersWhoNeedRoleRevoked.forEach(user => this.removeRoleFromUser(user, announcementRole, guild.id, requestId))
|
|
|
|
usersWhoNeedRole.forEach(user => this.addRoleToUser(user, announcementRole, guild.id, requestId))
|
|
|
|
}
|
|
|
|
|
2023-11-20 00:17:02 +01:00
|
|
|
}
|