Compare commits

..

4 Commits

Author SHA1 Message Date
mightypanders
c58c3c61a8 tests
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-04-21 19:46:13 +02:00
mightypanders
85770a6031 date based repetition 2022-04-21 19:46:03 +02:00
mightypanders
19f0dccaec tsconfig 2022-04-21 19:45:47 +02:00
mightypanders
ca50a337e5 update nodemon script 2022-04-21 19:45:04 +02:00
8 changed files with 230 additions and 15 deletions

View File

@ -7,7 +7,7 @@
"build": "tsc",
"clean": "rimraf build",
"watch": "yarn run clean && yarn run build -- -w",
"monitor": "nodemon build/index.js",
"monitor": "nodemon index.ts",
"start": "node build/index.js",
"buildstart": "yarn run build && node build/index.js",
"lint": "eslint . --ext .ts --fix",
@ -30,6 +30,7 @@
"discord.js": "^13.6.0",
"dotenv": "^16.0.0",
"express": "^4.17.1",
"ts-node": "^10.7.0",
"typescript": "^4.4.4",
"winston": "^3.3.3"
},
@ -37,8 +38,8 @@
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^5.3.0",
"@typescript-eslint/parser": "^5.3.0",
"jest": "^27.3.1",
"eslint": "^8.2.0",
"jest": "^27.3.1",
"jest-cli": "^27.3.1",
"nodemon": "^2.0.14",
"rimraf": "^3.0.2",

View File

