diff --git a/server/commands/announce.ts b/server/commands/announce.ts index 4fac196..28d7562 100644 --- a/server/commands/announce.ts +++ b/server/commands/announce.ts @@ -23,7 +23,11 @@ export default new Command({ run: async (interaction: RunOptions) => { const command = interaction.interaction const requestId = uuid() - const guildId = command.guildId! + if(!command.guildId) { + logger.error("COMMAND DOES NOT HAVE A GUILD ID; CANCELLING!!!", {requestId}) + return + } + const guildId = command.guildId const announcementType = command.options.data.find(option => option.name.includes("typ")) logger.info(`Got command for announcing ${announcementType?.value}!`, { guildId, requestId }) diff --git a/server/commands/closepoll.ts b/server/commands/closepoll.ts index 6997726..511afa5 100644 --- a/server/commands/closepoll.ts +++ b/server/commands/closepoll.ts @@ -1,11 +1,12 @@ -import { Guild, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildScheduledEventSetStatusArg, GuildScheduledEventStatus, Message, MessageEditOptions, TextChannel } from 'discord.js' +import { Guild, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildScheduledEventSetStatusArg, GuildScheduledEventStatus, Message, MessageCreateOptions, TextChannel } from 'discord.js' import { v4 as uuid } from 'uuid' +import { client } from '../..' import { config } from '../configuration' import { Emotes } from '../events/guildScheduledEventCreate' import { logger } from '../logger' import { Command } from '../structures/command' import { RunOptions } from '../types/commandTypes' -import { client } from '../..' +import { format } from 'date-fns' export default new Command({ name: 'closepoll', @@ -49,43 +50,38 @@ export async function closePoll(guild: Guild, requestId: string) { logger.debug(`Last message: ${JSON.stringify(lastMessage, null, 2)}`, { guildId, requestId }) + const votes = await (await getVotesByEmote(lastMessage, guildId, requestId)) - .sort((a, b) => b.count - a.count) - + .sort((a, b) => b.count - a.count) + logger.debug(`votes: ${JSON.stringify(votes, null, 2)}`, { guildId, requestId }) - - updateEvent(votes, guild!, guildId, requestId) - updateMessage(votes[0].movie, lastMessage, guildId, requestId) + + logger.info("Deleting vote message") + await lastMessage.delete() + const event = await getEvent(guild, guild.id, requestId) + if(event) { + updateEvent(event, votes, guild!, guildId, requestId) + sendVoteClosedMessage(event, votes[0].movie, guildId, requestId) + } //lastMessage.unpin() //todo: uncomment when bot has permission to pin/unpin } -async function updateMessage(movie: string, message: Message, guildId: string, requestId: string) { - const role = (await message.guild!.roles.fetch()).find(role => role.id === config.bot.announcement_role) - const body = `[Abstimmung beendet] Gewonnen hat: ${movie}` - .concat(message.content.substring("[Abstimmung]".length)) - - const options: MessageEditOptions = { +async function sendVoteClosedMessage(event: GuildScheduledEvent, movie: string, guildId: string, requestId: string) { + const date = event.scheduledStartAt ? format(event.scheduledStartAt, "dd.MM") : "Fehler, event hatte kein Datum" + const time = event.scheduledStartAt ? format(event.scheduledStartAt, "HH:mm") : "Fehler, event hatte kein Datum" + const body = `[Abstimmung beendet] <@&${config.bot.announcement_role}> Wir gucken ${movie} am ${date} um ${time}` + const options: MessageCreateOptions = { content: body, allowedMentions: { parse: ["roles"] } } - logger.info("Updating message.", { guildId, requestId }) - message.edit(options) + const announcementChannel = client.getAnnouncementChannelForGuild(guildId) + logger.info("Sending vote closed message.", { guildId, requestId }) + announcementChannel.send(options) } -async function updateEvent(votes: Vote[], guild: Guild, guildId: string, requestId: string) { +async function updateEvent(voteEvent: GuildScheduledEvent, votes: Vote[], guild: Guild, guildId: string, requestId: string) { logger.info(`Updating event with movie ${votes[0].movie}.`, { guildId, requestId }) - const voteEvents = (await guild.scheduledEvents.fetch()) - .map((value, _) => value) - .filter(event => event.name.toLowerCase().includes("voting offen")) - logger.debug(`Found events: ${JSON.stringify(voteEvents, null, 2)}`, { guildId, requestId }) - - if (!voteEvents || voteEvents.length <= 0) { - logger.error("Could not find vote event. Cancelling update!", { guildId, requestId }) - return - } - - const voteEvent: GuildScheduledEvent = voteEvents[0] const options: GuildScheduledEventEditOptions> = { name: votes[0].movie, description: `!wp\nNummer 2: ${votes[1].movie} mit ${votes[1].count - 1} Stimmen\nNummer 3: ${votes[2].movie} mit ${votes[2].count - 1} Stimmen` @@ -95,6 +91,19 @@ async function updateEvent(votes: Vote[], guild: Guild, guildId: string, request voteEvent.edit(options) } +async function getEvent(guild: Guild, guildId: string, requestId: string): Promise { + const voteEvents = (await guild.scheduledEvents.fetch()) + .map((value, _) => value) + .filter(event => event.name.toLowerCase().includes("voting offen")) + logger.debug(`Found events: ${JSON.stringify(voteEvents, null, 2)}`, { guildId, requestId }) + + if (!voteEvents || voteEvents.length <= 0) { + logger.error("Could not find vote event. Cancelling update!", { guildId, requestId }) + return null + } + return voteEvents[0] +} + type Vote = { emote: string, //todo habs nicht hinbekommen hier Emotes zu nutzen count: number, diff --git a/server/events/guildScheduledEventCreate.ts b/server/events/guildScheduledEventCreate.ts index 7c160f2..7b818bc 100644 --- a/server/events/guildScheduledEventCreate.ts +++ b/server/events/guildScheduledEventCreate.ts @@ -6,7 +6,7 @@ import { closePoll } from "../commands/closepoll"; import { config } from "../configuration"; import { logger } from "../logger"; import toDate from "date-fns/fp/toDate"; -import { addDays, isAfter, isBefore } from "date-fns"; +import { addDays, format, isAfter, isBefore } from "date-fns"; export const name = 'guildScheduledEventCreate' @@ -31,7 +31,13 @@ export async function execute(event: GuildScheduledEvent) { const announcementChannel: TextChannel = client.getAnnouncementChannelForGuild(event.guildId) logger.debug(`Found channel ${JSON.stringify(announcementChannel, null, 2)}`, { guildId: event.guildId, requestId }) - let message = `[Abstimmung]\n<@&${config.bot.announcement_role}> Es gibt eine neue Abstimmung für die nächste Watchparty! Stimme hierunter für den nächsten Film ab!\n` + if(!event.scheduledStartAt) { + logger.info("EVENT DOES NOT HAVE STARTDATE; CANCELLING", {guildId: event.guildId, requestId}) + return + } + const date = format(event.scheduledStartAt, "dd.MM") + const time = format(event.scheduledStartAt, "HH:mm") + let message = `[Abstimmung]\n<@&${config.bot.announcement_role}> Es gibt eine neue Abstimmung für die nächste Watchparty am ${date} um ${time}}! Stimme hierunter für den nächsten Film ab!\n` for (let i = 0; i < movies.length; i++) { message = message.concat(Emotes[i]).concat(": ").concat(movies[i].name!).concat("\n") diff --git a/server/events/guildScheduledEventUpdate.ts b/server/events/guildScheduledEventUpdate.ts index b479101..f183740 100644 --- a/server/events/guildScheduledEventUpdate.ts +++ b/server/events/guildScheduledEventUpdate.ts @@ -1,8 +1,9 @@ import { GuildMember, GuildScheduledEvent, GuildScheduledEventStatus } from "discord.js"; import { v4 as uuid } from "uuid"; -import { jellyfinHandler } from "../.."; +import { client, jellyfinHandler } from "../.."; import { getGuildSpecificTriggerRoleId } from "../helper/roleFilter"; import { logger } from "../logger"; +import { manageAnnouncementRoles } from "../commands/announce"; export const name = 'guildScheduledEventUpdate' @@ -27,6 +28,8 @@ export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildSche if (newEvent.status === GuildScheduledEventStatus.Active) createJFUsers(members, newEvent.name, requestId) else { + const announcements = (await client.getAnnouncementChannelForGuild(newEvent.guild!.id).messages.fetch()).filter(message => !message.pinned) + announcements.forEach(message => message.delete()) members.forEach(member => { member.createDM().then(channel => channel.send(`Die Watchparty ist vorbei, dein Account wurde wieder gelöscht. Wenn du einen permanenten Account haben möchtest, melde dich bei Samantha oder Marukus.`)) })