add function to fetch announcement role id
Compile the repository / compile (pull_request) Successful in 16s Details
Run unit tests / test (pull_request) Successful in 14s Details

intended flow: use id fetch function to get role for current guild, use role id to fetch role from discord role cache,
use role in intended use case
currently the role id is hardcoded, needs to be read from some sort of cache which persists the ids in config file
needs to be differentiated by guild id and role type
could be the target of a user configurable frontend in the future
This commit is contained in:
kenobi 2023-11-21 23:33:25 +01:00
parent 88061c361c
commit 417b24d408
1 changed files with 10 additions and 6 deletions

View File

@ -8,8 +8,10 @@ import { Maybe } from "../interfaces";
export default class RoleController { export default class RoleController {
constructor(private client: ExtendedClient) { } constructor(private client: ExtendedClient) { }
private getMediaRoleForGuild(guildId: string): Role { private getAnnounceRoleIdForGuild(guildId: string): string {
throw new Error("Method not implemented."); const role = config.bot.announcement_role
if (!role) throw new Error(`No announcementRole defined for guild ${guildId}`)
return role
} }
public async addRoleToUser(member: GuildMember, role: Role, guildId: string, requestId: string) { public async addRoleToUser(member: GuildMember, role: Role, guildId: string, requestId: string) {
logger.info(`Adding Role ${role.id} to user ${member.id}|${member.user.username}`, { requestId, guildId }) logger.info(`Adding Role ${role.id} to user ${member.id}|${member.user.username}`, { requestId, guildId })
@ -21,20 +23,22 @@ export default class RoleController {
} }
public async addMediaRoleToUser(user: User, guild: Guild, requestId: string) { public async addMediaRoleToUser(user: User, guild: Guild, requestId: string) {
const roleToAdd: Role = this.getMediaRoleForGuild(guild.id) const roleToAdd = await this.getAnnouncementRoleForGuild(guild, requestId)
if (!roleToAdd) throw new Error(`No announcementRole found to add to user`)
const guildMember = await guild.members.fetch(user) const guildMember = await guild.members.fetch(user)
return this.addRoleToUser(guildMember, roleToAdd, guild.id, requestId) return this.addRoleToUser(guildMember, roleToAdd, guild.id, requestId)
} }
public async removeMediaRoleFromUser(user: User, guild: Guild, requestId: string) { public async removeMediaRoleFromUser(user: User, guild: Guild, requestId: string) {
const roleToRemove: Role = this.getMediaRoleForGuild(guild.id) const roleToRemove = await this.getAnnouncementRoleForGuild(guild, requestId)
if (!roleToRemove) throw new Error(`No announcementRole found to remove from user`)
const guildMember = await guild.members.fetch(user) const guildMember = await guild.members.fetch(user)
return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId) return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId)
} }
public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise<Maybe<Role>> { public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise<Maybe<Role>> {
const mediaRole = this.getMediaRoleForGuild(guild.id) const mediaRole = this.getAnnounceRoleIdForGuild(guild.id)
return guild.roles.fetch() return guild.roles.fetch()
.then(fetchedRoles => fetchedRoles.find(role => role.id === mediaRole.id)) .then(fetchedRoles => fetchedRoles.find(role => role.id === mediaRole))
.catch(error => { logger.error(`Could not find announcement_role with id ${config.bot.announcement_role}. Error: ${error}`, { requestId, guildId: guild.id }) }) .catch(error => { logger.error(`Could not find announcement_role with id ${config.bot.announcement_role}. Error: ${error}`, { requestId, guildId: guild.id }) })
} }