Compare commits
3 Commits
40d220ed7b
...
fdc0fc47b5
Author | SHA1 | Date | |
---|---|---|---|
fdc0fc47b5 | |||
f3669ec34f | |||
c0369fcb49 |
@ -5,6 +5,7 @@ import { Emotes } from '../events/guildScheduledEventCreate'
|
||||
import { logger } from '../logger'
|
||||
import { Command } from '../structures/command'
|
||||
import { RunOptions } from '../types/commandTypes'
|
||||
import { client } from '../..'
|
||||
|
||||
export default new Command({
|
||||
name: 'closepoll',
|
||||
@ -14,9 +15,9 @@ export default new Command({
|
||||
const command = interaction.interaction
|
||||
const requestId = uuid()
|
||||
const guildId = command.guildId!
|
||||
logger.info("Got command for closing poll!", { guildId: guildId, requestId })
|
||||
logger.info("Got command for closing poll!", { guildId, requestId })
|
||||
if (!command.guild) {
|
||||
logger.error("No guild found in interaction. Cancelling closing request", {guildId: guildId, requestId})
|
||||
logger.error("No guild found in interaction. Cancelling closing request", { guildId, requestId })
|
||||
command.followUp("Es gab leider ein Problem. Ich konnte deine Anfrage nicht bearbeiten :(")
|
||||
return
|
||||
}
|
||||
@ -28,17 +29,9 @@ export default new Command({
|
||||
|
||||
export async function closePoll(guild: Guild, requestId: string) {
|
||||
const guildId = guild.id
|
||||
logger.info("stopping poll", { guildId: guildId, requestId })
|
||||
const channels: TextChannel[] = <TextChannel[]>(await guild.channels.fetch())
|
||||
?.filter(channel => channel!.id === config.bot.announcement_channel_id)
|
||||
.map((value, _) => value)
|
||||
logger.info("stopping poll", { guildId, requestId })
|
||||
|
||||
if(!channels || channels.length != 1) {
|
||||
logger.error(`Could not find announcement channel. Found ${channels}`)
|
||||
return
|
||||
}
|
||||
|
||||
const announcementChannel = channels[0]
|
||||
const announcementChannel: TextChannel = client.getAnnouncementChannelForGuild(guildId)
|
||||
|
||||
const messages: Message<true>[] = (await announcementChannel.messages.fetch()) //todo: fetch only pinned messages
|
||||
.map((value, _) => value)
|
||||
@ -46,20 +39,20 @@ export async function closePoll(guild: Guild, requestId: string) {
|
||||
.sort((a, b) => b.createdTimestamp - a.createdTimestamp)
|
||||
|
||||
if (!messages || messages.length <= 0) {
|
||||
logger.info("Could not find any vote messages. Cancelling pollClose", { guildId: guildId, requestId })
|
||||
logger.info("Could not find any vote messages. Cancelling pollClose", { guildId, requestId })
|
||||
return
|
||||
}
|
||||
|
||||
const lastMessage: Message<true> = messages[0]
|
||||
|
||||
logger.debug(`Found messages: ${JSON.stringify(messages, null, 2)}`, { guildId: guildId, requestId })
|
||||
logger.debug(`Found messages: ${JSON.stringify(messages, null, 2)}`, { guildId, requestId })
|
||||
|
||||
logger.debug(`Last message: ${JSON.stringify(lastMessage, null, 2)}`, { guildId: guildId, requestId })
|
||||
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)
|
||||
|
||||
logger.debug(`votes: ${JSON.stringify(votes, null, 2)}`, { guildId: guildId, requestId })
|
||||
logger.debug(`votes: ${JSON.stringify(votes, null, 2)}`, { guildId, requestId })
|
||||
|
||||
updateEvent(votes, guild!, guildId, requestId)
|
||||
updateMessage(votes[0].movie, lastMessage, guildId, requestId)
|
||||
@ -74,20 +67,20 @@ async function updateMessage(movie: string, message: Message, guildId: string, r
|
||||
const options: MessageEditOptions = {
|
||||
content: body,
|
||||
}
|
||||
logger.info("Updating message.", {guildId: guildId, requestId})
|
||||
logger.info("Updating message.", { guildId, requestId })
|
||||
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 })
|
||||
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: guildId, requestId })
|
||||
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: guildId, requestId })
|
||||
logger.error("Could not find vote event. Cancelling update!", { guildId, requestId })
|
||||
return
|
||||
}
|
||||
|
||||
@ -96,8 +89,8 @@ async function updateEvent(votes: Vote[], guild: Guild, guildId: string, request
|
||||
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`
|
||||
}
|
||||
logger.debug(`Updating event: ${JSON.stringify(voteEvent, null, 2)}`, { guildId: guildId, requestId })
|
||||
logger.info("Updating event.", {guildId: guildId, requestId})
|
||||
logger.debug(`Updating event: ${JSON.stringify(voteEvent, null, 2)}`, { guildId, requestId })
|
||||
logger.info("Updating event.", { guildId, requestId })
|
||||
voteEvent.edit(options)
|
||||
}
|
||||
|
||||
@ -109,12 +102,12 @@ type Vote = {
|
||||
|
||||
async function getVotesByEmote(message: Message, guildId: string, requestId: string): Promise<Vote[]> {
|
||||
const votes: Vote[] = []
|
||||
logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, {guildId: guildId, requestId})
|
||||
logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, { guildId, requestId })
|
||||
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 })
|
||||
logger.debug(`Getting reaction for emote ${emote}`, { guildId, requestId })
|
||||
const reaction = await message.reactions.resolve(emote)
|
||||
logger.debug(`Reaction for emote ${emote}: ${JSON.stringify(reaction, null, 2)}`, { guildId: guildId, requestId })
|
||||
logger.debug(`Reaction for emote ${emote}: ${JSON.stringify(reaction, null, 2)}`, { guildId, requestId })
|
||||
if (reaction) {
|
||||
const vote: Vote = { emote: emote, count: reaction.count, movie: extractMovieFromMessageByEmote(message, emote, guildId, requestId) }
|
||||
votes.push(vote)
|
||||
|
@ -24,6 +24,7 @@ export interface Config {
|
||||
watcher_role: string
|
||||
jf_admin_role: string
|
||||
announcement_channel_id: string
|
||||
jf_collection_id: string
|
||||
}
|
||||
}
|
||||
export const config: Config = {
|
||||
@ -56,6 +57,7 @@ export const config: Config = {
|
||||
workaround_token: process.env.TOKEN ?? "",
|
||||
watcher_role: process.env.WATCHER_ROLE ?? "",
|
||||
jf_admin_role: process.env.ADMIN_ROLE ?? "",
|
||||
announcement_channel_id: process.env.CHANNEL_ID ?? ""
|
||||
announcement_channel_id: process.env.CHANNEL_ID ?? "",
|
||||
jf_collection_id: process.env.JELLYFIN_COLLECTION_ID ?? ""
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { GuildScheduledEvent, Message, TextChannel } from "discord.js";
|
||||
import { ScheduledTask, schedule } from "node-cron";
|
||||
import { v4 as uuid } from "uuid";
|
||||
import { jellyfinHandler } from "../..";
|
||||
import { client, jellyfinHandler } from "../..";
|
||||
import { closePoll } from "../commands/closepoll";
|
||||
import { config } from "../configuration";
|
||||
import { logger } from "../logger";
|
||||
@ -28,8 +28,8 @@ export async function execute(event: GuildScheduledEvent) {
|
||||
logger.info(`Got ${movies.length} random movies. Creating voting`, { guildId: event.guildId, requestId })
|
||||
logger.debug(`Movies: ${JSON.stringify(movies.map(movie => movie.name))}`, { guildId: event.guildId, requestId })
|
||||
|
||||
const channel: TextChannel = <TextChannel>(await event.guild?.channels.fetch())?.filter(channel => channel!.id === config.bot.announcement_channel_id).map((value, _) => value)[0] //todo: needs to be done less sketchy
|
||||
logger.debug(`Found channel ${JSON.stringify(channel, null, 2)}`, { guildId: event.guildId, requestId })
|
||||
const announcementChannel: TextChannel = client.getAnnouncementChannelForGuild(event.guildId)
|
||||
logger.debug(`Found channel ${JSON.stringify(announcementChannel, null, 2)}`, { guildId: event.guildId, requestId })
|
||||
|
||||
let message = "[Abstimmung]\nEs gibt eine neue Abstimmung für die nächste Watchparty! Stimme hierunter für den nächsten Film ab!\n"
|
||||
|
||||
@ -37,7 +37,7 @@ export async function execute(event: GuildScheduledEvent) {
|
||||
message = message.concat(Emotes[i]).concat(": ").concat(movies[i].name!).concat("\n")
|
||||
}
|
||||
|
||||
const sentMessage: Message<true> = await (await channel.fetch()).send(message)
|
||||
const sentMessage: Message<true> = await (await announcementChannel.fetch()).send(message)
|
||||
|
||||
for (let i = 0; i < movies.length; i++) {
|
||||
sentMessage.react(Emotes[i])
|
||||
|
@ -47,6 +47,6 @@ async function createJFUsers(members: GuildMember[], movieName: string, requestI
|
||||
}
|
||||
|
||||
async function deleteJFUsers(guildId: string, requestId?: string) {
|
||||
logger.info(`Watchparty ended, deleting tmp users`)
|
||||
logger.info(`Watchparty ended, deleting tmp users`, { guildId, requestId })
|
||||
jellyfinHandler.purge(guildId, requestId)
|
||||
}
|
@ -230,25 +230,25 @@ export class JellyfinHandler {
|
||||
}
|
||||
|
||||
public async getAllMovies(guildId: string, requestId: string): Promise<BaseItemDto[]> {
|
||||
logger.info("requesting all movies from jellyfin", { guildId: guildId, requestId })
|
||||
logger.info("requesting all movies from jellyfin", { guildId, requestId })
|
||||
|
||||
const liloJfUser = await this.getUser(<GuildMember>{ guild: { id: guildId }, displayName: "lilo" }, requestId)
|
||||
|
||||
const searchParams: GetItemsRequest = {
|
||||
userId: liloJfUser?.id,
|
||||
parentId: "f137a2dd21bbc1b99aa5c0f6bf02a805" // collection ID for all movies
|
||||
parentId: this.config.bot.jf_collection_id // collection ID for all movies
|
||||
}
|
||||
const movies = (await (this.moviesApi.getItems(searchParams))).items?.filter(item => !item.isFolder)
|
||||
// logger.debug(JSON.stringify(movies, null, 2), { guildId: guildId, requestId })
|
||||
logger.info(`Found ${movies?.length} movies in total`, { guildId: guildId, requestId })
|
||||
logger.info(`Found ${movies?.length} movies in total`, { guildId, requestId })
|
||||
return movies ?? []
|
||||
}
|
||||
|
||||
public async getRandomMovies(count: number, guildId: string, requestId: string): Promise<BaseItemDto[]> {
|
||||
logger.info(`${count} random movies requested.`, { guildId: guildId, requestId })
|
||||
logger.info(`${count} random movies requested.`, { guildId, requestId })
|
||||
const allMovies = await this.getAllMovies(guildId, requestId)
|
||||
if (count >= allMovies.length) {
|
||||
logger.info(`${count} random movies requested but found only ${allMovies.length}. Returning all Movies.`, { guildId: guildId, requestId })
|
||||
logger.info(`${count} random movies requested but found only ${allMovies.length}. Returning all Movies.`, { guildId, requestId })
|
||||
return allMovies
|
||||
}
|
||||
const movies: BaseItemDto[] = []
|
||||
|
@ -1,15 +1,18 @@
|
||||
import { ApplicationCommandDataResolvable, Client, ClientOptions, Collection, GatewayIntentBits, Guild, IntentsBitField, Snowflake } from "discord.js";
|
||||
import { ApplicationCommandDataResolvable, Client, ClientOptions, Collection, GatewayIntentBits, Guild, IntentsBitField, Snowflake, TextChannel } from "discord.js";
|
||||
import { CommandType } from "../types/commandTypes";
|
||||
import fs from 'fs'
|
||||
import { config } from "../configuration";
|
||||
import { logger } from "../logger";
|
||||
import { JellyfinHandler } from "../jellyfin/handler";
|
||||
|
||||
|
||||
|
||||
export class ExtendedClient extends Client {
|
||||
private eventFilePath = `${__dirname}/../events`
|
||||
private commandFilePath = `${__dirname}/../commands`
|
||||
private jellyfin: JellyfinHandler
|
||||
public commands: Collection<string, CommandType> = new Collection()
|
||||
private announcementChannels: Collection<string, TextChannel> = new Collection //guildId to TextChannel
|
||||
public constructor(jf: JellyfinHandler) {
|
||||
const intents: IntentsBitField = new IntentsBitField()
|
||||
intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages, IntentsBitField.Flags.GuildScheduledEvents, IntentsBitField.Flags.GuildVoiceStates)
|
||||
@ -61,20 +64,38 @@ export class ExtendedClient extends Client {
|
||||
//logger.info(`Ready processing ${JSON.stringify(client)}`)
|
||||
logger.info(`SlashCommands: ${JSON.stringify(slashCommands)}`)
|
||||
const guilds = client.guilds.cache
|
||||
|
||||
this.registerCommands(slashCommands, guilds)
|
||||
this.cacheUsers(guilds)
|
||||
this.cacheAnnouncementServer(guilds)
|
||||
})
|
||||
} catch (error) {
|
||||
logger.info(`Error refreshing slash commands: ${error}`)
|
||||
}
|
||||
}
|
||||
private async cacheAnnouncementServer(guilds: Collection<Snowflake, Guild>) {
|
||||
for (const guild of guilds.values()) {
|
||||
const channels: TextChannel[] = <TextChannel[]>(await guild.channels.fetch())
|
||||
?.filter(channel => channel!.id === config.bot.announcement_channel_id)
|
||||
.map((value, _) => value)
|
||||
|
||||
if (!channels || channels.length != 1) {
|
||||
logger.error(`Could not find announcement channel for guild ${guild.name} with guildId ${guild.id}. Found ${channels}`)
|
||||
continue
|
||||
}
|
||||
logger.info(`Fetched announcement channel: ${JSON.stringify(channels[0])}`)
|
||||
this.announcementChannels.set(guild.id, channels[0])
|
||||
}
|
||||
}
|
||||
public getAnnouncementChannelForGuild(guildId: string): TextChannel {
|
||||
return this.announcementChannels.get(guildId)! //we set the channel by ourselves only if we find one, I think this is sage (mark my words)
|
||||
}
|
||||
public async cacheUsers(guilds: Collection<Snowflake, Guild>) {
|
||||
guilds.forEach((guild: Guild, id: Snowflake) => {
|
||||
logger.info(`Fetching members for ${guild.name}|${id}`)
|
||||
guild.members.fetch()
|
||||
logger.info(`Fetched: ${guild.memberCount} members`)
|
||||
})
|
||||
|
||||
}
|
||||
public async registerEventCallback() {
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user