Merge pull request 'feat/#42_announce_manual_watchparty' (#50) from feat/#42_announce_manual_watchparty into master
Reviewed-on: #50
This commit is contained in:
commit
2802afa7d5
@ -3,7 +3,7 @@ import { Guild, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildSchedu
|
|||||||
import { v4 as uuid } from 'uuid'
|
import { v4 as uuid } from 'uuid'
|
||||||
import { client } from '../..'
|
import { client } from '../..'
|
||||||
import { config } from '../configuration'
|
import { config } from '../configuration'
|
||||||
import { Emotes } from '../events/guildScheduledEventCreate'
|
import { Emotes } from '../events/autoCreateVoteByWPEvent'
|
||||||
import { Maybe } from '../interfaces'
|
import { Maybe } from '../interfaces'
|
||||||
import { logger } from '../logger'
|
import { logger } from '../logger'
|
||||||
import { Command } from '../structures/command'
|
import { Command } from '../structures/command'
|
||||||
|
48
server/events/announceManualWatchparty.ts
Normal file
48
server/events/announceManualWatchparty.ts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import { GuildScheduledEvent, TextChannel } from "discord.js";
|
||||||
|
import { v4 as uuid } from "uuid";
|
||||||
|
import { client } from "../..";
|
||||||
|
import { config } from "../configuration";
|
||||||
|
import { createDateStringFromEvent } from "../helper/dateHelper";
|
||||||
|
import { Maybe } from "../interfaces";
|
||||||
|
import { logger } from "../logger";
|
||||||
|
|
||||||
|
|
||||||
|
export const name = 'guildScheduledEventCreate'
|
||||||
|
|
||||||
|
export async function execute(event: GuildScheduledEvent) {
|
||||||
|
const guildId = event.guildId
|
||||||
|
const requestId = uuid()
|
||||||
|
try {
|
||||||
|
if (!event.description) {
|
||||||
|
logger.debug("Got GuildScheduledEventCreate event. But has no description. Aborting.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.description.includes("!wp")) {
|
||||||
|
logger.info("Got manual create event of watchparty event!", { guildId, requestId })
|
||||||
|
if(event.description.includes("!private")) {
|
||||||
|
logger.info("Event description contains \"!private\". Won't announce.", { guildId, requestId })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const channel: Maybe<TextChannel> = client.getAnnouncementChannelForGuild(guildId)
|
||||||
|
|
||||||
|
if (!channel) {
|
||||||
|
logger.error("Could not obtain announcement channel. Aborting announcement.", { guildId, requestId })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = `[Watchparty] https://discord.com/events/${event.guildId}/${event.id} \nHey <@&${config.bot.announcement_role}>, wir gucken ${event.name} ${createDateStringFromEvent(event, guildId, requestId)}`
|
||||||
|
|
||||||
|
channel.send(message)
|
||||||
|
} else {
|
||||||
|
logger.debug("Got GuildScheduledEventCreate event but no !wp in description. Not creating manual wp announcement.", { guildId, requestId })
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
// sendFailureDM(error)
|
||||||
|
logger.error(<string>error, { guildId, requestId })
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
import { format } from "date-fns";
|
|
||||||
import { GuildScheduledEvent, Message, MessageCreateOptions, TextChannel } from "discord.js";
|
import { GuildScheduledEvent, Message, MessageCreateOptions, TextChannel } from "discord.js";
|
||||||
import { ScheduledTask } from "node-cron";
|
import { ScheduledTask } from "node-cron";
|
||||||
import { v4 as uuid } from "uuid";
|
import { v4 as uuid } from "uuid";
|
||||||
import { client, yavinJellyfinHandler } from "../..";
|
import { client, yavinJellyfinHandler } from "../..";
|
||||||
import { config } from "../configuration";
|
import { config } from "../configuration";
|
||||||
|
import { createDateStringFromEvent } from "../helper/dateHelper";
|
||||||
import { Maybe } from "../interfaces";
|
import { Maybe } from "../interfaces";
|
||||||
import { logger } from "../logger";
|
import { logger } from "../logger";
|
||||||
|
|
||||||
@ -17,8 +17,7 @@ export let task: ScheduledTask | undefined
|
|||||||
|
|
||||||
export async function execute(event: GuildScheduledEvent) {
|
export async function execute(event: GuildScheduledEvent) {
|
||||||
const requestId = uuid()
|
const requestId = uuid()
|
||||||
logger.debug(`New event created: ${JSON.stringify(event, null, 2)}`, { guildId: event.guildId, requestId })
|
|
||||||
|
|
||||||
if (event.name.toLowerCase().includes("!nextwp")) {
|
if (event.name.toLowerCase().includes("!nextwp")) {
|
||||||
logger.info("Event was a placeholder event to start a new watchparty and voting. Creating vote!", { guildId: event.guildId, requestId })
|
logger.info("Event was a placeholder event to start a new watchparty and voting. Creating vote!", { guildId: event.guildId, requestId })
|
||||||
logger.debug("Renaming event", { guildId: event.guildId, requestId })
|
logger.debug("Renaming event", { guildId: event.guildId, requestId })
|
||||||
@ -39,9 +38,7 @@ export async function execute(event: GuildScheduledEvent) {
|
|||||||
logger.info("EVENT DOES NOT HAVE STARTDATE; CANCELLING", {guildId: event.guildId, requestId})
|
logger.info("EVENT DOES NOT HAVE STARTDATE; CANCELLING", {guildId: event.guildId, requestId})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const date = format(event.scheduledStartAt, "dd.MM")
|
let message = `[Abstimmung] für https://discord.com/events/${event.guildId}/${event.id}\n<@&${config.bot.announcement_role}> Es gibt eine neue Abstimmung für die nächste Watchparty ${createDateStringFromEvent(event, event.guildId, requestId)}! Stimme hierunter für den nächsten Film ab!\n`
|
||||||
const time = format(event.scheduledStartAt, "HH:mm")
|
|
||||||
let message = `[Abstimmung] für https://discord.com/events/${event.guildId}/${event.id}\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++) {
|
for (let i = 0; i < movies.length; i++) {
|
||||||
message = message.concat(Emotes[i]).concat(": ").concat(movies[i]).concat("\n")
|
message = message.concat(Emotes[i]).concat(": ").concat(movies[i]).concat("\n")
|
52
server/events/deleteAnnouncementsWhenWPEnds.ts
Normal file
52
server/events/deleteAnnouncementsWhenWPEnds.ts
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import { Collection, GuildScheduledEvent, GuildScheduledEventStatus, Message } from "discord.js";
|
||||||
|
import { v4 as uuid } from "uuid";
|
||||||
|
import { client } from "../..";
|
||||||
|
import { logger } from "../logger";
|
||||||
|
|
||||||
|
|
||||||
|
export const name = 'guildScheduledEventUpdate'
|
||||||
|
|
||||||
|
export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildScheduledEvent) {
|
||||||
|
const requestId = uuid()
|
||||||
|
try {
|
||||||
|
if (!newEvent.guild) {
|
||||||
|
logger.error("Event has no guild, aborting.", { guildId: newEvent.guildId, requestId })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const guildId = newEvent.guildId
|
||||||
|
|
||||||
|
if (newEvent.description?.toLowerCase().includes("!wp") && newEvent.status === GuildScheduledEventStatus.Completed) {
|
||||||
|
logger.info("A watchparty ended. Cleaning up announcements!", { guildId, requestId })
|
||||||
|
const announcementChannel = client.getAnnouncementChannelForGuild(newEvent.guild.id)
|
||||||
|
if (!announcementChannel) {
|
||||||
|
logger.error("Could not find announcement channel. Aborting", { guildId: newEvent.guild.id, requestId })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = await newEvent.guild.scheduledEvents.fetch()
|
||||||
|
|
||||||
|
const wpAnnouncements = (await announcementChannel.messages.fetch()).filter(message => !message.cleanContent.includes("[initial]"))
|
||||||
|
const announcementsWithoutEvent = filterAnnouncementsByPendingWPs(wpAnnouncements, events)
|
||||||
|
logger.info(`Deleting ${announcementsWithoutEvent.length} announcements.`, {guildId, requestId})
|
||||||
|
announcementsWithoutEvent.forEach(message => message.delete())
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(<string>error, { guildId: newEvent.guildId, requestId })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterAnnouncementsByPendingWPs(messages: Collection<string, Message<true>>, events: Collection<string, GuildScheduledEvent<GuildScheduledEventStatus>>): Message<true>[] {
|
||||||
|
const filteredMessages: Message<true>[] = []
|
||||||
|
for (const message of messages.values()) {
|
||||||
|
let foundEventForMessage = false
|
||||||
|
for (const event of events.values()) {
|
||||||
|
if (message.cleanContent.includes(event.id)) { //announcement always has eventid because of eventbox
|
||||||
|
foundEventForMessage = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!foundEventForMessage){
|
||||||
|
filteredMessages.push(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredMessages
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { GuildMember, GuildScheduledEvent, GuildScheduledEventStatus } from "discord.js";
|
import { GuildMember, GuildScheduledEvent, GuildScheduledEventStatus } from "discord.js";
|
||||||
import { v4 as uuid } from "uuid";
|
import { v4 as uuid } from "uuid";
|
||||||
import { client, jellyfinHandler } from "../..";
|
import { jellyfinHandler } from "../..";
|
||||||
import { getGuildSpecificTriggerRoleId } from "../helper/roleFilter";
|
import { getGuildSpecificTriggerRoleId } from "../helper/roleFilter";
|
||||||
import { logger } from "../logger";
|
import { logger } from "../logger";
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ export const name = 'guildScheduledEventUpdate'
|
|||||||
export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildScheduledEvent) {
|
export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildScheduledEvent) {
|
||||||
try {
|
try {
|
||||||
const requestId = uuid()
|
const requestId = uuid()
|
||||||
logger.debug(`Got scheduledEvent update. New Event: ${JSON.stringify(newEvent, null, 2)}`, { guildId: newEvent.guildId, requestId })
|
// logger.debug(`Got scheduledEvent update. New Event: ${JSON.stringify(newEvent, null, 2)}`, { guildId: newEvent.guildId, requestId })
|
||||||
if (!newEvent.guild) {
|
if (!newEvent.guild) {
|
||||||
logger.error("Event has no guild, aborting.", { guildId: newEvent.guildId, requestId })
|
logger.error("Event has no guild, aborting.", { guildId: newEvent.guildId, requestId })
|
||||||
return
|
return
|
||||||
@ -32,13 +32,7 @@ export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildSche
|
|||||||
if (newEvent.status === GuildScheduledEventStatus.Active)
|
if (newEvent.status === GuildScheduledEventStatus.Active)
|
||||||
createJFUsers(members, newEvent.name, requestId)
|
createJFUsers(members, newEvent.name, requestId)
|
||||||
else {
|
else {
|
||||||
const announcementChannel = await client.getAnnouncementChannelForGuild(newEvent.guild.id)
|
|
||||||
if(!announcementChannel) {
|
|
||||||
logger.error("Could not find announcement channel. Aborting", { guildId: newEvent.guild.id, requestId })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const announcements = (await announcementChannel.messages.fetch()).filter(message => !message.pinned)
|
|
||||||
announcements.forEach(message => message.delete())
|
|
||||||
members.forEach(member => {
|
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.`))
|
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.`))
|
||||||
})
|
})
|
16
server/helper/dateHelper.ts
Normal file
16
server/helper/dateHelper.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { format } from "date-fns";
|
||||||
|
import { GuildScheduledEvent } from "discord.js";
|
||||||
|
import { logger } from "../logger";
|
||||||
|
|
||||||
|
export function createDateStringFromEvent(event: GuildScheduledEvent, requestId: string, guildId?: string): string {
|
||||||
|
if(!event.scheduledStartAt) {
|
||||||
|
logger.error("Event has no start. Cannot create dateString.", {guildId, requestId})
|
||||||
|
return `"habe keinen Startzeitpunkt ermitteln können"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = format(event.scheduledStartAt, "dd.MM")
|
||||||
|
const time = format(event.scheduledStartAt, "HH:mm")
|
||||||
|
|
||||||
|
|
||||||
|
return `am ${date} um ${time}`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user