move preparation of vote Message sending into vote controller
All checks were successful
Compile the repository / compile (pull_request) Successful in 17s
Run unit tests / test (pull_request) Successful in 13s

event only needs to supply information, text creation, sending and pinning happens in the vote controller
This commit is contained in:
kenobi 2023-11-18 17:28:44 +01:00
parent 4a3e8809be
commit 4600820889
3 changed files with 29 additions and 8 deletions

View File

@ -4,7 +4,6 @@ import { client, yavinJellyfinHandler } from "../..";
import { Maybe } from "../interfaces";
import { logger } from "../logger";
export const name = 'guildScheduledEventCreate'
export async function execute(event: GuildScheduledEvent) {
@ -25,15 +24,21 @@ export async function execute(event: GuildScheduledEvent) {
return
}
logger.debug(`Found channel ${JSON.stringify(announcementChannel, null, 2)}`, { guildId: event.guildId, requestId })
if (!event.scheduledStartAt) {
logger.info("EVENT DOES NOT HAVE STARTDATE; CANCELLING", { guildId: event.guildId, requestId })
logger.info("Event does not have a start date, cancelling", { guildId: event.guildId, requestId })
return
}
const sentMessageText = client.voteController.createVoteMessageText(event.id, event.scheduledStartAt, movies, event.guild?.id ?? "", requestId)
const sentMessage = await client.voteController.sendVoteMessage(sentMessageText, movies.length, announcementChannel)
const sentMessage = await client.voteController.prepareAndSendVoteMessage({
movies,
startDate: event.scheduledStartAt,
event,
announcementChannel,
pinAfterSending: true
},
event.guildId,
requestId)
sentMessage.pin()
logger.debug(JSON.stringify(sentMessage))
}
}

View File

@ -5,7 +5,7 @@ import { getMembersWithRoleFromGuild } from "./roleFilter"
import { config } from "../configuration"
import { VoteMessage, isVoteEndedMessage, isVoteMessage } from "./messageIdentifiers"
import { createDateStringFromEvent } from "./dateHelper"
import { Maybe } from "../interfaces"
import { Maybe, voteMessageInputInformation as prepareVoteMessageInput } from "../interfaces"
import format from "date-fns/format"
import toDate from "date-fns/toDate"
import differenceInDays from "date-fns/differenceInDays"
@ -161,6 +161,14 @@ export default class VoteController {
throw Error(`Could not find eventId in Vote Message`)
}
public async prepareAndSendVoteMessage(inputInfo: prepareVoteMessageInput, guildId: string, requestId: string) {
const messageText = this.createVoteMessageText(inputInfo.event.id, inputInfo.startDate, inputInfo.movies, guildId, requestId)
const sentMessage = await this.sendVoteMessage(messageText, inputInfo.movies.length, inputInfo.announcementChannel)
if (inputInfo.pinAfterSending)
sentMessage.pin()
return sentMessage
}
public createVoteMessageText(eventId: string, eventStartDate: Date, movies: string[], guildId: string, requestId: string): string {
let message = `[Abstimmung] für https://discord.com/events/${guildId}/${eventId} \n<@&${config.bot.announcement_role}> Es gibt eine neue Abstimmung für die nächste Watchparty ${createDateStringFromEvent(eventStartDate, guildId, requestId)}! Stimme hierunter für den nächsten Film ab!\n`
@ -171,6 +179,7 @@ export default class VoteController {
return message
}
public async sendVoteMessage(message: string, movieCount: number, announcementChannel: TextChannel) {
const options: MessageCreateOptions = {

View File

@ -1,5 +1,5 @@
import { Collection } from "@discordjs/collection"
import { Role } from "discord.js"
import { GuildScheduledEvent, Role, TextChannel } from "discord.js"
export type Maybe<T> = T | undefined | null
export interface Player {
@ -39,3 +39,10 @@ export interface JellyfinConfig {
collectionUser: string
}
export type PermissionLevel = "VIEWER" | "ADMIN" | "TEMPORARY"
export interface voteMessageInputInformation {
movies: string[],
startDate: Date,
event: GuildScheduledEvent,
announcementChannel: TextChannel,
pinAfterSending: boolean,
}