@ -1,9 +1,8 @@
import { GuildScheduledEvent } from "discord.js"
import { repetitionMarkerIsFound } from "../handler/repeatingEvents/helper"
import { handleRepeatingEvent } from "../handler/repeatingEvents/repeatingEvents.controller"
const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
export const name = 'guildScheduledEventUpdate'
export function execute(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
console.dir(oldguildScheduledEvent)

View File

@ -1,8 +1,14 @@
import { format } from "date-fns"
import add from "date-fns/add"
import { DateResolvable, GuildScheduledEvent } from "discord.js"
import { DateResolvable, Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
import { findInScheduleTypes } from "../../helper/typeFind"
import { Maybe } from "../../interfaces"
import { RepetitonInfo, 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 {
const lines = description.split(`\n`)
@ -11,10 +17,12 @@ export function getRepetitonInfo(description: string): RepetitonInfo {
throw new Error('Cant find repetition string')
const schedule: supportedSchedule = determineSchedule(repetitionString)
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
const endDate = determineEndDate(repetitionString)
return {
totalAmount,
alreadyOccured,
schedule
schedule,
endDate
}
}
@ -36,6 +44,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}`
}
@ -71,3 +81,25 @@ export function getNewScheduledStart(oldguildScheduledEvent: GuildScheduledEvent
const newDate = add(oldDate, duration)
return newDate
}
function determineEndDate(description: string): Maybe<Date> {
const segments = description.split(':')
if (segments.length === 3) {
// rep:sched:countOrDate
const segmentValue = segments[2]
if (segmentValue.includes('/')) {
if (segmentValue.match(/\//g) || [].length !== 2) {
return
}
}
const dateValue = new Date(segmentValue)
return dateValue
}
else if (segments.length === 4) {
// rep:sched:count:date
const segmentValue = segments[3]
const dateValue = new Date(segmentValue)
return dateValue
}
return
}

View File

@ -1,8 +1,16 @@
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
import { RepetitonInfo } from "../../types/scheduledEventTypes"
import { addRepetitonStringToEventDescription, buildNewRepetitionString, getNewScheduledStart, getRepetitonInfo } from "./helper"
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getNewScheduledStart, 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) {
if (!oldguildScheduledEvent.description) throw new Error('Event has no description -> cant handle this')
@ -20,9 +28,12 @@ export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent
channel: oldguildScheduledEvent.channel?.id,
reason: 'Repetition'
}
newguildScheduledEvent.guild?.scheduledEvents.create(newEventOptions)
if (!newguildScheduledEvent.guild) throw new Error('No guild on event?')
createEventInGuild(newguildScheduledEvent.guild, newEventOptions)
} catch (err) {
console.error(err)
}
} else {
console.log('Event does not need to be repeated')
}
}

View File

@ -0,0 +1,30 @@
import { getRepetitonInfo } from "../server/handler/repeatingEvents/helper"
describe('ScheduledEvent Creation Events', () => {
test('Daily Event with absolute end date', () => {
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)
expect(rInfo).toBeDefined()
expect(rInfo.endDate).toBeDefined()
expect(rInfo.endDate).toEqual(new Date("2022-05-22"))
expect(rInfo.schedule).toEqual('daily')
})
})

View File

@ -1,7 +1,9 @@
import { findInScheduleTypes } from '../server/helper/typeFind'
import { supportedSchedule } from '../server/types/scheduledEventTypes'
import { getRepetitonInfo } from '../server/handler/repeatingEvents/helper'
import { createEventInGuild, 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'
@ -33,3 +35,62 @@ describe('Parsing of Repetition Info from Description String',()=>{
})
})
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/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)
})

View File

@ -1,6 +1,6 @@
{
"extends":"@tsconfig/recommended/tsconfig.json",
"exclude":["node_modules","**/*.test.ts"],
"exclude":["node_modules"],
"compilerOptions": {
/* Basic Options */
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,

View File

@ -298,6 +298,18 @@
resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9"
integrity sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==
"@cspotcode/source-map-consumer@0.8.0":
version "0.8.0"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz#33bf4b7b39c178821606f669bbc447a6a629786b"
integrity sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==
"@cspotcode/source-map-support@0.7.0":
version "0.7.0"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz#4789840aa859e46d2f3173727ab707c66bf344f5"
integrity sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==
dependencies:
"@cspotcode/source-map-consumer" "0.8.0"
"@dabh/diagnostics@^2.0.2":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@dabh/diagnostics/-/diagnostics-2.0.3.tgz#7f7e97ee9a725dffc7808d93668cc984e1dc477a"
@ -635,6 +647,26 @@
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==
"@tsconfig/node10@^1.0.7":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.8.tgz#c1e4e80d6f964fbecb3359c43bd48b40f7cadad9"
integrity sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==
"@tsconfig/node12@^1.0.7":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.9.tgz#62c1f6dee2ebd9aead80dc3afa56810e58e1a04c"
integrity sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==
"@tsconfig/node14@^1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.1.tgz#95f2d167ffb9b8d2068b0b235302fafd4df711f2"
integrity sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==
"@tsconfig/node16@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==
"@tsconfig/recommended@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@tsconfig/recommended/-/recommended-1.0.1.tgz#7619bad397e06ead1c5182926c944e0ca6177f52"
@ -932,12 +964,17 @@ acorn-walk@^7.1.1:
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-7.2.0.tgz#0de889a601203909b0fbe07b8938dc21d2e967bc"
integrity sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA==
acorn-walk@^8.1.1:
version "8.2.0"
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.2.0.tgz#741210f2e2426454508853a2f44d0ab83b7f69c1"
integrity sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==
acorn@^7.1.1:
version "7.4.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.4.1.tgz#feaed255973d2e77555b83dbc08851a6c63520fa"
integrity sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==
acorn@^8.2.4, acorn@^8.7.0:
acorn@^8.2.4, acorn@^8.4.1, acorn@^8.7.0:
version "8.7.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.0.tgz#90951fde0f8f09df93549481e5fc141445b791cf"
integrity sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==
@ -1005,6 +1042,11 @@ anymatch@^3.0.3, anymatch@~3.1.2:
normalize-path "^3.0.0"
picomatch "^2.0.4"
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
@ -1425,6 +1467,11 @@ cors@^2.8.5:
object-assign "^4"
vary "^1"
create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@ -1553,6 +1600,11 @@ diff-sequences@^27.5.1:
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-27.5.1.tgz#eaecc0d327fd68c8d9672a1e64ab8dccb2ef5327"
integrity sha512-k1gCAXAsNgLwEL+Y8Wvl+M6oEFj5bgazfZULpS5CneoPPXRaCCW7dm+q21Ky2VEE5X+VeRDBVg1Pcvvsr4TtNQ==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
dir-glob@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f"
@ -3047,7 +3099,7 @@ make-dir@^3.0.0:
dependencies:
semver "^6.0.0"
make-error@1.x:
make-error@1.x, make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
@ -3960,6 +4012,25 @@ ts-mixer@^6.0.0:
resolved "https://registry.yarnpkg.com/ts-mixer/-/ts-mixer-6.0.0.tgz#4e631d3a36e3fa9521b973b132e8353bc7267f9f"
integrity sha512-nXIb1fvdY5CBSrDIblLn73NW0qRDk5yJ0Sk1qPBF560OdJfQp9jhl+0tzcY09OZ9U+6GpeoI9RjwoIKFIoB9MQ==
ts-node@^10.7.0:
version "10.7.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.7.0.tgz#35d503d0fab3e2baa672a0e94f4b40653c2463f5"
integrity sha512-TbIGS4xgJoX2i3do417KSaep1uRAW/Lu+WAL2doDHC0D6ummjirVOXU5/7aiZotbQ5p1Zp9tP7U6cYhA0O7M8A==
dependencies:
"@cspotcode/source-map-support" "0.7.0"
"@tsconfig/node10" "^1.0.7"
"@tsconfig/node12" "^1.0.7"
"@tsconfig/node14" "^1.0.0"
"@tsconfig/node16" "^1.0.2"
acorn "^8.4.1"
acorn-walk "^8.1.1"
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
v8-compile-cache-lib "^3.0.0"
yn "3.1.1"
tslib@^1.8.1:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
@ -4092,6 +4163,11 @@ utils-merge@1.0.1:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
v8-compile-cache-lib@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==
v8-compile-cache@^2.0.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"
@ -4297,6 +4373,11 @@ yargs@^16.2.0:
y18n "^5.0.5"
yargs-parser "^20.2.2"
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==
zod@^3.11.6:
version "3.13.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.13.4.tgz#5d6fe03ef4824a637d7ef50b5441cf6ab3acede0"