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