2023-06-10 22:53:11 +02:00
|
|
|
import { Guild, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildScheduledEventSetStatusArg, GuildScheduledEventStatus, Message, MessageEditOptions, TextChannel } from 'discord.js'
|
2023-06-10 17:27:32 +02:00
|
|
|
import { v4 as uuid } from 'uuid'
|
|
|
|
import { config } from '../configuration'
|
|
|
|
import { Emotes } from '../events/guildScheduledEventCreate'
|
2023-06-10 22:53:11 +02:00
|
|
|
import { logger } from '../logger'
|
|
|
|
import { Command } from '../structures/command'
|
|
|
|
import { RunOptions } from '../types/commandTypes'
|
2023-06-10 17:27:32 +02:00
|
|
|
|
|
|
|
export default new Command({
|
|
|
|
name: 'closepoll',
|
|
|
|
description: 'Aktuelle Umfrage für nächste Watchparty beenden und Gewinner in Event eintragen.',
|
|
|
|
options: [],
|
|
|
|
run: async (interaction: RunOptions) => {
|
|
|
|
const command = interaction.interaction
|
|
|
|
const requestId = uuid()
|
|
|
|
const guildId = command.guildId!
|
|
|
|
logger.info("Got command for closing poll!", { guildId: guildId, requestId })
|
2023-06-10 22:53:11 +02:00
|
|
|
if (!command.guild) {
|
2023-06-10 22:58:00 +02:00
|
|
|
logger.error("No guild found in interaction. Cancelling closing request", {guildId: guildId, requestId})
|
2023-06-10 22:53:11 +02:00
|
|
|
command.followUp("Es gab leider ein Problem. Ich konnte deine Anfrage nicht bearbeiten :(")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
command.followUp("Alles klar, beende die Umfrage :)")
|
|
|
|
closePoll(command.guild, requestId)
|
|
|
|
}
|
|
|
|
})
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
export async function closePoll(guild: Guild, requestId: string) {
|
|
|
|
const guildId = guild.id
|
|
|
|
logger.info("stopping poll", { guildId: guildId, requestId })
|
2023-06-11 15:42:31 +02:00
|
|
|
const channels: TextChannel[] = <TextChannel[]>(await guild.channels.fetch())
|
2023-06-10 22:53:11 +02:00
|
|
|
?.filter(channel => channel!.id === config.bot.announcement_channel_id)
|
2023-06-11 09:01:25 +02:00
|
|
|
.map((value, _) => value)
|
|
|
|
|
|
|
|
if(!channels || channels.length != 1) {
|
|
|
|
logger.error(`Could not find announcement channel. Found ${channels}`)
|
2023-06-11 09:12:14 +02:00
|
|
|
return
|
2023-06-11 09:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const announcementChannel = channels[0]
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
const messages: Message<true>[] = (await announcementChannel.messages.fetch()) //todo: fetch only pinned messages
|
|
|
|
.map((value, _) => value)
|
|
|
|
.filter(message => !message.cleanContent.includes("[Abstimmung beendet]") && message.cleanContent.includes("[Abstimmung]"))
|
|
|
|
.sort((a, b) => b.createdTimestamp - a.createdTimestamp)
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
if (!messages || messages.length <= 0) {
|
|
|
|
logger.info("Could not find any vote messages. Cancelling pollClose", { guildId: guildId, requestId })
|
|
|
|
return
|
|
|
|
}
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
const lastMessage: Message<true> = messages[0]
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
logger.debug(`Found messages: ${JSON.stringify(messages, null, 2)}`, { guildId: guildId, requestId })
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
logger.debug(`Last message: ${JSON.stringify(lastMessage, null, 2)}`, { guildId: guildId, requestId })
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
const votes = await (await getVotesByEmote(lastMessage, guildId, requestId))
|
|
|
|
.sort((a, b) => b.count - a.count)
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
logger.debug(`votes: ${JSON.stringify(votes, null, 2)}`, { guildId: guildId, requestId })
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
updateEvent(votes, guild!, guildId, requestId)
|
|
|
|
updateMessage(votes[0].movie, lastMessage, guildId, requestId)
|
2023-06-10 17:27:32 +02:00
|
|
|
|
2023-06-10 22:53:11 +02:00
|
|
|
//lastMessage.unpin() //todo: uncomment when bot has permission to pin/unpin
|
|
|
|
}
|
2023-06-10 17:27:32 +02:00
|
|
|
|
|
|
|
async function updateMessage(movie: string, message: Message, guildId: string, requestId: string) {
|
|
|
|
const body = `[Abstimmung beendet] Gewonnen hat: ${movie}`
|
|
|
|
.concat(message.cleanContent.substring("[Abstimmung]".length))
|
|
|
|
|
|
|
|
const options: MessageEditOptions = {
|
|
|
|
content: body,
|
|
|
|
}
|
2023-06-10 22:58:00 +02:00
|
|
|
logger.info("Updating message.", {guildId: guildId, requestId})
|
2023-06-10 17:27:32 +02:00
|
|
|
message.edit(options)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateEvent(votes: Vote[], guild: Guild, guildId: string, requestId: string) {
|
|
|
|
logger.info(`Updating event with movie ${votes[0].movie}.`, { guildId: 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: guildId, requestId })
|
2023-06-10 22:53:11 +02:00
|
|
|
|
|
|
|
if (!voteEvents || voteEvents.length <= 0) {
|
2023-06-10 17:27:32 +02:00
|
|
|
logger.error("Could not find vote event. Cancelling update!", { guildId: guildId, requestId })
|
2023-06-10 22:53:11 +02:00
|
|
|
return
|
2023-06-10 17:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const voteEvent: GuildScheduledEvent<GuildScheduledEventStatus> = voteEvents[0]
|
|
|
|
const options: GuildScheduledEventEditOptions<GuildScheduledEventStatus.Scheduled, GuildScheduledEventSetStatusArg<GuildScheduledEventStatus.Scheduled>> = {
|
|
|
|
name: votes[0].movie,
|
2023-06-10 22:53:11 +02:00
|
|
|
description: `!wp\nNummer 2: ${votes[1].movie} mit ${votes[1].count - 1} Stimmen\nNummer 3: ${votes[2].movie} mit ${votes[2].count - 1} Stimmen`
|
2023-06-10 17:27:32 +02:00
|
|
|
}
|
|
|
|
logger.debug(`Updating event: ${JSON.stringify(voteEvent, null, 2)}`, { guildId: guildId, requestId })
|
2023-06-10 22:58:00 +02:00
|
|
|
logger.info("Updating event.", {guildId: guildId, requestId})
|
2023-06-10 17:27:32 +02:00
|
|
|
voteEvent.edit(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Vote = {
|
|
|
|
emote: string, //todo habs nicht hinbekommen hier Emotes zu nutzen
|
|
|
|
count: number,
|
|
|
|
movie: string
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVotesByEmote(message: Message, guildId: string, requestId: string): Promise<Vote[]> {
|
|
|
|
const votes: Vote[] = []
|
2023-06-10 22:58:00 +02:00
|
|
|
logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, {guildId: guildId, requestId})
|
2023-06-10 17:27:32 +02:00
|
|
|
for (let i = 0; i < Object.keys(Emotes).length / 2; i++) {
|
|
|
|
const emote = Emotes[i]
|
|
|
|
logger.debug(`Getting reaction for emote ${emote}`, { guildId: guildId, requestId })
|
|
|
|
const reaction = await message.reactions.resolve(emote)
|
|
|
|
logger.debug(`Reaction for emote ${emote}: ${JSON.stringify(reaction, null, 2)}`, { guildId: guildId, requestId })
|
|
|
|
if (reaction) {
|
|
|
|
const vote: Vote = { emote: emote, count: reaction.count, movie: extractMovieFromMessageByEmote(message, emote, guildId, requestId) }
|
|
|
|
votes.push(vote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return votes
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractMovieFromMessageByEmote(message: Message, emote: string, guildId: string, requestId: string): string {
|
|
|
|
const lines = message.cleanContent.split("\n")
|
|
|
|
const emoteLines = lines.filter(line => line.includes(emote))
|
|
|
|
|
|
|
|
if (!emoteLines) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
const movie = emoteLines[0].substring(emoteLines[0].indexOf(emote) + emote.length + 2) // plus colon and space
|
|
|
|
|
|
|
|
return movie
|
|
|
|
}
|