Fetch announcement channel at server start
This commit is contained in:
parent
40d220ed7b
commit
c0369fcb49
@ -5,6 +5,7 @@ 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',
|
||||||
@ -29,16 +30,8 @@ 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: 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)
|
|
||||||
|
|
||||||
if(!channels || channels.length != 1) {
|
const announcementChannel: TextChannel = client.getAnnouncementChannelForGuild(guildId)
|
||||||
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)
|
||||||
|
@ -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 { jellyfinHandler } from "../..";
|
import { client, 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 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
|
const announcementChannel: TextChannel = client.getAnnouncementChannelForGuild(event.guildId)
|
||||||
logger.debug(`Found channel ${JSON.stringify(channel, null, 2)}`, { guildId: event.guildId, requestId })
|
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"
|
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 channel.fetch()).send(message)
|
const sentMessage: Message<true> = await (await announcementChannel.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])
|
||||||
|
@ -1,18 +1,21 @@
|
|||||||
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 { 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
|
||||||
@ -61,20 +64,38 @@ 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