make return value for role non optional, instead throws on error
Compile the repository / compile (pull_request) Successful in 22s Details
Run unit tests / test (pull_request) Successful in 19s Details

This commit is contained in:
kenobi 2023-11-25 20:05:37 +01:00
parent ef39c6315d
commit a50ac1716f
1 changed files with 8 additions and 5 deletions

View File

@ -34,11 +34,16 @@ export default class RoleController {
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<Role> {
const mediaRole = this.getAnnounceRoleIdForGuild(guild.id) const mediaRole = this.getAnnounceRoleIdForGuild(guild.id)
return guild.roles.fetch() const announcement_role = await guild.roles.fetch()
.then(fetchedRoles => fetchedRoles.find(role => role.id === mediaRole)) .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 })
throw error
})
if (!announcement_role) throw new Error(`Could not find announcement_role with id ${config.bot.announcement_role}.`)
return announcement_role
} }
public async assignAnnouncementRolesFromReaction(guild: Guild, reaction: MessageReaction, requestId: string) { public async assignAnnouncementRolesFromReaction(guild: Guild, reaction: MessageReaction, requestId: string) {
@ -46,8 +51,6 @@ export default class RoleController {
logger.info("Managing roles", { guildId, requestId }) logger.info("Managing roles", { guildId, requestId })
const announcementRole = await this.getAnnouncementRoleForGuild(guild, requestId) const announcementRole = await this.getAnnouncementRoleForGuild(guild, requestId)
if (!announcementRole)
throw new Error(`No announcementRole found in guild ${guildId}`)
const usersWhoWantRole: User[] = (await reaction.users.fetch()).filter(user => !user.bot).map(user => user) const usersWhoWantRole: User[] = (await reaction.users.fetch()).filter(user => !user.bot).map(user => user)