From 8efae12907a21507777c58acf7d3efd661c5e16c Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 20 Nov 2023 00:03:45 +0100 Subject: [PATCH 01/16] add hook --- server/events/handleMessageReactionAdd.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/events/handleMessageReactionAdd.ts b/server/events/handleMessageReactionAdd.ts index 38cb212..81141b6 100644 --- a/server/events/handleMessageReactionAdd.ts +++ b/server/events/handleMessageReactionAdd.ts @@ -25,6 +25,10 @@ export async function execute(messageReaction: MessageReaction, user: User) { //logger.debug(`reactedUponMessage payload: ${JSON.stringify(reactedUponMessage)}`) logger.info(`emoji: ${messageReaction.emoji.toString()}`) + if (messageReaction.emoji.toString() === Emoji.ticket) { + logger.info(`User: ${user.id}, ${user.username} has added a ticket reaction. Starting role management`, { requestId, guildId }) + return + } if (!Object.values(ValidVoteEmotes).includes(messageReaction.emoji.toString()) && messageReaction.emoji.toString() !== NONE_OF_THAT) { logger.info(`${messageReaction.emoji.toString()} currently not handled`) -- 2.45.2 From ba4aefed8ec030ee2cdd07328eb8dfd295bffdc2 Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 20 Nov 2023 00:17:02 +0100 Subject: [PATCH 02/16] add a minimal vote controller --- server/helper/role.controller.ts | 25 +++++++++++++++++++++++++ server/structures/client.ts | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 server/helper/role.controller.ts diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts new file mode 100644 index 0000000..63903cd --- /dev/null +++ b/server/helper/role.controller.ts @@ -0,0 +1,25 @@ +import { Role, User } from "discord.js"; +import { ExtendedClient } from "../structures/client"; + +export default class RoleController { + + constructor(private client: ExtendedClient) { } + private getMediaRoleForGuild(guildId: string): Role { + throw new Error("Method not implemented."); + } + public addRoleToUser(user: User, role: Role, guildId: string, requestId: string) { + throw new Error("Method not implemented."); + } + private removeRoleFromUser(user: User, roleToAdd: any, guildId: string, requestId: string) { + throw new Error("Method not implemented."); + } + + public addMediaRoleToUser(user: User, guildId: string, requestId: string) { + const roleToAdd: Role = this.getMediaRoleForGuild(guildId) + return this.addRoleToUser(user, roleToAdd, guildId, requestId) + } + public removeMediaRoleFromUser(user: User, guildId: string, requestId: string) { + const roleToRemove: Role = this.getMediaRoleForGuild(guildId) + return this.removeRoleFromUser(user, roleToRemove, guildId, requestId) + } +} diff --git a/server/structures/client.ts b/server/structures/client.ts index caaad67..a3ccfcb 100644 --- a/server/structures/client.ts +++ b/server/structures/client.ts @@ -11,6 +11,7 @@ import { CommandType } from "../types/commandTypes"; import { isInitialAnnouncement } from "../helper/messageIdentifiers"; import VoteController from "../helper/vote.controller"; import { yavinJellyfinHandler } from "../.."; +import RoleController from "../helper/role.controller"; @@ -19,6 +20,7 @@ export class ExtendedClient extends Client { private commandFilePath = `${__dirname}/../commands` private jellyfin: JellyfinHandler public voteController: VoteController = new VoteController(this, yavinJellyfinHandler) + public roleController: RoleController = new RoleController(this) public commands: Collection = new Collection() private announcementChannels: Collection = new Collection() //guildId to TextChannel private announcementRoleHandlerTask: Collection = new Collection() //one task per guild -- 2.45.2 From 2c8cd96ac7a7644cb6c3e48610aa13e5b9b6c7e6 Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 20 Nov 2023 00:17:19 +0100 Subject: [PATCH 03/16] call vote controller in messageReactionAdd --- server/events/handleMessageReactionAdd.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/server/events/handleMessageReactionAdd.ts b/server/events/handleMessageReactionAdd.ts index 81141b6..c94c3f6 100644 --- a/server/events/handleMessageReactionAdd.ts +++ b/server/events/handleMessageReactionAdd.ts @@ -22,13 +22,8 @@ export async function execute(messageReaction: MessageReaction, user: User) { } logger.info(`Got reaction on message`, { requestId, guildId }) - //logger.debug(`reactedUponMessage payload: ${JSON.stringify(reactedUponMessage)}`) logger.info(`emoji: ${messageReaction.emoji.toString()}`) - if (messageReaction.emoji.toString() === Emoji.ticket) { - logger.info(`User: ${user.id}, ${user.username} has added a ticket reaction. Starting role management`, { requestId, guildId }) - return - } if (!Object.values(ValidVoteEmotes).includes(messageReaction.emoji.toString()) && messageReaction.emoji.toString() !== NONE_OF_THAT) { logger.info(`${messageReaction.emoji.toString()} currently not handled`) @@ -43,7 +38,8 @@ export async function execute(messageReaction: MessageReaction, user: User) { } else if (isInitialAnnouncement(reactedUponMessage)) { if (messageReaction.emoji.toString() === Emoji.ticket) { - logger.error(`Got a role emoji. Not implemented yet. ${reactedUponMessage.id}`) + logger.error(`Got a role emoji. ${reactedUponMessage.id}`) + return client.roleController.addMediaRoleToUser(user, guildId, requestId) } return } -- 2.45.2 From 016bb243ccb8d20cf6be99504d121cd96335d908 Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 20 Nov 2023 00:17:28 +0100 Subject: [PATCH 04/16] add messageReactionRemove --- server/events/handleMessageReactionRemove.ts | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 server/events/handleMessageReactionRemove.ts diff --git a/server/events/handleMessageReactionRemove.ts b/server/events/handleMessageReactionRemove.ts new file mode 100644 index 0000000..3e5b137 --- /dev/null +++ b/server/events/handleMessageReactionRemove.ts @@ -0,0 +1,33 @@ + +import { Message, MessageReaction, User } from "discord.js"; +import { logger, newRequestId, noGuildId } from "../logger"; +import { Emoji } from "../constants"; +import { client } from "../.."; +import { isInitialAnnouncement } from "../helper/messageIdentifiers"; + + +export const name = 'messageReactionRemove' + +export async function execute(messageReaction: MessageReaction, user: User) { + if (user.id == client.user?.id) { + logger.info('Skipping bot reaction') + return + } + const requestId = newRequestId() + const guildId = messageReaction.message.inGuild() ? messageReaction.message.guildId : noGuildId + const reactedUponMessage: Message = messageReaction.message.partial ? await messageReaction.message.fetch() : messageReaction.message + if (!messageReaction.message.guild) { + logger.warn(`Received messageReactionRemove on non-guild message.`, { requestId }) + return + } + + logger.info(`Got reaction on message`, { requestId, guildId }) + + logger.info(`emoji: ${messageReaction.emoji.toString()}`) + if (isInitialAnnouncement(reactedUponMessage)) { + if (messageReaction.emoji.toString() === Emoji.ticket) { + logger.info(`User: ${user.id}, ${user.username} has removed a ticket reaction. Starting role management`, { requestId, guildId }) + return client.roleController.removeMediaRoleFromUser(user, guildId, requestId) + } + } +} -- 2.45.2 From 8f320cee5c64dece196eb6733366858c35df2742 Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 20 Nov 2023 00:27:23 +0100 Subject: [PATCH 05/16] implement very basic functionality of role add/rm --- server/helper/role.controller.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts index 63903cd..6584786 100644 --- a/server/helper/role.controller.ts +++ b/server/helper/role.controller.ts @@ -1,5 +1,6 @@ -import { Role, User } from "discord.js"; +import { Guild, Role, User } from "discord.js"; import { ExtendedClient } from "../structures/client"; +import { GuildMember } from "discord.js"; export default class RoleController { @@ -7,19 +8,21 @@ export default class RoleController { private getMediaRoleForGuild(guildId: string): Role { throw new Error("Method not implemented."); } - public addRoleToUser(user: User, role: Role, guildId: string, requestId: string) { - throw new Error("Method not implemented."); + public async addRoleToUser(member: GuildMember, role: Role, guildId: string, requestId: string) { + return await member.roles.add(role) } - private removeRoleFromUser(user: User, roleToAdd: any, guildId: string, requestId: string) { - throw new Error("Method not implemented."); + private async removeRoleFromUser(member: GuildMember, role: any, guildId: string, requestId: string) { + return await member.roles.remove(role) } - public addMediaRoleToUser(user: User, guildId: string, requestId: string) { - const roleToAdd: Role = this.getMediaRoleForGuild(guildId) - return this.addRoleToUser(user, roleToAdd, guildId, requestId) + public async addMediaRoleToUser(user: User, guild: Guild, requestId: string) { + const roleToAdd: Role = this.getMediaRoleForGuild(guild.id) + const guildMember = await guild.members.fetch(user) + return this.addRoleToUser(guildMember, roleToAdd, guild.id, requestId) } - public removeMediaRoleFromUser(user: User, guildId: string, requestId: string) { - const roleToRemove: Role = this.getMediaRoleForGuild(guildId) - return this.removeRoleFromUser(user, roleToRemove, guildId, requestId) + public async removeMediaRoleFromUser(user: User, guild: Guild, requestId: string) { + const roleToRemove: Role = this.getMediaRoleForGuild(guild.id) + const guildMember = await guild.members.fetch(user) + return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId) } } -- 2.45.2 From 6d0eaed426b1446f1f8f7fb9ca6f20358b3da16b Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 20 Nov 2023 00:31:16 +0100 Subject: [PATCH 06/16] fix incorrect method call --- server/events/handleMessageReactionAdd.ts | 2 +- server/events/handleMessageReactionRemove.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/events/handleMessageReactionAdd.ts b/server/events/handleMessageReactionAdd.ts index c94c3f6..03f46b8 100644 --- a/server/events/handleMessageReactionAdd.ts +++ b/server/events/handleMessageReactionAdd.ts @@ -39,7 +39,7 @@ export async function execute(messageReaction: MessageReaction, user: User) { else if (isInitialAnnouncement(reactedUponMessage)) { if (messageReaction.emoji.toString() === Emoji.ticket) { logger.error(`Got a role emoji. ${reactedUponMessage.id}`) - return client.roleController.addMediaRoleToUser(user, guildId, requestId) + return client.roleController.addMediaRoleToUser(user, messageReaction.message.guild, requestId) } return } diff --git a/server/events/handleMessageReactionRemove.ts b/server/events/handleMessageReactionRemove.ts index 3e5b137..3196e28 100644 --- a/server/events/handleMessageReactionRemove.ts +++ b/server/events/handleMessageReactionRemove.ts @@ -27,7 +27,7 @@ export async function execute(messageReaction: MessageReaction, user: User) { if (isInitialAnnouncement(reactedUponMessage)) { if (messageReaction.emoji.toString() === Emoji.ticket) { logger.info(`User: ${user.id}, ${user.username} has removed a ticket reaction. Starting role management`, { requestId, guildId }) - return client.roleController.removeMediaRoleFromUser(user, guildId, requestId) + return client.roleController.removeMediaRoleFromUser(user, messageReaction.message.guild, requestId) } } } -- 2.45.2 From 90b0b070804ce0a6efdb7034d0375bd4ca27fece Mon Sep 17 00:00:00 2001 From: kenobi Date: Tue, 21 Nov 2023 22:40:53 +0100 Subject: [PATCH 07/16] extend Maybe type to also include void --- server/interfaces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/interfaces.ts b/server/interfaces.ts index a6fc009..cfa6a4f 100644 --- a/server/interfaces.ts +++ b/server/interfaces.ts @@ -1,7 +1,7 @@ import { Collection } from "@discordjs/collection" import { GuildScheduledEvent, Role, TextChannel } from "discord.js" -export type Maybe = T | undefined | null +export type Maybe = T | undefined | null | void export interface Player { name: string } -- 2.45.2 From f83f54749dfc4f87a542d3498460951199d57396 Mon Sep 17 00:00:00 2001 From: kenobi Date: Tue, 21 Nov 2023 22:41:41 +0100 Subject: [PATCH 08/16] move manage announce roles to role controller --- server/commands/announce.ts | 36 ---------------------- server/helper/role.controller.ts | 53 ++++++++++++++++++++++++++++++-- server/structures/client.ts | 3 +- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/server/commands/announce.ts b/server/commands/announce.ts index 80472d5..9f9d68c 100644 --- a/server/commands/announce.ts +++ b/server/commands/announce.ts @@ -82,41 +82,5 @@ Für eine Erklärung wie das alles funktioniert mach einfach /mitgucken für ein } -export async function manageAnnouncementRoles(guild: Guild, reaction: MessageReaction, requestId: string) { - const guildId = guild.id - logger.info("Managing roles", { guildId, requestId }) - - const announcementRole: Role | undefined = (await guild.roles.fetch()).find(role => role.id === config.bot.announcement_role) - if (!announcementRole) { - logger.error(`Could not find announcement role! Aborting! Was looking for role with id: ${config.bot.announcement_role}`, { guildId, requestId }) - return - } - - 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 === config.bot.announcement_role) !== 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 === config.bot.announcement_role) === 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 => user.roles.remove(announcementRole)) - usersWhoNeedRole.forEach(user => user.roles.add(announcementRole)) -} - diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts index 6584786..788f2b4 100644 --- a/server/helper/role.controller.ts +++ b/server/helper/role.controller.ts @@ -1,6 +1,9 @@ -import { Guild, Role, User } from "discord.js"; +import { Guild, MessageReaction, Role, User } from "discord.js"; import { ExtendedClient } from "../structures/client"; import { GuildMember } from "discord.js"; +import { logger } from "../logger"; +import { config } from "../configuration"; +import { Maybe } from "../interfaces"; export default class RoleController { @@ -9,9 +12,11 @@ export default class RoleController { throw new Error("Method not implemented."); } 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 }) return await member.roles.add(role) } - private async removeRoleFromUser(member: GuildMember, role: any, guildId: string, requestId: string) { + 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 }) return await member.roles.remove(role) } @@ -25,4 +30,48 @@ export default class RoleController { const guildMember = await guild.members.fetch(user) return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId) } + + public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise> { + const mediaRole = this.getMediaRoleForGuild(guild.id) + return guild.roles.fetch() + .then(fetchedRoles => fetchedRoles.find(role => role.id === mediaRole.id)) + .catch(error => { logger.error(`Could not find announcement_role with id ${config.bot.announcement_role}. Error: ${error}`, { requestId, guildId: guild.id }) }) + } + + public async manageAnnouncementRoles(guild: Guild, reaction: MessageReaction, requestId: string) { + const guildId = guild.id + logger.info("Managing roles", { guildId, 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 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)) + } + } diff --git a/server/structures/client.ts b/server/structures/client.ts index a3ccfcb..60fdedc 100644 --- a/server/structures/client.ts +++ b/server/structures/client.ts @@ -2,7 +2,6 @@ import { ApplicationCommandDataResolvable, Client, ClientOptions, Collection, Gu import fs from 'fs'; import { ScheduledTask, schedule } from "node-cron"; import { v4 as uuid } from 'uuid'; -import { manageAnnouncementRoles } from "../commands/announce"; import { config } from "../configuration"; import { Maybe } from "../interfaces"; import { JellyfinHandler } from "../jellyfin/handler"; @@ -177,7 +176,7 @@ export class ExtendedClient extends Client { const reactions = message.reactions.resolve("🎫") //logger.debug(`reactions: ${JSON.stringify(reactions, null, 2)}`, { guildId: guild.id, requestId }) if (reactions) { - manageAnnouncementRoles(message.guild, reactions, requestId) + this.roleController.manageAnnouncementRoles(message.guild, reactions, requestId) } else { logger.error("Did not get reactions! Aborting!", { guildId: guild.id, requestId }) } -- 2.45.2 From 88061c361cd5596f89dadc5334c7687e8646bcba Mon Sep 17 00:00:00 2001 From: kenobi Date: Tue, 21 Nov 2023 22:42:50 +0100 Subject: [PATCH 09/16] rename announcement role assign function --- server/helper/role.controller.ts | 2 +- server/structures/client.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts index 788f2b4..94d9b58 100644 --- a/server/helper/role.controller.ts +++ b/server/helper/role.controller.ts @@ -38,7 +38,7 @@ export default class RoleController { .catch(error => { logger.error(`Could not find announcement_role with id ${config.bot.announcement_role}. Error: ${error}`, { requestId, guildId: guild.id }) }) } - public async manageAnnouncementRoles(guild: Guild, reaction: MessageReaction, requestId: string) { + public async assignAnnouncementRolesFromReactions(guild: Guild, reaction: MessageReaction, requestId: string) { const guildId = guild.id logger.info("Managing roles", { guildId, requestId }) diff --git a/server/structures/client.ts b/server/structures/client.ts index 60fdedc..1963f92 100644 --- a/server/structures/client.ts +++ b/server/structures/client.ts @@ -176,7 +176,7 @@ export class ExtendedClient extends Client { const reactions = message.reactions.resolve("🎫") //logger.debug(`reactions: ${JSON.stringify(reactions, null, 2)}`, { guildId: guild.id, requestId }) if (reactions) { - this.roleController.manageAnnouncementRoles(message.guild, reactions, requestId) + this.roleController.assignAnnouncementRolesFromReactions(message.guild, reactions, requestId) } else { logger.error("Did not get reactions! Aborting!", { guildId: guild.id, requestId }) } -- 2.45.2 From 417b24d4088458ccdf2dca269a9f89babff70506 Mon Sep 17 00:00:00 2001 From: kenobi Date: Tue, 21 Nov 2023 23:33:25 +0100 Subject: [PATCH 10/16] add function to fetch announcement role id 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 --- server/helper/role.controller.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts index 94d9b58..4011d1e 100644 --- a/server/helper/role.controller.ts +++ b/server/helper/role.controller.ts @@ -8,8 +8,10 @@ import { Maybe } from "../interfaces"; export default class RoleController { constructor(private client: ExtendedClient) { } - private getMediaRoleForGuild(guildId: string): Role { - throw new Error("Method not implemented."); + private getAnnounceRoleIdForGuild(guildId: string): string { + 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) { 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) { - 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) return this.addRoleToUser(guildMember, roleToAdd, guild.id, requestId) } 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) return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId) } public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise> { - const mediaRole = this.getMediaRoleForGuild(guild.id) + const mediaRole = this.getAnnounceRoleIdForGuild(guild.id) 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 }) }) } -- 2.45.2 From 1ae8278fb85fd376d123151cdb89dbfab37d0dc5 Mon Sep 17 00:00:00 2001 From: kenobi Date: Wed, 22 Nov 2023 19:51:35 +0100 Subject: [PATCH 11/16] remove unused client --- server/helper/role.controller.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts index 4011d1e..462d927 100644 --- a/server/helper/role.controller.ts +++ b/server/helper/role.controller.ts @@ -1,5 +1,4 @@ import { Guild, MessageReaction, Role, User } from "discord.js"; -import { ExtendedClient } from "../structures/client"; import { GuildMember } from "discord.js"; import { logger } from "../logger"; import { config } from "../configuration"; @@ -7,7 +6,7 @@ import { Maybe } from "../interfaces"; export default class RoleController { - constructor(private client: ExtendedClient) { } + constructor() { } private getAnnounceRoleIdForGuild(guildId: string): string { const role = config.bot.announcement_role if (!role) throw new Error(`No announcementRole defined for guild ${guildId}`) -- 2.45.2 From d1aacbb3d3dfde559581821e2dcabfeab3e8ae90 Mon Sep 17 00:00:00 2001 From: kenobi Date: Wed, 22 Nov 2023 19:56:20 +0100 Subject: [PATCH 12/16] fix incorrect parameter passing --- server/structures/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/structures/client.ts b/server/structures/client.ts index 1963f92..186b41d 100644 --- a/server/structures/client.ts +++ b/server/structures/client.ts @@ -19,7 +19,7 @@ export class ExtendedClient extends Client { private commandFilePath = `${__dirname}/../commands` private jellyfin: JellyfinHandler public voteController: VoteController = new VoteController(this, yavinJellyfinHandler) - public roleController: RoleController = new RoleController(this) + public roleController: RoleController = new RoleController() public commands: Collection = new Collection() private announcementChannels: Collection = new Collection() //guildId to TextChannel private announcementRoleHandlerTask: Collection = new Collection() //one task per guild -- 2.45.2 From 1f372b0aacd94d6eb5cfbe2e72f61dfdf77c1a31 Mon Sep 17 00:00:00 2001 From: kenobi Date: Sat, 25 Nov 2023 19:50:54 +0100 Subject: [PATCH 13/16] fix naming for reaction --- server/helper/role.controller.ts | 2 +- server/structures/client.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts index 462d927..402103d 100644 --- a/server/helper/role.controller.ts +++ b/server/helper/role.controller.ts @@ -41,7 +41,7 @@ export default class RoleController { .catch(error => { logger.error(`Could not find announcement_role with id ${config.bot.announcement_role}. Error: ${error}`, { requestId, guildId: guild.id }) }) } - public async assignAnnouncementRolesFromReactions(guild: Guild, reaction: MessageReaction, requestId: string) { + public async assignAnnouncementRolesFromReaction(guild: Guild, reaction: MessageReaction, requestId: string) { const guildId = guild.id logger.info("Managing roles", { guildId, requestId }) diff --git a/server/structures/client.ts b/server/structures/client.ts index 186b41d..8f0c771 100644 --- a/server/structures/client.ts +++ b/server/structures/client.ts @@ -173,10 +173,10 @@ export class ExtendedClient extends Client { } //logger.debug(`Message: ${JSON.stringify(message, null, 2)}`, { guildId: guild.id, requestId }) - const reactions = message.reactions.resolve("🎫") + const ticketReaction = message.reactions.resolve("🎫") //logger.debug(`reactions: ${JSON.stringify(reactions, null, 2)}`, { guildId: guild.id, requestId }) - if (reactions) { - this.roleController.assignAnnouncementRolesFromReactions(message.guild, reactions, requestId) + if (ticketReaction) { + this.roleController.assignAnnouncementRolesFromReaction(message.guild, ticketReaction, requestId) } else { logger.error("Did not get reactions! Aborting!", { guildId: guild.id, requestId }) } -- 2.45.2 From ef39c6315df101186308301794e18d2a6959e84c Mon Sep 17 00:00:00 2001 From: kenobi Date: Sat, 25 Nov 2023 19:51:02 +0100 Subject: [PATCH 14/16] fix interface name and import --- server/helper/vote.controller.ts | 2 +- server/interfaces.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/helper/vote.controller.ts b/server/helper/vote.controller.ts index 064aff1..f3d82af 100644 --- a/server/helper/vote.controller.ts +++ b/server/helper/vote.controller.ts @@ -5,7 +5,7 @@ import { getMembersWithRoleFromGuild } from "./roleFilter" import { config } from "../configuration" import { VoteMessage, isVoteEndedMessage, isVoteMessage } from "./messageIdentifiers" import { createDateStringFromEvent } from "./dateHelper" -import { Maybe, voteMessageInputInformation as prepareVoteMessageInput } from "../interfaces" +import { Maybe, prepareVoteMessageInput } from "../interfaces" import format from "date-fns/format" import toDate from "date-fns/toDate" import differenceInDays from "date-fns/differenceInDays" diff --git a/server/interfaces.ts b/server/interfaces.ts index cfa6a4f..d8db7b5 100644 --- a/server/interfaces.ts +++ b/server/interfaces.ts @@ -39,7 +39,7 @@ export interface JellyfinConfig { collectionUser: string } export type PermissionLevel = "VIEWER" | "ADMIN" | "TEMPORARY" -export interface voteMessageInputInformation { +export interface prepareVoteMessageInput { movies: string[], startDate: Date, event: GuildScheduledEvent, -- 2.45.2 From a50ac1716f5eccf38d88763742d26e573b460dca Mon Sep 17 00:00:00 2001 From: kenobi Date: Sat, 25 Nov 2023 20:05:37 +0100 Subject: [PATCH 15/16] make return value for role non optional, instead throws on error --- server/helper/role.controller.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/server/helper/role.controller.ts b/server/helper/role.controller.ts index 402103d..8cb9dc2 100644 --- a/server/helper/role.controller.ts +++ b/server/helper/role.controller.ts @@ -34,11 +34,16 @@ export default class RoleController { return this.removeRoleFromUser(guildMember, roleToRemove, guild.id, requestId) } - public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise> { + public async getAnnouncementRoleForGuild(guild: Guild, requestId: string): Promise { 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)) - .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) { @@ -46,8 +51,6 @@ export default class RoleController { logger.info("Managing roles", { guildId, 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) -- 2.45.2 From a60fc2db7e8736a0899d07f0bc797ba84da1e861 Mon Sep 17 00:00:00 2001 From: kenobi Date: Sat, 25 Nov 2023 20:35:15 +0100 Subject: [PATCH 16/16] remove unnecessary maybe type union --- server/interfaces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/interfaces.ts b/server/interfaces.ts index d8db7b5..b512417 100644 --- a/server/interfaces.ts +++ b/server/interfaces.ts @@ -1,7 +1,7 @@ import { Collection } from "@discordjs/collection" import { GuildScheduledEvent, Role, TextChannel } from "discord.js" -export type Maybe = T | undefined | null | void +export type Maybe = T | undefined | null export interface Player { name: string } -- 2.45.2