Compare commits

...

48 Commits

Author SHA1 Message Date
77aaca2764 add @types/node
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-05-05 22:31:19 +02:00
408ac9d8b3 remove unnecessary dependencies and code
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-05-05 22:29:33 +02:00
35353cdd58 [CI SKIP] add badge 2022-05-05 21:52:05 +02:00
f8afee69bd send dm on failure to parse
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-05-05 21:39:53 +02:00
9ae406dfb3 handle test behaviour on start up 2022-05-05 21:39:10 +02:00
8e8c18b3c1 remove repetition type finder 2022-05-05 21:38:38 +02:00
2f2817897f handle dates more explicitly 2022-05-05 21:38:10 +02:00
0c63b20ffe dont start express server 2022-05-05 21:34:51 +02:00
aac7967037 upgrade all npm packages to latest version
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-04-24 15:51:28 +02:00
23e3bbc4b1 major overhaul of schedule parsing code 2022-04-24 15:51:07 +02:00
22f0117d30 schedule parsing test cases 2022-04-24 15:50:40 +02:00
790a0d65f4 use repetitionstring validator in new and updated events
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-04-23 21:32:32 +02:00
b22cb1167d implement repetitionstring validator
parses repetition string and sends a message on fail to the creator
which contains the offending error code
2022-04-23 21:31:47 +02:00
0d28fce640 remove unused property 2022-04-23 21:31:09 +02:00
dec3a3070f add CustomError Class as wrapper for "Error"
throw new CustomError('message','errorCode')
2022-04-23 21:30:11 +02:00
99386b1662 adjust an error message 2022-04-23 21:29:35 +02:00
6ff3e68921 adjust npm scripts 2022-04-23 21:26:08 +02:00
c58c3c61a8 tests
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-04-21 19:46:13 +02:00
85770a6031 date based repetition 2022-04-21 19:46:03 +02:00
19f0dccaec tsconfig 2022-04-21 19:45:47 +02:00
ca50a337e5 update nodemon script 2022-04-21 19:45:04 +02:00
72b88b8387 add monthly as day increment option
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-04-13 21:52:32 +02:00
d79abb3d93 fix dockerfile
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-04-13 21:41:02 +02:00
180c467826 refactoring and adding capability to recognize more schedules
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-04-13 21:38:59 +02:00
9df18575fd implement auto repeating events
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-04-12 22:39:47 +02:00
10bc4ae5df update interfaces
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-04-09 23:02:08 +02:00
4dad1b9ad8 register commands to guilds the bot is in
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-23 21:59:27 +01:00
9b2ddc35fa big update of structure
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-09 12:29:42 +01:00
2f1cdbbaf6 remove unneccessary build step
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-06 18:22:04 +01:00
828a8e7445 fix start command for dockerfile
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-03-06 18:20:15 +01:00
73090f71a1 correct tag in build.sh 2022-03-06 18:20:05 +01:00
b7d778ff79 update lock
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-06 18:00:12 +01:00
cd256f44ed slim dependencies, modify scripts 2022-03-06 18:00:06 +01:00
402295c610 add docker build files 2022-03-06 17:59:45 +01:00
166836da4d update secrets
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-03-06 17:15:00 +01:00
bb37aeb410 update secrets 2022-03-06 17:12:32 +01:00
f8535929ac update secrets
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-06 13:57:23 +01:00
15a3b8f39c update secrets
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-06 13:56:29 +01:00
4b064de1c0 update secrets 2022-03-06 13:56:04 +01:00
921ecc73b3 update secrets
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-06 13:54:58 +01:00
26ffb50886 update secrets
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-06 13:48:15 +01:00
2896149a33 update secrets 2022-03-06 13:44:41 +01:00
8115d4ba81 try to add secrets 2022-03-06 13:41:30 +01:00
a60f35459c use event handling code
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-03-05 01:24:22 +01:00
03d9b43c44 add event handling code 2022-03-05 01:24:05 +01:00
6ce9d8db46 lock 2022-03-05 01:23:50 +01:00
bbbefc8a6c update tsconfig 2022-03-05 01:23:38 +01:00
54fc2f4ab6 update all packages 2022-03-05 01:23:18 +01:00
38 changed files with 1713 additions and 1564 deletions

