Compare commits
21 Commits
72b88b8387
...
master
Author | SHA1 | Date | |
---|---|---|---|
77aaca2764 | |||
408ac9d8b3 | |||
35353cdd58 | |||
f8afee69bd | |||
9ae406dfb3 | |||
8e8c18b3c1 | |||
2f2817897f | |||
0c63b20ffe | |||
aac7967037 | |||
23e3bbc4b1 | |||
22f0117d30 | |||
790a0d65f4 | |||
b22cb1167d | |||
0d28fce640 | |||
dec3a3070f | |||
99386b1662 | |||
6ff3e68921 | |||
c58c3c61a8 | |||
85770a6031 | |||
19f0dccaec | |||
ca50a337e5 |
@ -1,3 +1,6 @@
|
|||||||
|
## Build Status
|
||||||
|
[](https://wood.brudi.xyz/kenobi/node-event-bot)
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
- yarn
|
- yarn
|
||||||
- npm
|
- npm
|
||||||
|
12
index.ts
12
index.ts
@ -1,13 +1,3 @@
|
|||||||
import { config } from "./server/configuration"
|
|
||||||
import Server from "./server/server"
|
|
||||||
import { ExtendedClient } from "./server/structures/client"
|
import { ExtendedClient } from "./server/structures/client"
|
||||||
|
|
||||||
const server = Server.init(config.port)
|
|
||||||
export const client = new ExtendedClient()
|
export const client = new ExtendedClient()
|
||||||
|
client.start()
|
||||||
|
|
||||||
server.start(() => {
|
|
||||||
console.log(`Server running on port ${server.getPort()}`)
|
|
||||||
//const discordAdapter = new DiscordAdapter()
|
|
||||||
client.start()
|
|
||||||
})
|
|
||||||
|
13
package.json
13
package.json
@ -18,18 +18,15 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@discordjs/rest": "^0.3.0",
|
"@discordjs/rest": "^0.4.1",
|
||||||
"@tsconfig/recommended": "^1.0.1",
|
"@tsconfig/recommended": "^1.0.1",
|
||||||
"@types/cors": "^2.8.12",
|
"@types/node": "^17.0.31",
|
||||||
"@types/express": "^4.17.13",
|
|
||||||
"axios": "^0.26.0",
|
"axios": "^0.26.0",
|
||||||
"body-parser": "^1.19.0",
|
|
||||||
"cors": "^2.8.5",
|
|
||||||
"date-fns": "^2.28.0",
|
"date-fns": "^2.28.0",
|
||||||
"discord-api-types": "^0.27.3",
|
"discord-api-types": "^0.31.2",
|
||||||
"discord.js": "^13.6.0",
|
"discord.js": "^13.6.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"express": "^4.17.1",
|
"ts-node": "^10.7.0",
|
||||||
"typescript": "^4.4.4",
|
"typescript": "^4.4.4",
|
||||||
"winston": "^3.3.3"
|
"winston": "^3.3.3"
|
||||||
},
|
},
|
||||||
@ -37,8 +34,8 @@
|
|||||||
"@types/jest": "^27.0.2",
|
"@types/jest": "^27.0.2",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
||||||
"@typescript-eslint/parser": "^5.3.0",
|
"@typescript-eslint/parser": "^5.3.0",
|
||||||
"jest": "^27.3.1",
|
|
||||||
"eslint": "^8.2.0",
|
"eslint": "^8.2.0",
|
||||||
|
"jest": "^27.3.1",
|
||||||
"jest-cli": "^27.3.1",
|
"jest-cli": "^27.3.1",
|
||||||
"nodemon": "^2.0.14",
|
"nodemon": "^2.0.14",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import { GuildScheduledEvent } from 'discord.js'
|
import { GuildScheduledEvent } from 'discord.js'
|
||||||
|
import { validateRepetitionStringAndSendMessageOnFail } from '../handler/repeatingEvents/helper'
|
||||||
export const name = 'guildScheduledEventCreate'
|
export const name = 'guildScheduledEventCreate'
|
||||||
export function execute(guildScheduledEvent: GuildScheduledEvent) {
|
export async function execute(guildScheduledEvent: GuildScheduledEvent) {
|
||||||
try{
|
try {
|
||||||
console.log(`${JSON.stringify(guildScheduledEvent)} has been created.`)
|
console.log(`${JSON.stringify(guildScheduledEvent)} has been created.`)
|
||||||
}catch(error) {
|
validateRepetitionStringAndSendMessageOnFail(guildScheduledEvent)
|
||||||
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
import { GuildScheduledEvent } from "discord.js"
|
import { GuildScheduledEvent } from "discord.js"
|
||||||
|
import { config } from "../configuration"
|
||||||
|
import { repetitionMarkerIsFound, validateRepetitionStringAndSendMessageOnFail } from "../handler/repeatingEvents/helper"
|
||||||
import { handleRepeatingEvent } from "../handler/repeatingEvents/repeatingEvents.controller"
|
import { handleRepeatingEvent } from "../handler/repeatingEvents/repeatingEvents.controller"
|
||||||
|
|
||||||
|
|
||||||
const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
|
|
||||||
|
|
||||||
export const name = 'guildScheduledEventUpdate'
|
export const name = 'guildScheduledEventUpdate'
|
||||||
export function execute(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
|
export function execute(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
|
||||||
|
if (config.debug) {
|
||||||
console.dir(oldguildScheduledEvent)
|
console.dir(oldguildScheduledEvent)
|
||||||
console.dir(newguildScheduledEvent)
|
console.dir(newguildScheduledEvent)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldguildScheduledEvent.description !== newguildScheduledEvent.description) {
|
||||||
|
validateRepetitionStringAndSendMessageOnFail(newguildScheduledEvent)
|
||||||
|
}
|
||||||
if (oldguildScheduledEvent.description && repetitionMarkerIsFound(oldguildScheduledEvent.description)) {
|
if (oldguildScheduledEvent.description && repetitionMarkerIsFound(oldguildScheduledEvent.description)) {
|
||||||
// valid repeating event
|
// valid repeating event
|
||||||
if (newguildScheduledEvent.status === 'COMPLETED')
|
if (newguildScheduledEvent.status === 'COMPLETED')
|
||||||
|
@ -1,42 +1,55 @@
|
|||||||
import add from "date-fns/add"
|
import { format } from "date-fns"
|
||||||
import { DateResolvable, GuildScheduledEvent } from "discord.js"
|
import { Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
||||||
import { findInScheduleTypes } from "../../helper/typeFind"
|
import { sendFailureDM } from "../../helper/sendFailureDM"
|
||||||
import { RepetitonInfo, supportedSchedule } from "../../types/scheduledEventTypes"
|
import { CustomError, errorCodes, Maybe } from "../../interfaces"
|
||||||
|
import { RepetitonInfo, Schedule, supportedSchedule } from "../../types/scheduledEventTypes"
|
||||||
|
|
||||||
|
export const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
|
||||||
|
export function createEventInGuild(guild: Guild, eventInfo: GuildScheduledEventCreateOptions): Promise<any> {
|
||||||
|
return guild.scheduledEvents.create(eventInfo)
|
||||||
|
}
|
||||||
export function getRepetitonInfo(description: string): RepetitonInfo {
|
export function getRepetitonInfo(description: string): RepetitonInfo {
|
||||||
|
|
||||||
const lines = description.split(`\n`)
|
const lines = description.split(`\n`)
|
||||||
const repetitionString = lines.find(x => x.startsWith('$rep:'))
|
const repetitionString = lines.find(x => x.startsWith('$rep:'))
|
||||||
if (!repetitionString)
|
if (!repetitionString)
|
||||||
throw new Error('Cant find repetition string')
|
throw new CustomError('Cant find repetition string', errorCodes.no_string_present)
|
||||||
const schedule: supportedSchedule = determineSchedule(repetitionString)
|
const scheduleString = determineScheduleString(repetitionString)
|
||||||
|
const schedule: Schedule = new Schedule(scheduleString)
|
||||||
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
|
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
|
||||||
|
const endDate = determineEndDate(repetitionString)
|
||||||
return {
|
return {
|
||||||
totalAmount,
|
totalAmount,
|
||||||
alreadyOccured,
|
alreadyOccured,
|
||||||
schedule
|
schedule,
|
||||||
|
endDate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function determineSchedule(repetitionLine: string): supportedSchedule {
|
export function determineScheduleString(repetitionLine: string): supportedSchedule {
|
||||||
const segments = repetitionLine.split(':')
|
const segments = repetitionLine.split(':')
|
||||||
const scheduleSegment = segments[1]
|
const scheduleSegment = segments[1]
|
||||||
const easilyKnownScheduleName = findInScheduleTypes(scheduleSegment)
|
if (scheduleSegment)
|
||||||
if (easilyKnownScheduleName)
|
return scheduleSegment
|
||||||
return easilyKnownScheduleName
|
|
||||||
else
|
else
|
||||||
throw new Error('Inferring schedule names is not yet supported')
|
throw new CustomError('No schedule segment found', errorCodes.no_schedule)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function determineRepetitionCount(description: string): { totalAmount: number; alreadyOccured: number } {
|
export function determineRepetitionCount(description: string): { totalAmount: number; alreadyOccured: number } {
|
||||||
const segments = description.split(':')
|
const segments = description.split(':')
|
||||||
const amountSegment = segments[2]
|
const amountSegment = segments[2]
|
||||||
|
if (amountSegment) {
|
||||||
const amounts = amountSegment.split('/')
|
const amounts = amountSegment.split('/')
|
||||||
return { totalAmount: Number(amounts[1]) ?? 0, alreadyOccured: Number(amounts[0]) ?? 0 }
|
return { totalAmount: Number(amounts[1]) ?? 0, alreadyOccured: Number(amounts[0]) ?? 0 }
|
||||||
|
} else {
|
||||||
|
throw new CustomError('No amount was defined', errorCodes.no_repetition_amount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildNewRepetitionString(repetitionInfo: RepetitonInfo) {
|
export function buildNewRepetitionString(repetitionInfo: RepetitonInfo) {
|
||||||
return `$rep:${repetitionInfo.schedule}:${repetitionInfo.alreadyOccured + 1}/${repetitionInfo.totalAmount}`
|
if (repetitionInfo.endDate)
|
||||||
|
return `$rep:${repetitionInfo.schedule.getSanitizedScheduleString()}:${format(repetitionInfo.endDate, 'yyyy-MM-dd')}`
|
||||||
|
return `$rep:${repetitionInfo.schedule.getSanitizedScheduleString()}:${repetitionInfo.alreadyOccured + 1}/${repetitionInfo.totalAmount}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addRepetitonStringToEventDescription(oldguildScheduledEvent: string, newRepetitonString: string): string | undefined {
|
export function addRepetitonStringToEventDescription(oldguildScheduledEvent: string, newRepetitonString: string): string | undefined {
|
||||||
@ -47,27 +60,60 @@ export function addRepetitonStringToEventDescription(oldguildScheduledEvent: str
|
|||||||
return newLines.join('\n')
|
return newLines.join('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getNewScheduledStart(oldguildScheduledEvent: GuildScheduledEvent<"SCHEDULED" | "ACTIVE" | "COMPLETED" | "CANCELED">, rInfo: RepetitonInfo): DateResolvable {
|
function determineEndDate(description: string): Maybe<Date> {
|
||||||
const oldDate = oldguildScheduledEvent.scheduledStartAt
|
const segments = description.split(':')
|
||||||
let daysToAdd = 0
|
if (segments.length === 3) {
|
||||||
let monthsToAdd = 0
|
// rep:sched:countOrDate
|
||||||
switch (rInfo.schedule) {
|
const segmentValue = segments[2]
|
||||||
case 'daily':
|
if (segmentValue.includes('/')) {
|
||||||
daysToAdd = 1
|
if (segmentValue.match(/\//g) || [].length !== 2) {
|
||||||
break
|
return
|
||||||
case 'weekly':
|
|
||||||
daysToAdd = 7
|
|
||||||
break
|
|
||||||
case 'monthly':
|
|
||||||
monthsToAdd = 1
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw new Error('No schedule found, cant add days')
|
|
||||||
}
|
}
|
||||||
const duration: Duration = {
|
|
||||||
days: daysToAdd,
|
|
||||||
months: monthsToAdd
|
|
||||||
}
|
}
|
||||||
const newDate = add(oldDate, duration)
|
const dateValue = new Date(segmentValue)
|
||||||
return newDate
|
return dateValue
|
||||||
|
}
|
||||||
|
else if (segments.length === 4) {
|
||||||
|
// rep:sched:count:date
|
||||||
|
const segmentValue = segments[3]
|
||||||
|
const dateValue = new Date(segmentValue)
|
||||||
|
return dateValue
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
export function checkIfRepetitionStringIsValid(description: string): string {
|
||||||
|
if (!description) return 'not_present'
|
||||||
|
if (repetitionMarkerIsFound(description)) {
|
||||||
|
let repetitionInfo
|
||||||
|
try {
|
||||||
|
repetitionInfo = getRepetitonInfo(description)
|
||||||
|
if (!repetitionInfo.schedule) return 'no_schedule'
|
||||||
|
if (!repetitionInfo.totalAmount && !repetitionInfo.alreadyOccured && !repetitionInfo.endDate) {
|
||||||
|
if (!repetitionInfo.totalAmount || !repetitionInfo.alreadyOccured) return 'no_amount'
|
||||||
|
if (!repetitionInfo.endDate) return 'no_end'
|
||||||
|
}
|
||||||
|
return 'valid'
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof CustomError) {
|
||||||
|
return error.getCode()
|
||||||
|
}
|
||||||
|
return 'invalid'
|
||||||
|
}
|
||||||
|
} return 'not_present'
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function validateRepetitionStringAndSendMessageOnFail(event: GuildScheduledEvent): Promise<void> {
|
||||||
|
console.log('This should not be accessed')
|
||||||
|
|
||||||
|
const validResponses = [
|
||||||
|
'valid',
|
||||||
|
'not_present'
|
||||||
|
]
|
||||||
|
const resultstring: string = checkIfRepetitionStringIsValid(event.description ?? "")
|
||||||
|
if (validResponses.includes(resultstring)) {
|
||||||
|
// do success things?
|
||||||
|
} else {
|
||||||
|
const creatorMessage = `The repetition string in your event could not be parsed. Reason: ${resultstring}`
|
||||||
|
sendFailureDM(creatorMessage, event.creatorId ?? undefined)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
||||||
import { RepetitonInfo } from "../../types/scheduledEventTypes"
|
import { RepetitonInfo } from "../../types/scheduledEventTypes"
|
||||||
import { addRepetitonStringToEventDescription, buildNewRepetitionString, getNewScheduledStart, getRepetitonInfo } from "./helper"
|
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getRepetitonInfo } from "./helper"
|
||||||
|
|
||||||
const needsToBeRepeated = (rInfo: RepetitonInfo): boolean => rInfo.alreadyOccured < rInfo.totalAmount
|
function needsToBeRepeated(rInfo: RepetitonInfo): boolean {
|
||||||
|
if (rInfo.endDate) {
|
||||||
|
if (new Date() < rInfo.endDate)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return rInfo.alreadyOccured < rInfo.totalAmount
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
|
export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
|
||||||
if (!oldguildScheduledEvent.description) throw new Error('Event has no description -> cant handle this')
|
if (!oldguildScheduledEvent.description) throw new Error('Event has no description -> cant handle this')
|
||||||
@ -10,19 +18,27 @@ export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent
|
|||||||
if (needsToBeRepeated(repetitionInfo)) {
|
if (needsToBeRepeated(repetitionInfo)) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
const newDate = repetitionInfo.schedule.getNewDate(oldguildScheduledEvent.scheduledStartAt)
|
||||||
|
if (repetitionInfo.endDate && (repetitionInfo.endDate <= newDate)) {
|
||||||
|
console.log(`Wont repeat: EndDate: ${repetitionInfo.endDate} RepetitionDate: ${newDate}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
const newRepetitonString = buildNewRepetitionString(repetitionInfo)
|
const newRepetitonString = buildNewRepetitionString(repetitionInfo)
|
||||||
const newEventOptions: GuildScheduledEventCreateOptions = {
|
const newEventOptions: GuildScheduledEventCreateOptions = {
|
||||||
name: oldguildScheduledEvent.name,
|
name: oldguildScheduledEvent.name,
|
||||||
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
|
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
|
||||||
scheduledStartTime: getNewScheduledStart(oldguildScheduledEvent, repetitionInfo),
|
scheduledStartTime: newDate,
|
||||||
privacyLevel: oldguildScheduledEvent.privacyLevel,
|
privacyLevel: oldguildScheduledEvent.privacyLevel,
|
||||||
entityType: oldguildScheduledEvent.entityType,
|
entityType: oldguildScheduledEvent.entityType,
|
||||||
channel: oldguildScheduledEvent.channel?.id,
|
channel: oldguildScheduledEvent.channel?.id,
|
||||||
reason: 'Repetition'
|
reason: 'Repetition'
|
||||||
}
|
}
|
||||||
newguildScheduledEvent.guild?.scheduledEvents.create(newEventOptions)
|
if (!newguildScheduledEvent.guild) throw new Error('No guild on event?')
|
||||||
|
createEventInGuild(newguildScheduledEvent.guild, newEventOptions)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Event does not need to be repeated')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
server/helper/sendFailureDM.ts
Normal file
12
server/helper/sendFailureDM.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { client } from "../.."
|
||||||
|
import { CustomError, errorCodes } from "../interfaces"
|
||||||
|
|
||||||
|
export async function sendFailureDM(creatorMessage: string, creatorId?: string): Promise<void> {
|
||||||
|
if (!creatorId) throw new CustomError('No creator ID present', errorCodes.no_creator_id)
|
||||||
|
const creator = await client.users.fetch(creatorId)
|
||||||
|
console.log(`Creator ${JSON.stringify(creator)}`)
|
||||||
|
if (creator)
|
||||||
|
if (!creator.dmChannel)
|
||||||
|
await creator.createDM()
|
||||||
|
await creator.dmChannel?.send(creatorMessage)
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
import { scheduleNames, supportedSchedule } from "../types/scheduledEventTypes";
|
|
||||||
|
|
||||||
export function findInScheduleTypes(inputString: string): supportedSchedule {
|
|
||||||
const maybeScheduleName: unknown = JSON.parse(`"${inputString.toLowerCase()}"`);
|
|
||||||
const scheduleName = scheduleNames.find((validName: supportedSchedule) => validName === maybeScheduleName);
|
|
||||||
if (scheduleName) {
|
|
||||||
// `sheepName` comes from the list of `sheepNames` so the compiler is happy.
|
|
||||||
return scheduleName;
|
|
||||||
}
|
|
||||||
throw new Error('That is not a schedule name.');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -9,3 +9,21 @@ export interface localized_string {
|
|||||||
[k in supported_languages]: string
|
[k in supported_languages]: string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class CustomError extends Error {
|
||||||
|
private code: string
|
||||||
|
public constructor(message: string, errorCode: string) {
|
||||||
|
super(message)
|
||||||
|
this.code = errorCode
|
||||||
|
}
|
||||||
|
public getCode() { return this.code }
|
||||||
|
}
|
||||||
|
export const errorCodes = {
|
||||||
|
no_end_date: 'no_end_date',
|
||||||
|
no_string_present: 'no_string_present',
|
||||||
|
no_schedule: 'no_schedule',
|
||||||
|
schedule_not_supported: 'schedule_not_supported',
|
||||||
|
no_repetition_amount: 'no_repetition_amount',
|
||||||
|
invalid_repetition_string: 'invalid_repetition_string',
|
||||||
|
no_creator_id: "no_creator_id",
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
import bodyParser from "body-parser"
|
|
||||||
import cors from "cors"
|
|
||||||
import express from "express"
|
|
||||||
import { config } from "./configuration"
|
|
||||||
|
|
||||||
export default class Server {
|
|
||||||
private app: express.Application
|
|
||||||
private port: number
|
|
||||||
|
|
||||||
public constructor(port: number) {
|
|
||||||
this.port = port
|
|
||||||
this.app = express()
|
|
||||||
}
|
|
||||||
public static init(port: number): Server {
|
|
||||||
return new Server(port)
|
|
||||||
}
|
|
||||||
public start(callback: (...args: any[]) => void): void {
|
|
||||||
this.setBodyParser()
|
|
||||||
this.setCors()
|
|
||||||
this.getApp().listen(this.port, callback)
|
|
||||||
|
|
||||||
}
|
|
||||||
public getApp(): express.Application {
|
|
||||||
return this.app
|
|
||||||
}
|
|
||||||
public getPort(): number {
|
|
||||||
return this.port
|
|
||||||
}
|
|
||||||
private setBodyParser(): void {
|
|
||||||
this.getApp().use(bodyParser.urlencoded(config.server.bodyParser.urlEncodedOptions))
|
|
||||||
this.getApp().use(bodyParser.json(config.server.bodyParser.jsonOptions))
|
|
||||||
}
|
|
||||||
private setCors(): void {
|
|
||||||
this.getApp().use(cors())
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,6 +11,7 @@ export class ExtendedClient extends Client {
|
|||||||
super({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_SCHEDULED_EVENTS] })
|
super({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_SCHEDULED_EVENTS] })
|
||||||
}
|
}
|
||||||
public start() {
|
public start() {
|
||||||
|
if (process.env.NODE_ENV === 'test') return
|
||||||
const promises = []
|
const promises = []
|
||||||
promises.push(this.registerSlashCommands())
|
promises.push(this.registerSlashCommands())
|
||||||
promises.push(this.registerEventCallback())
|
promises.push(this.registerEventCallback())
|
||||||
|
@ -1,11 +1,102 @@
|
|||||||
|
import { add } from "date-fns"
|
||||||
|
import { DateResolvable } from "discord.js"
|
||||||
|
import { CustomError, errorCodes } from "../interfaces"
|
||||||
|
|
||||||
export interface RepetitonInfo {
|
export interface RepetitonInfo {
|
||||||
startDate?: Date, // If defined will take precedence over repetitonAmount
|
startDate?: Date, // If defined will take precedence over repetitonAmount
|
||||||
endDate?: Date,// If defined will take precedence over repetitonAmount
|
endDate?: Date,// If defined will take precedence over repetitonAmount
|
||||||
totalAmount: number,
|
totalAmount: number,
|
||||||
alreadyOccured: number,
|
alreadyOccured: number,
|
||||||
schedule: supportedSchedule
|
schedule: Schedule
|
||||||
numberOfDays?: number
|
|
||||||
}
|
}
|
||||||
export const scheduleNames = ['daily', 'weekly', 'monthly', 'everyTwoWeeks', 'everyNDays']
|
export const scheduleNames = ['daily', 'weekly', 'monthly', 'everyNWeeks', 'everyNDays', 'everyNMonths']
|
||||||
export type supportedSchedule = typeof scheduleNames[number]
|
export type supportedSchedule = typeof scheduleNames[number]
|
||||||
|
export interface IScheduleType {
|
||||||
|
name: supportedSchedule,
|
||||||
|
multiplier: number,
|
||||||
|
duration: Duration
|
||||||
|
}
|
||||||
|
export const scheduleTypes: IScheduleType[] = [
|
||||||
|
{
|
||||||
|
name: 'daily',
|
||||||
|
multiplier: 1,
|
||||||
|
duration: {
|
||||||
|
days: 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'weekly',
|
||||||
|
multiplier: 1,
|
||||||
|
duration: {
|
||||||
|
weeks: 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
export class Schedule {
|
||||||
|
private scheduleName: string
|
||||||
|
private multiplier = 1
|
||||||
|
private duration: Duration
|
||||||
|
private baseScheduleTypes = ['daily', 'weekly', 'monthly', 'yearly']
|
||||||
|
private _scheduleString: string
|
||||||
|
constructor(scheduleString: string) {
|
||||||
|
this._scheduleString = scheduleString.toLowerCase()
|
||||||
|
this.scheduleName = this._scheduleString
|
||||||
|
if (this.baseScheduleTypes.includes(this._scheduleString)) {
|
||||||
|
this.multiplier = 1
|
||||||
|
}
|
||||||
|
if (this._scheduleString.includes('every')) {
|
||||||
|
this.scheduleName = this.getBaseScheduleNameFromVariableString()
|
||||||
|
this.multiplier = this.getMultiplierFromVariableString()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (this.scheduleName) {
|
||||||
|
case 'daily':
|
||||||
|
this.duration = { days: 1 }
|
||||||
|
break
|
||||||
|
case 'weekly':
|
||||||
|
this.duration = { weeks: 1 }
|
||||||
|
break
|
||||||
|
case 'monthly':
|
||||||
|
this.duration = { months: 1 }
|
||||||
|
break
|
||||||
|
case 'yearly':
|
||||||
|
this.duration = { years: 1 }
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
throw new CustomError('Schedule type not supported', errorCodes.schedule_not_supported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public getSanitizedScheduleString(): string {
|
||||||
|
return this._scheduleString
|
||||||
|
}
|
||||||
|
private getBaseScheduleNameFromVariableString(): string {
|
||||||
|
if (this._scheduleString.includes('week')) return 'weekly'
|
||||||
|
if (this._scheduleString.includes('day')) return 'daily'
|
||||||
|
if (this._scheduleString.includes('month')) return 'monthly'
|
||||||
|
if (this._scheduleString.includes('year')) return 'yearly'
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
public getMultiplierFromVariableString(): number {
|
||||||
|
const matches = this._scheduleString.match(/\d+/)
|
||||||
|
if (matches) {
|
||||||
|
const multi = matches[0]
|
||||||
|
if (multi)
|
||||||
|
return parseInt(multi)
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
public calculateDuration(): Duration {
|
||||||
|
const dur: Duration = {
|
||||||
|
days: this.duration.days ? this.duration.days * this.multiplier : undefined,
|
||||||
|
weeks: this.duration.weeks ? this.duration.weeks * this.multiplier : undefined,
|
||||||
|
months: this.duration.months ? this.duration.months * this.multiplier : undefined,
|
||||||
|
years: this.duration.years ? this.duration.years * this.multiplier : undefined,
|
||||||
|
}
|
||||||
|
return dur
|
||||||
|
}
|
||||||
|
|
||||||
|
public getNewDate(oldDate: Date): Date {
|
||||||
|
const newDate = add(oldDate, this.calculateDuration())
|
||||||
|
return newDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
32
tests/createdEvent.test.ts
Normal file
32
tests/createdEvent.test.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { getRepetitonInfo } from "../server/handler/repeatingEvents/helper"
|
||||||
|
|
||||||
|
describe('ScheduledEvent Creation Events', () => {
|
||||||
|
|
||||||
|
test('Daily Event with absolute end date', () => {
|
||||||
|
jest.mock('../server/helper/sendFailureDM.ts')
|
||||||
|
const eventObject = {
|
||||||
|
"id": "965576921410859018",
|
||||||
|
"guildId": "907936880190967850",
|
||||||
|
"channelId": "907936880190967854",
|
||||||
|
"creatorId": "191951058111692800",
|
||||||
|
"name": "Created event",
|
||||||
|
"description": "$rep:daily:2022-05-22",
|
||||||
|
"scheduledStartTimestamp": 1650294000782,
|
||||||
|
"scheduledEndTimestamp": null,
|
||||||
|
"privacyLevel": "GUILD_ONLY",
|
||||||
|
"status": "SCHEDULED",
|
||||||
|
"entityType": "VOICE",
|
||||||
|
"entityId": null,
|
||||||
|
"userCount": null,
|
||||||
|
"creator": null,
|
||||||
|
"entityMetadata": null
|
||||||
|
}
|
||||||
|
const rInfo = getRepetitonInfo(eventObject.description)
|
||||||
|
const expectedSchedule = { "_scheduleString": "daily", "baseScheduleTypes": ["daily", "weekly", "monthly", "yearly"], "duration": { "days": 1 }, "multiplier": 1, "scheduleName": "daily" }
|
||||||
|
expect(rInfo).toBeDefined()
|
||||||
|
expect(rInfo.endDate).toBeDefined()
|
||||||
|
expect(rInfo.endDate).toEqual(new Date("2022-05-22"))
|
||||||
|
expect(rInfo.schedule).toEqual(expectedSchedule)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
@ -1,35 +1,92 @@
|
|||||||
import { findInScheduleTypes } from '../server/helper/typeFind'
|
import { Schedule } from '../server/types/scheduledEventTypes'
|
||||||
import { supportedSchedule } from '../server/types/scheduledEventTypes'
|
import { buildNewRepetitionString, getRepetitonInfo } from '../server/handler/repeatingEvents/helper'
|
||||||
import { getRepetitonInfo } from '../server/handler/repeatingEvents/helper'
|
|
||||||
import { RepetitonInfo } from '../server/types/scheduledEventTypes'
|
import { RepetitonInfo } from '../server/types/scheduledEventTypes'
|
||||||
describe('Schedule names are parsed correctly', () => {
|
describe('Parsing of Repetition Info from Description String', () => {
|
||||||
const dailyValue: supportedSchedule = 'daily'
|
test('Happy Path', () => {
|
||||||
const weeklyValue: supportedSchedule = 'weekly'
|
|
||||||
const monthlyValue: supportedSchedule = 'monthly'
|
|
||||||
test('Easy schedule names', () => {
|
|
||||||
expect(findInScheduleTypes('daily')).toEqual(dailyValue)
|
|
||||||
expect(findInScheduleTypes('weekly')).toEqual(weeklyValue)
|
|
||||||
expect(findInScheduleTypes('monthly')).toEqual(monthlyValue)
|
|
||||||
})
|
|
||||||
test('Medium schedule names', () => {
|
|
||||||
expect(findInScheduleTypes('Daily')).toEqual(dailyValue)
|
|
||||||
expect(findInScheduleTypes('Weekly')).toEqual(weeklyValue)
|
|
||||||
expect(findInScheduleTypes('Monthly')).toEqual(monthlyValue)
|
|
||||||
expect(findInScheduleTypes('DAILY')).toEqual(dailyValue)
|
|
||||||
expect(findInScheduleTypes('WEEKLy')).toEqual(weeklyValue)
|
|
||||||
expect(findInScheduleTypes('MONTHly')).toEqual(monthlyValue)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
describe('Parsing of Repetition Info from Description String',()=>{
|
|
||||||
test('Happy Path',()=>{
|
|
||||||
const inputString = '$rep:daily:1/3'
|
const inputString = '$rep:daily:1/3'
|
||||||
const expectedInfo: RepetitonInfo = {
|
const expectedInfo: RepetitonInfo = {
|
||||||
totalAmount: 3,
|
totalAmount: 3,
|
||||||
alreadyOccured: 1,
|
alreadyOccured: 1,
|
||||||
schedule: 'daily'
|
schedule: new Schedule('daily')
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(getRepetitonInfo(inputString)).toEqual(expectedInfo)
|
expect(getRepetitonInfo(inputString)).toEqual(expectedInfo)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
describe('new RepetitionString for complex schedule', () => {
|
||||||
|
const repString = '$rep:EvEry3WeeKs:2022-12-01'
|
||||||
|
const repInfo = getRepetitonInfo(repString)
|
||||||
|
const schedule = new Schedule(repString)
|
||||||
|
const str = schedule.getSanitizedScheduleString()
|
||||||
|
expect(str).toEqual('$rep:every3weeks:2022-12-01')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = schedule.getNewDate(oldDate)
|
||||||
|
expect(newDate).toEqual(new Date('2022-01-22'))
|
||||||
|
expect(buildNewRepetitionString(repInfo)).toEqual('$rep:every3weeks:2022-12-01')
|
||||||
|
})
|
||||||
|
describe('new RepetitionString for complex schedule', () => {
|
||||||
|
const repString = '$rep:EvEry3WeeKs:2022-12-01'
|
||||||
|
const schedule = new Schedule(repString)
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = schedule.getNewDate(oldDate)
|
||||||
|
})
|
||||||
|
const oldEvent = {
|
||||||
|
"id": "965576921410859018",
|
||||||
|
"guildId": "907936880190967850",
|
||||||
|
"channelId": "907936880190967854",
|
||||||
|
"creatorId": "191951058111692800",
|
||||||
|
"name": "Created event",
|
||||||
|
"description": "$rep:daily:2022-05-22",
|
||||||
|
"createdTimestamp": 1650294000782,
|
||||||
|
"createdAt": new Date("2022-04-01"),
|
||||||
|
"scheduledStartTimestamp": 1650294000782,
|
||||||
|
"scheduledEndTimestamp": null,
|
||||||
|
"privacyLevel": "GUILD_ONLY",
|
||||||
|
"status": "ACTIVE",
|
||||||
|
"entityType": "VOICE",
|
||||||
|
"entityId": null,
|
||||||
|
"userCount": null,
|
||||||
|
"creator": null,
|
||||||
|
guild: {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const newEvent = {
|
||||||
|
"id": "965576921410859018",
|
||||||
|
"guildId": "907936880190967850",
|
||||||
|
"channelId": "907936880190967854",
|
||||||
|
"creatorId": "191951058111692800",
|
||||||
|
"createdTimestamp": 1650294000782,
|
||||||
|
"name": "Created event",
|
||||||
|
"description": "$rep:daily:2022-05-22",
|
||||||
|
"scheduledStartTimestamp": 1650294000782,
|
||||||
|
"scheduledEndTimestamp": null,
|
||||||
|
"privacyLevel": "GUILD_ONLY",
|
||||||
|
"status": "COMPLETED",
|
||||||
|
"entityType": "VOICE",
|
||||||
|
"entityId": null,
|
||||||
|
"userCount": null,
|
||||||
|
"creator": null,
|
||||||
|
guild: {}
|
||||||
|
}
|
||||||
|
jest.mock('../server/helper/sendFailureDM.ts')
|
||||||
|
jest.mock('../server/handler/repeatingEvents/helper.ts', () => ({
|
||||||
|
...(jest.requireActual('../server/handler/repeatingEvents/helper.ts')),
|
||||||
|
createEventInGuild: jest.fn().mockImplementation((opt: any) => {
|
||||||
|
return
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
//test('handleRepeatingEvent', () => {
|
||||||
|
//
|
||||||
|
// const expectedOptions: GuildScheduledEventCreateOptions = {
|
||||||
|
// channel: "",
|
||||||
|
// description: "",
|
||||||
|
// name: newEvent.name,
|
||||||
|
// entityType: <'VOICE'>newEvent.entityType,
|
||||||
|
// privacyLevel: <'GUILD_ONLY'>newEvent.privacyLevel,
|
||||||
|
// reason: 'Repetition',
|
||||||
|
// scheduledStartTime: ""
|
||||||
|
// }
|
||||||
|
// //@ts-ignore
|
||||||
|
// //handleRepeatingEvent(oldEvent, newEvent)
|
||||||
|
// expect(createEventInGuild).toHaveBeenCalledWith({}, expectedOptions)
|
||||||
|
//})
|
||||||
|
66
tests/scheduleparsing.test.ts
Normal file
66
tests/scheduleparsing.test.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { Schedule } from "../server/types/scheduledEventTypes"
|
||||||
|
|
||||||
|
test('multiplier is parsed', () => {
|
||||||
|
const schedule = new Schedule('every3weeks')
|
||||||
|
const multi = schedule.getMultiplierFromVariableString()
|
||||||
|
expect(multi).toEqual(3)
|
||||||
|
})
|
||||||
|
test('get duration 3 weeks', () => {
|
||||||
|
const schedule = new Schedule('every3weeks')
|
||||||
|
const duration = schedule.calculateDuration()
|
||||||
|
expect(duration).toEqual({ weeks: 3 })
|
||||||
|
})
|
||||||
|
test('get duration 4 years', () => {
|
||||||
|
const schedule = new Schedule('every4years')
|
||||||
|
const duration = schedule.calculateDuration()
|
||||||
|
expect(duration).toEqual({ years: 4 })
|
||||||
|
})
|
||||||
|
test('get duration 27 days', () => {
|
||||||
|
const schedule = new Schedule('every27days')
|
||||||
|
const duration = schedule.calculateDuration()
|
||||||
|
expect(duration).toEqual({ days: 27 })
|
||||||
|
})
|
||||||
|
describe('get new Dates', () => {
|
||||||
|
test('every 27 days', () => {
|
||||||
|
const schedule = new Schedule('every27days')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-28')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 weeks', () => {
|
||||||
|
const schedule = new Schedule('every2weeks')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-15')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 1 month', () => {
|
||||||
|
const schedule = new Schedule('every1months')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-02-01')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 4 year', () => {
|
||||||
|
const schedule = new Schedule('every4years')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2026-01-01')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 days', () => {
|
||||||
|
const schedule = new Schedule('every2days')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-03')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 days', () => {
|
||||||
|
const schedule = new Schedule('Every2DAys')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-03')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 day[s]', () => {
|
||||||
|
const schedule = new Schedule('Every2DAy')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-03')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
})
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"extends":"@tsconfig/recommended/tsconfig.json",
|
"extends":"@tsconfig/recommended/tsconfig.json",
|
||||||
"exclude":["node_modules","**/*.test.ts"],
|
"exclude":["node_modules"],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
|
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
|
||||||
|
Reference in New Issue
Block a user