add votecontroller to consolidate handling of votes
This commit is contained in:
		
							
								
								
									
										33
									
								
								server/helper/vote.controller.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								server/helper/vote.controller.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					import { Message, MessageReaction, User } from "discord.js"
 | 
				
			||||||
 | 
					import { client } from "../.."
 | 
				
			||||||
 | 
					import { NONE_OF_THAT } from "../constants"
 | 
				
			||||||
 | 
					import { logger } from "../logger"
 | 
				
			||||||
 | 
					import { messageIsVoteMessage } from "./messageIdentifiers"
 | 
				
			||||||
 | 
					import { getMembersWithRoleFromGuild } from "./roleFilter"
 | 
				
			||||||
 | 
					import { config } from "../configuration"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default class VoteController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public async handleNoneOfThatVote(messageReaction: MessageReaction, user: User, reactedUponMessage: Message, requestId: string, guildId: string) {
 | 
				
			||||||
 | 
							if (!messageReaction.message.guild) return 'No guild'
 | 
				
			||||||
 | 
							if (messageIsVoteMessage(reactedUponMessage)) {
 | 
				
			||||||
 | 
								logger.debug(`${reactedUponMessage.id} is vote message`, { requestId, guildId })
 | 
				
			||||||
 | 
								if (messageReaction.message.reactions.cache.find(reaction => reaction.emoji.toString() == NONE_OF_THAT)) {
 | 
				
			||||||
 | 
									const watcherRoleMember = await getMembersWithRoleFromGuild(config.bot.announcement_role, messageReaction.message.guild)
 | 
				
			||||||
 | 
									logger.info("ROLE MEMBERS " + JSON.stringify(watcherRoleMember), { requestId, guildId })
 | 
				
			||||||
 | 
									const watcherRoleMemberCount = watcherRoleMember.size
 | 
				
			||||||
 | 
									logger.info(`MEMBER COUNT: ${watcherRoleMemberCount}`, { requestId, guildId })
 | 
				
			||||||
 | 
									let noneOfThatReactions = messageReaction.message.reactions.cache.get(NONE_OF_THAT)?.users.cache.filter(x => x.id !== client.user?.id).size ?? 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									const memberThreshold = (watcherRoleMemberCount / 2)
 | 
				
			||||||
 | 
									logger.info(`Reroll ${noneOfThatReactions} > ${memberThreshold} ?`, { requestId, guildId })
 | 
				
			||||||
 | 
									if (noneOfThatReactions > memberThreshold) {
 | 
				
			||||||
 | 
										logger.info('Starting poll reroll', { requestId, guildId })
 | 
				
			||||||
 | 
										messageReaction.message.edit((messageReaction.message.content ?? "").concat('\nDiese Abstimmung muss wiederholt werden.'))
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									logger.info(`No reroll`, { requestId, guildId })
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -10,6 +10,7 @@ import { logger } from "../logger";
 | 
				
			|||||||
import { CommandType } from "../types/commandTypes";
 | 
					import { CommandType } from "../types/commandTypes";
 | 
				
			||||||
import { checkForPollsToClose } from "../commands/closepoll";
 | 
					import { checkForPollsToClose } from "../commands/closepoll";
 | 
				
			||||||
import { messageIsInitialAnnouncement } from "../helper/messageIdentifiers";
 | 
					import { messageIsInitialAnnouncement } from "../helper/messageIdentifiers";
 | 
				
			||||||
 | 
					import VoteController from "../helper/vote.controller";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -17,6 +18,7 @@ export class ExtendedClient extends Client {
 | 
				
			|||||||
	private eventFilePath = `${__dirname}/../events`
 | 
						private eventFilePath = `${__dirname}/../events`
 | 
				
			||||||
	private commandFilePath = `${__dirname}/../commands`
 | 
						private commandFilePath = `${__dirname}/../commands`
 | 
				
			||||||
	private jellyfin: JellyfinHandler
 | 
						private jellyfin: JellyfinHandler
 | 
				
			||||||
 | 
						public VoteController: VoteController = new VoteController()
 | 
				
			||||||
	public commands: Collection<string, CommandType> = new Collection()
 | 
						public commands: Collection<string, CommandType> = new Collection()
 | 
				
			||||||
	private announcementChannels: Collection<string, TextChannel> = new Collection() //guildId to TextChannel
 | 
						private announcementChannels: Collection<string, TextChannel> = new Collection() //guildId to TextChannel
 | 
				
			||||||
	private announcementRoleHandlerTask: Collection<string, ScheduledTask> = new Collection() //one task per guild
 | 
						private announcementRoleHandlerTask: Collection<string, ScheduledTask> = new Collection() //one task per guild
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user