View File

@ -1,6 +1,7 @@
pipeline:
build:
image: node
commands:
- yarn install
- yarn run clean
docker:
image: plugins/docker
settings:
registry: registry.brudi.xyz
repo: registry.brudi.xyz/kenobi/node-event-bot
tags: latest

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM node:alpine as Build
ENV NODE_ENV=production
WORKDIR /app
COPY [ "yarn.lock", "package.json", "index.ts", "tsconfig.json", "./" ]
COPY server ./server
RUN yarn install --production
RUN yarn run build
CMD ["yarn","start"]

View File

@ -1,3 +1,6 @@
## Build Status
[![status-badge](https://wood.brudi.xyz/api/badges/kenobi/node-event-bot/status.svg)](https://wood.brudi.xyz/kenobi/node-event-bot)
## Requirements
- yarn
- npm

1
dockerbuild.sh Executable file
View File

@ -0,0 +1 @@
docker build --tag node-event-bot:latest .

View File

@ -1,13 +1,3 @@
import express, { Application } from "express"
import { config } from "./server/configuration"
import DiscordAdapter from "./server/discordAdapter"
import Routes from "./server/routes"
import Server from "./server/server"
const server = Server.init(config.port)
server.start(() => {
console.log(`Server running on port ${server.getPort()}`)
new Routes().setRoutes(server.getApp())
const discordAdapter = new DiscordAdapter()
})
import { ExtendedClient } from "./server/structures/client"
export const client = new ExtendedClient()
client.start()

View File

@ -8,7 +8,8 @@
"clean": "rimraf build",
"watch": "yarn run clean && yarn run build -- -w",
"monitor": "nodemon build/index.js",
"start": "yarn run build && node build/index.js",
"start": "node build/index.js",
"buildstart": "yarn run build && node build/index.js",
"lint": "eslint . --ext .ts --fix",
"test": "jest",
"test-watch": "jest --watch",
@ -17,26 +18,24 @@
"author": "",
"license": "ISC",
"dependencies": {
"@discordjs/rest": "^0.1.0-canary.0",
"axios": "^0.24.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"discord-api-types": "^0.24.0",
"@discordjs/rest": "^0.4.1",
"@tsconfig/recommended": "^1.0.1",
"@types/node": "^17.0.31",
"axios": "^0.26.0",
"date-fns": "^2.28.0",
"discord-api-types": "^0.31.2",
"discord.js": "^13.6.0",
"dotenv": "^10.0.0",
"eslint": "^8.2.0",
"express": "^4.17.1",
"jest": "^27.3.1",
"dotenv": "^16.0.0",
"ts-node": "^10.7.0",
"typescript": "^4.4.4",
"winston": "^3.3.3"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.1",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^5.3.0",
"@typescript-eslint/parser": "^5.3.0",
"eslint": "^8.2.0",
"jest": "^27.3.1",
"jest-cli": "^27.3.1",
"nodemon": "^2.0.14",
"rimraf": "^3.0.2",

View File

@ -1,25 +0,0 @@
import RegistrationHandler from "./RegistrationHandler"
export default class MuteHandler {
public mute(player: string): boolean {
const register = RegistrationHandler.Instance
const binding = register.getNameRegisteredForSteamUser(player)
console.log(`Performing mute wizardry on ${player}, ${JSON.stringify(binding)}`)
return true
}
public unmute(player: string): boolean {
const register = RegistrationHandler.Instance
const binding = register.getNameRegisteredForSteamUser(player)
console.log(`Performing unmute wizardry on ${player}`)
return true
}
public unmuteAll(): boolean {
const register = RegistrationHandler.Instance
const binding = register.getAllMappings()
console.log(`Performing unmute wizardry on all players`)
return true
}
}

View File

@ -1,55 +0,0 @@
import { GuildMember } from "discord.js"
import { Maybe, userNameBinding } from "./interfaces"
export default class RegistrationHandler {
private userRegister: userNameBinding[] = []
private static _instance: RegistrationHandler
public constructor() {
console.log('Setup RegistrationHandler')
}
public static get Instance() {
return this._instance || (this._instance = new this())
}
public register(discordUser: GuildMember, steamname: string): boolean {
try {
const binding: userNameBinding = {
Steam: steamname,
DiscordUser: discordUser
}
console.log(`Trying to register ${JSON.stringify(binding)}`)
let alreadyPresentBinding = this.userRegister.find(x => x.DiscordUser.user.username == binding.DiscordUser.user.username)
if (alreadyPresentBinding) {
console.log(`Binding already present: ${JSON.stringify(alreadyPresentBinding)}, overwriting.`)
alreadyPresentBinding = binding
}
else {
this.userRegister.push(binding)
console.log(`Binding successfully added.`)
}
} catch (error) {
console.error(error)
return false
}
return true
}
public getAllMappings(): userNameBinding[] {
return this.userRegister
}
public removeUser(discordUser: GuildMember): void {
this.userRegister = this.userRegister.filter(x => x.DiscordUser.user.id !== discordUser.user.id)
}
public getNameRegisteredForDiscordUser(discordUser: GuildMember): Maybe<userNameBinding> {
return this.userRegister.find(x => x.DiscordUser.user.id == discordUser.user.id)
}
public getNameRegisteredForSteamUser(steamUser: string): Maybe<userNameBinding> {
return this.userRegister.find(x => x.Steam == steamUser)
}
public listRegisteredMembers(): string {
const output = this.userRegister.map(x => `${x.DiscordUser.user.username} : ${x.Steam}\n`)
return output.join()
}
private printHelpText(): string { return "" }
private buildHelpText(): string { return "" }
}

18
server/commands/echo.ts Normal file
View File

@ -0,0 +1,18 @@
import { Command } from '../structures/command'
import { RunOptions } from '../types/commandTypes'
export default new Command({
name: 'echo',
description: 'Echoes a text',
options: [
{
name: 'echo',
description: 'The text to echo',
type: 'STRING',
required: true
}
],
run: async (interaction: RunOptions) => {
console.log('echo called')
interaction.interaction.reply(interaction.toString())
}
})

View File

@ -0,0 +1,35 @@
import { client } from '../..'
import { Command } from '../structures/command'
import { RunOptions } from '../types/commandTypes'
export default new Command({
name: 'list',
description: 'Lists upcoming events',
options: [
{
name: 'count',
description: 'The max amount of events to list',
type: 'INTEGER',
required: false,
minValue: 1,
maxValue: 100,
}
],
run: async (opt: RunOptions) => {
console.dir(opt)
const interactionGuild = opt.interaction.guild
const events = interactionGuild?.scheduledEvents.cache
let output = ''
if (!events?.values()) {
opt.interaction.followUp('No events to list')
return
}
const amount = 0
if (opt.interaction.options.get('count'))
for (const e of events.values()) {
output += e.toString()
output += `\n`
}
opt.interaction.followUp(output)
}
})

9
server/commands/pong.ts Normal file
View File

@ -0,0 +1,9 @@
import { Command } from '../structures/command'
export default new Command({
name: 'ping',
description: 'Does a pong, duh',
run: async ({ interaction }) => {
console.log(`Ping interaction received.`)
interaction.followUp('pong')
}
})

View File

@ -1,76 +0,0 @@
import { REST } from '@discordjs/rest'
import { Routes } from 'discord-api-types/v9'
import { config } from './configuration'
import { Client, Collection, CommandInteraction, GuildMember, GuildScheduledEvent, GuildScheduledEventManager, Intents, Snowflake } from 'discord.js'
import RegistrationHandler from './RegistrationHandler'
import { discordCommand } from './interfaces'
import eventHandler from './eventHandler'
export default class DiscordAdapter {
private rest: REST
private client: Client
private registration: RegistrationHandler
public constructor() {
this.rest = new REST({ version: '9' }).setToken(config.token)
this.client = new Client({ intents: [Intents.FLAGS.GUILDS] })
this.registration = RegistrationHandler.Instance
this.setupCallbacks(this.client)
this.client.login(config.token)
this.registerCommands(this.commandList)
}
public async registerCommands(pCommands: discordCommand[]) {
try {
console.log('Refreshing slash commands')
await this.rest.put(Routes.applicationGuildCommands(config.client_id, config.guild_id), { body: pCommands })
console.log('Successfully refreshed slash commands')
} catch (error) {
console.log(`Error refreshing slash commands: ${error}`)
}
}
private setupCallbacks(c: Client) {
c.on('ready', () => {
console.log(`Logged in as ${c.user?.tag}`)
})
c.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return
const interactionCommand = this.commandList.find(x => x.name == interaction.commandName)
if (interactionCommand)
interactionCommand.performCommand(interaction, this.registration)
})
}
public async showNext(interaction: CommandInteraction): Promise<void> {
const guild = interaction.guild
if (!guild) {
console.log(`There is no guild here`)
return
}
const output = new eventHandler().getNextEvent(guild)
await interaction.reply(output)
return
}
public async listEvents(interaction: CommandInteraction): Promise<void> {
const guild = interaction.guild
if (!guild) {
console.log(`There is no guild here`)
return
}
const output = new eventHandler().listAllEvents(guild)
await interaction.reply(output)
}
public commandList: discordCommand[] = [
{
name: "shownext",
description: "Shows next Events",
performCommand: this.showNext
},
{
name: "listevents",
description: "Lists all Events",
performCommand: this.listEvents
}
]
}

View File

@ -0,0 +1,5 @@
export const name = 'channelCreate'
export function execute(channel: any) {
console.log(`${JSON.stringify(channel)} has been Created.`)
}

View File

@ -0,0 +1,11 @@
import { GuildScheduledEvent } from 'discord.js'
import { validateRepetitionStringAndSendMessageOnFail } from '../handler/repeatingEvents/helper'
export const name = 'guildScheduledEventCreate'
export async function execute(guildScheduledEvent: GuildScheduledEvent) {
try {
console.log(`${JSON.stringify(guildScheduledEvent)} has been created.`)
validateRepetitionStringAndSendMessageOnFail(guildScheduledEvent)
} catch (error) {
console.error(error)
}
}

View File

@ -0,0 +1,4 @@
export const name = 'guildScheduledEventDelete'
export function execute(guildScheduledEvent: any) {
console.log(`${JSON.stringify(guildScheduledEvent)} has been Deleted.`)
}

View File

@ -0,0 +1,23 @@
import { GuildScheduledEvent } from "discord.js"
import { config } from "../configuration"
import { repetitionMarkerIsFound, validateRepetitionStringAndSendMessageOnFail } from "../handler/repeatingEvents/helper"
import { handleRepeatingEvent } from "../handler/repeatingEvents/repeatingEvents.controller"
export const name = 'guildScheduledEventUpdate'
export function execute(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
if (config.debug) {
console.dir(oldguildScheduledEvent)
console.dir(newguildScheduledEvent)
}
if (oldguildScheduledEvent.description !== newguildScheduledEvent.description) {
validateRepetitionStringAndSendMessageOnFail(newguildScheduledEvent)
}
if (oldguildScheduledEvent.description && repetitionMarkerIsFound(oldguildScheduledEvent.description)) {
// valid repeating event
if (newguildScheduledEvent.status === 'COMPLETED')
handleRepeatingEvent(oldguildScheduledEvent, newguildScheduledEvent)
}
}

View File

@ -0,0 +1,38 @@
import { CommandInteraction, CommandInteractionOptionResolver } from "discord.js"
import { ExtendedInteraction } from "../types/commandTypes"
import { Event } from '../structures/event'
import { client } from "../.."
/**
export default new Event('interactionCreate', async (interaction) => {
console.log(`Interaction has been created, ${JSON.stringify(interaction)}`)
if (interaction.isCommand()) {
await interaction.deferReply()
const command = client.commands.get(interaction.commandName)
if (!command)
return interaction.followUp('You have used a non existant command')
command.run({
args: interaction.options as CommandInteractionOptionResolver,
client: client,
interaction: interaction as ExtendedInteraction
})
}
})
*/
export const name = 'interactionCreate'
export async function execute(interaction: ExtendedInteraction) {
//console.dir(interaction, { depth: null })
if (interaction.isCommand()) {
console.log(`Interaction is a command.`)
await interaction.deferReply()
const command = client.commands.get(interaction.commandName)
if (!command)
return interaction.followUp('Invalid command')
command.run({
args: interaction.options as CommandInteractionOptionResolver,
client,
interaction
})
}
}

View File

@ -0,0 +1,4 @@
export const name = 'messageCreate'
export function execute(message: any) {
console.log(`${JSON.stringify(message)} has been created`)
}

4
server/events/ready.ts Normal file
View File

@ -0,0 +1,4 @@
export const name = 'ready'
export function execute(client: any) {
console.log(`Processing ready: ${JSON.stringify(client)} has been created.`)
}

View File

@ -0,0 +1,119 @@
import { format } from "date-fns"
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"
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`)
const repetitionString = lines.find(x => x.startsWith('$rep:'))
if (!repetitionString)
throw new CustomError('Cant find repetition string', errorCodes.no_string_present)
const scheduleString = determineScheduleString(repetitionString)
const schedule: Schedule = new Schedule(scheduleString)
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
const endDate = determineEndDate(repetitionString)
return {
totalAmount,
alreadyOccured,
schedule,
endDate
}
}
export function determineScheduleString(repetitionLine: string): supportedSchedule {
const segments = repetitionLine.split(':')
const scheduleSegment = segments[1]
if (scheduleSegment)
return scheduleSegment
else
throw new CustomError('No schedule segment found', errorCodes.no_schedule)
}
export function determineRepetitionCount(description: string): { totalAmount: number; alreadyOccured: number } {
const segments = description.split(':')
const amountSegment = segments[2]
if (amountSegment) {
const amounts = amountSegment.split('/')
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) {
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 {
const lines = oldguildScheduledEvent.split(`\n`)
const repLineIndex = lines.findIndex(x => x.startsWith('$rep:'))
const newLines = lines.filter((_, index) => repLineIndex !== index)
newLines.push(newRepetitonString)
return newLines.join('\n')
}
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
}
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)
}
}

View File

@ -0,0 +1,44 @@
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
import { RepetitonInfo } from "../../types/scheduledEventTypes"
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getRepetitonInfo } from "./helper"
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')
const repetitionInfo = getRepetitonInfo(oldguildScheduledEvent.description)
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: newDate,
privacyLevel: oldguildScheduledEvent.privacyLevel,
entityType: oldguildScheduledEvent.entityType,
channel: oldguildScheduledEvent.channel?.id,
reason: 'Repetition'
}
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,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)
}

View File

@ -1,5 +1,3 @@
import { CommandInteraction, GuildMember } from "discord.js"
import RegistrationHandler from "./RegistrationHandler"
export type Maybe<T> = T | undefined
export interface Player {
@ -8,17 +6,24 @@ export interface Player {
export type supported_languages = "german" | "english"
export interface localized_string {
[k: string]: {
german: string,
english: string
[k in supported_languages]: string
}
}
export interface userNameBinding {
Steam: string,
DiscordUser: GuildMember
export class CustomError extends Error {
private code: string
public constructor(message: string, errorCode: string) {
super(message)
this.code = errorCode
}
export interface discordCommand {
name: string,
description: string
options?: any[]
performCommand(interaction: CommandInteraction, registration: RegistrationHandler): Promise<void>
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",
}

0
server/logger.ts Normal file
View File

View File

@ -1,36 +0,0 @@
import express from "express";
import MuteHandler from "./MuteHandler";
export default class Routes {
public constructor(
private muteHandler = new MuteHandler()
) { }
public setRoutes(app: express.Application): void {
app.route('').get(this.landingPage.bind(this))
app.route('/').get(this.landingPage.bind(this))
app.route('/mute').post(this.mutePlayer.bind(this))
app.route('/unmute/all').post(this.unmuteAll.bind(this))
app.route('/unmute/:id').post(this.unmutePlayer.bind(this))
}
private async mutePlayer(req: express.Request, res: express.Response): Promise<void> {
const playerName = req.body.name
console.log(`Muting player ${playerName}`)
this.muteHandler.mute(playerName)
res.status(200).json()
}
private async unmuteAll(_: express.Request, res: express.Response): Promise<void> {
console.log(`Unmuting all players`)
this.muteHandler.unmuteAll()
res.status(200).json()
}
private async unmutePlayer(req: express.Request, res: express.Response): Promise<void> {
const playerName = req.body.name
console.log(`Unmuting player ${playerName}`)
this.muteHandler.unmute(playerName)
res.status(200).json()
}
private async landingPage(_: express.Request, res: express.Response): Promise<void> {
res.send('Hello World')
}
}

View File

@ -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())
}
}

View File

@ -0,0 +1,83 @@
import { ApplicationCommandDataResolvable, Client, Collection, Guild, Intents, Snowflake } from "discord.js";
import { CommandType } from "../types/commandTypes";
import fs from 'fs'
import { config } from "../configuration";
export class ExtendedClient extends Client {
private eventFilePath = `${__dirname}/../events`
private commandFilePath = `${__dirname}/../commands`
public commands: Collection<string, CommandType> = new Collection()
public constructor() {
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())
Promise.all(promises).then(() => {
this.login(config.token)
})
}
private async importFile(filepath: string): Promise<any> {
console.debug(`Importing ${filepath}`)
const imported = await import(filepath)
console.debug(`Imported ${JSON.stringify(imported)}`)
return imported.default ?? imported
}
public async registerCommands(cmds: ApplicationCommandDataResolvable[], guildIds: Collection<Snowflake, Guild>) {
if (guildIds) {
guildIds.forEach(guild => {
this.guilds.cache.get(guild.id)?.commands.set(cmds)
console.log(`Registering commands to ${guild}|${guild.id}`)
})
} else {
this.application?.commands.set(cmds)
console.log(`Registering global commands`)
}
return
}
public async registerSlashCommands() {
try {
const slashCommands: ApplicationCommandDataResolvable[] = []
const commandFiles = fs.readdirSync(this.commandFilePath).filter(file => file.endsWith('.ts') || file.endsWith('.js'))
for (const commandFile of commandFiles) {
const filePath = `${this.commandFilePath}/${commandFile}`
const command = await this.importFile(filePath)
console.debug(JSON.stringify(command))
if (!command.name) return
console.debug(command)
this.commands.set(command.name, command)
slashCommands.push(command)
}
this.on("ready", (client: Client) => {
console.log(`Ready processing ${JSON.stringify(client)}`)
console.log(`SlashCommands: ${JSON.stringify(slashCommands)}`)
const guilds = client.guilds.cache
this.registerCommands(slashCommands, guilds)
})
} catch (error) {
console.log(`Error refreshing slash commands: ${error}`)
}
}
public async registerEventCallback() {
try {
const eventFiles = fs.readdirSync(this.eventFilePath).filter(file => file.endsWith('.ts') || file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = `${this.eventFilePath}/${file}`
const event = await this.importFile(filePath)
if (event.once) {
console.log(`Registering once ${file}`)
this.once(event.name, (...args: any[]) => event.execute(...args))
}
else {
console.log(`Registering on ${file}`)
this.on(event.name, (...args: any[]) => event.execute(...args))
}
}
console.log(this.eventNames())
} catch (error) {
console.error(error)
}
}
}

View File

@ -0,0 +1,7 @@
import { CommandType } from "../types/commandTypes";
export class Command {
constructor(commandOptions: CommandType) {
Object.assign(this, commandOptions)
}
}

View File

@ -0,0 +1,8 @@
import { ClientEvents } from "discord.js";
export class Event<Key extends keyof ClientEvents>{
constructor(
public event: Key,
public run: (...args: ClientEvents[Key]) => any
) { }
}

6
server/types/clients.ts Normal file
View File

@ -0,0 +1,6 @@
import { ApplicationCommandDataResolvable } from "discord.js"
export interface RegisterCommandOptions {
guildId?: string
commands: ApplicationCommandDataResolvable[]
}

View File

@ -0,0 +1,17 @@
import { PermissionResolvable, ChatInputApplicationCommandData, CommandInteraction, CommandInteractionOptionResolver, GuildMember } from "discord.js";
import { ExtendedClient } from "../structures/client";
export interface ExtendedInteraction extends CommandInteraction {
member: GuildMember
}
export interface RunOptions {
client: ExtendedClient
interaction: ExtendedInteraction
args: CommandInteractionOptionResolver
}
type RunFunction = (options: RunOptions) => any
export type CommandType = {
userPermissions?: PermissionResolvable[]
run: RunFunction
} & ChatInputApplicationCommandData

View File

@ -0,0 +1,102 @@
import { add } from "date-fns"
import { DateResolvable } from "discord.js"
import { CustomError, errorCodes } from "../interfaces"
export interface RepetitonInfo {
startDate?: Date, // If defined will take precedence over repetitonAmount
endDate?: Date,// If defined will take precedence over repetitonAmount
totalAmount: number,
alreadyOccured: number,
schedule: Schedule
}
export const scheduleNames = ['daily', 'weekly', 'monthly', 'everyNWeeks', 'everyNDays', 'everyNMonths']
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
}
}

View 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)
})
})

View File

@ -1,91 +0,0 @@
import { GuildMember } from "discord.js"
import RegistrationHandler from "../server/RegistrationHandler"
const guildMember: GuildMember = <GuildMember><unknown>{
guild: {
id: 'guild_id',
name: 'Bot Playground',
icon: null,
features: [],
commands: { permissions: [], guild: [] },
members: { guild: [] },
channels: { guild: [] },
bans: { guild: [] },
roles: { guild: [] },
presences: {},
voiceStates: { guild: [] },
stageInstances: { guild: [] },
invites: { guild: [] },
deleted: false,
available: true,
shardId: 0,
splash: null,
banner: null,
description: null,
verificationLevel: 'NONE',
vanityURLCode: null,
nsfwLevel: 'DEFAULT',
discoverySplash: null,
memberCount: 2,
large: false,
applicationId: null,
afkTimeout: 300,
afkChannelId: null,
systemChannelId: 'channel_id',
premiumTier: 'NONE',
premiumSubscriptionCount: 0,
explicitContentFilter: 'DISABLED',
mfaLevel: 'NONE',
joinedTimestamp: 1636540056755,
defaultMessageNotifications: 'ALL_MESSAGES',
systemChannelFlags: { bitfield: 0 },
maximumMembers: 250000,
maximumPresences: null,
approximateMemberCount: null,
approximatePresenceCount: null,
vanityURLUses: null,
rulesChannelId: null,
publicUpdatesChannelId: null,
preferredLocale: 'en-US',
ownerId: 'ownerID',
emojis: { guild: [] },
stickers: { guild: [] }
},
joinedTimestamp: 1636539420924,
premiumSinceTimestamp: null,
deleted: false,
nickname: null,
pending: false,
_roles: [],
user: {
id: 'user_id',
bot: false,
system: false,
flags: { bitfield: 256 },
username: 'username',
discriminator: '0965',
avatar: 'avatar_string',
banner: undefined,
accentColor: undefined
},
avatar: null
}
const registeredUser = {
"Steam": "abc",
"DiscordUser": guildMember
}
test(`Instances`, () => {
const register = RegistrationHandler.Instance
expect(register).toBeDefined()
})
test(`Registration works`, () => {
const register = RegistrationHandler.Instance
register.register(<GuildMember>guildMember, "abc")
const result = register.getAllMappings()
console.log(JSON.stringify(result))
expect(result).toBeDefined()
expect(result).toEqual([registeredUser])
})

92
tests/repetition.test.ts Normal file
View File

@ -0,0 +1,92 @@
import { Schedule } from '../server/types/scheduledEventTypes'
import { buildNewRepetitionString, getRepetitonInfo } from '../server/handler/repeatingEvents/helper'
import { RepetitonInfo } from '../server/types/scheduledEventTypes'
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: 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",
"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)
//})

View 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)
})
})

View File

@ -1,5 +1,6 @@
{
"extends":"@tsconfig/recommended/tsconfig.json",
"exclude":["node_modules"],
"compilerOptions": {
/* Basic Options */
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
@ -39,13 +40,13 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

2118
yarn.lock

File diff suppressed because it is too large Load Diff