Compare commits
5 Commits
aac7967037
...
f8afee69bd
Author | SHA1 | Date | |
---|---|---|---|
|
f8afee69bd | ||
|
9ae406dfb3 | ||
|
8e8c18b3c1 | ||
|
2f2817897f | ||
|
0c63b20ffe |
13
index.ts
13
index.ts
@ -2,12 +2,13 @@ import { config } from "./server/configuration"
|
||||
import Server from "./server/server"
|
||||
import { ExtendedClient } from "./server/structures/client"
|
||||
|
||||
const server = Server.init(config.port)
|
||||
//const server = Server.init(config.port)
|
||||
export const client = new ExtendedClient()
|
||||
|
||||
|
||||
server.start(() => {
|
||||
console.log(`Server running on port ${server.getPort()}`)
|
||||
//const discordAdapter = new DiscordAdapter()
|
||||
client.start()
|
||||
})
|
||||
|
||||
//server.start(() => {
|
||||
// console.log(`Server running on port ${server.getPort()}`)
|
||||
// //const discordAdapter = new DiscordAdapter()
|
||||
// client.start()
|
||||
//})
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { format } from "date-fns"
|
||||
import add from "date-fns/add"
|
||||
import { DateResolvable, Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
||||
import { client } from "../../.."
|
||||
import { Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
||||
import { sendFailureDM } from "../../helper/sendFailureDM"
|
||||
import { CustomError, errorCodes, Maybe } from "../../interfaces"
|
||||
import { RepetitonInfo, Schedule, supportedSchedule } from "../../types/scheduledEventTypes"
|
||||
|
||||
@ -49,8 +48,8 @@ export function determineRepetitionCount(description: string): { totalAmount: nu
|
||||
|
||||
export function buildNewRepetitionString(repetitionInfo: RepetitonInfo) {
|
||||
if (repetitionInfo.endDate)
|
||||
return `$rep:${repetitionInfo.schedule}:${format(repetitionInfo.endDate, 'yyyy-MM-dd')}`
|
||||
return `$rep:${repetitionInfo.schedule}:${repetitionInfo.alreadyOccured + 1}/${repetitionInfo.totalAmount}`
|
||||
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 {
|
||||
@ -104,6 +103,7 @@ export function checkIfRepetitionStringIsValid(description: string): string {
|
||||
}
|
||||
|
||||
export async function validateRepetitionStringAndSendMessageOnFail(event: GuildScheduledEvent): Promise<void> {
|
||||
console.log('This should not be accessed')
|
||||
|
||||
const validResponses = [
|
||||
'valid',
|
||||
@ -114,13 +114,6 @@ export async function validateRepetitionStringAndSendMessageOnFail(event: GuildS
|
||||
// do success things?
|
||||
} else {
|
||||
const creatorMessage = `The repetition string in your event could not be parsed. Reason: ${resultstring}`
|
||||
console.log(creatorMessage)
|
||||
if (!event.creatorId) throw new CustomError('No creator ID present', errorCodes.no_creator_id)
|
||||
const creator = await client.users.fetch(event.creatorId)
|
||||
console.log(`Creator ${JSON.stringify(creator)}`)
|
||||
if (creator)
|
||||
if (!creator.dmChannel)
|
||||
await creator.createDM()
|
||||
await creator.dmChannel?.send(creatorMessage)
|
||||
sendFailureDM(creatorMessage, event.creatorId ?? undefined)
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,16 @@ export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent
|
||||
if (needsToBeRepeated(repetitionInfo)) {
|
||||
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 newEventOptions: GuildScheduledEventCreateOptions = {
|
||||
name: oldguildScheduledEvent.name,
|
||||
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
|
||||
scheduledStartTime: repetitionInfo.schedule.getNewDate(oldguildScheduledEvent.scheduledStartAt),
|
||||
scheduledStartTime: newDate,
|
||||
privacyLevel: oldguildScheduledEvent.privacyLevel,
|
||||
entityType: oldguildScheduledEvent.entityType,
|
||||
channel: oldguildScheduledEvent.channel?.id,
|
||||
|
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,14 +0,0 @@
|
||||
import { CustomError, errorCodes } from "../interfaces";
|
||||
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 CustomError('That is not a schedule name.', errorCodes.schedule_not_supported);
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ export class ExtendedClient extends Client {
|
||||
super({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_SCHEDULED_EVENTS] })
|
||||
}
|
||||
public start() {
|
||||
if (process.env.NODE_ENV === 'test') return
|
||||
const promises = []
|
||||
promises.push(this.registerSlashCommands())
|
||||
promises.push(this.registerEventCallback())
|
||||
|
@ -66,6 +66,9 @@ export class Schedule {
|
||||
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'
|
||||
|
@ -3,6 +3,7 @@ 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",
|
||||
@ -21,10 +22,11 @@ describe('ScheduledEvent Creation Events', () => {
|
||||
"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('daily')
|
||||
expect(rInfo.schedule).toEqual(expectedSchedule)
|
||||
})
|
||||
|
||||
})
|
||||
|
@ -1,40 +1,35 @@
|
||||
import { findInScheduleTypes } from '../server/helper/typeFind'
|
||||
import { supportedSchedule } from '../server/types/scheduledEventTypes'
|
||||
import { createEventInGuild, getRepetitonInfo } from '../server/handler/repeatingEvents/helper'
|
||||
import { Schedule } from '../server/types/scheduledEventTypes'
|
||||
import { buildNewRepetitionString, getRepetitonInfo } from '../server/handler/repeatingEvents/helper'
|
||||
import { RepetitonInfo } from '../server/types/scheduledEventTypes'
|
||||
import { handleRepeatingEvent } from '../server/handler/repeatingEvents/repeatingEvents.controller'
|
||||
import { GuildScheduledEventCreateOptions } from 'discord.js'
|
||||
describe('Schedule names are parsed correctly', () => {
|
||||
const dailyValue: supportedSchedule = 'daily'
|
||||
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 expectedInfo: RepetitonInfo = {
|
||||
totalAmount: 3,
|
||||
alreadyOccured: 1,
|
||||
schedule: 'daily'
|
||||
schedule: new Schedule('daily')
|
||||
}
|
||||
|
||||
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",
|
||||
@ -73,24 +68,25 @@ const newEvent = {
|
||||
"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)
|
||||
})
|
||||
//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)
|
||||
//})
|
||||
|
Loading…
Reference in New Issue
Block a user