bot base
This commit is contained in:
parent
8dd89f151e
commit
3d2022d1dd
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
node_modules/
|
||||
build/
|
||||
node_modules
|
||||
build
|
||||
.env
|
||||
|
26
index.ts
Normal file
26
index.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { ExtendedClient } from "./server/structures/client"
|
||||
export const client = new ExtendedClient()
|
||||
|
||||
import { AuthenticateUserByNameRequest, CreateUserByNameRequest } from "./jellyfin/api"
|
||||
import { UserApi } from "./jellyfin/api/userApi"
|
||||
import { config } from "./server/configuration"
|
||||
client.start()
|
||||
|
||||
async function start() {
|
||||
const jfUserAPI: UserApi = new UserApi(config.jellyfin_url)
|
||||
const options = {
|
||||
headers: {
|
||||
"X-MediaBrowser-Token": config.jellfin_token
|
||||
}
|
||||
}
|
||||
let u = (await jfUserAPI.getUsers(false, false, options)).body
|
||||
console.log(JSON.stringify(u.map(x => x.name), null, 2))
|
||||
const user: CreateUserByNameRequest = {
|
||||
name: "Testuser1",
|
||||
password: "1234"
|
||||
}
|
||||
const a = await jfUserAPI.createUserByName(user, options)
|
||||
console.log(JSON.stringify(a, null, 2))
|
||||
u = (await jfUserAPI.getUsers(undefined, undefined, options)).body
|
||||
console.log(JSON.stringify(u.map(x => x.name), null, 2))
|
||||
}
|
19
server/commands/echo.ts
Normal file
19
server/commands/echo.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { ApplicationCommandOptionType } from 'discord.js'
|
||||
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: ApplicationCommandOptionType.String,
|
||||
required: true
|
||||
}
|
||||
],
|
||||
run: async (interaction: RunOptions) => {
|
||||
console.log('echo called')
|
||||
interaction.interaction.reply(interaction.toString())
|
||||
}
|
||||
})
|
9
server/commands/pong.ts
Normal file
9
server/commands/pong.ts
Normal 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')
|
||||
}
|
||||
})
|
30
server/configuration.ts
Normal file
30
server/configuration.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import dotenv from "dotenv"
|
||||
dotenv.config()
|
||||
|
||||
export const config = {
|
||||
server: {
|
||||
bodyParser: {
|
||||
urlEncodedOptions: {
|
||||
inflate: true,
|
||||
limit: '5mb',
|
||||
type: 'application/x-www-form-urlencoded',
|
||||
extended: true,
|
||||
parameterLimit: 1000
|
||||
},
|
||||
jsonOptions: {
|
||||
inflate: true,
|
||||
limit: '5mb',
|
||||
type: 'application/json',
|
||||
strict: true
|
||||
}
|
||||
}
|
||||
},
|
||||
debug: true,
|
||||
port: 1234,
|
||||
silent: false,
|
||||
token: process.env.BOT_TOKEN ?? "",
|
||||
guild_id: process.env.GUILD_ID ?? "",
|
||||
client_id: process.env.CLIENT_ID ?? "",
|
||||
jellfin_token:process.env.JELLYFIN_TOKEN ?? "",
|
||||
jellyfin_url:process.env.JELLYFIN_URL ?? ""
|
||||
}
|
52
server/constants.ts
Normal file
52
server/constants.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import { localized_string } from "./interfaces"
|
||||
|
||||
export const reactions = {
|
||||
troll_grin: "U+1F92A",
|
||||
angry_face: "U+1F621",
|
||||
ok: "U+1F44C"
|
||||
}
|
||||
export const commands = {
|
||||
LIST_COMMAND: "list",
|
||||
REGISTER_COMMAND: "register",
|
||||
REMOVE_COMMAND: "remove",
|
||||
SHOW_FOR_STEAM_COMMAND: "forsteam",
|
||||
SHOW_COMMAND: "show"
|
||||
}
|
||||
export const msg_strings: localized_string = {
|
||||
greeting: {
|
||||
german: "Ich wurde neugestartet. Bitte registriert euch erneut, falls ihr automatisch gemutet werden wollt :)",
|
||||
english: "I have been restarted. Please register again if you want to be muted automatically :)"
|
||||
},
|
||||
fmt_registered_user: {
|
||||
german: "Habe den Steamname {steam_name} mit dem Discordnamen {discord_name} verknüpft.",
|
||||
english: "Registered the steam name {steam_name} for the discord name {discord_name}."
|
||||
},
|
||||
fmt_registered_for_steam: {
|
||||
german: "Aktuell registriert für User {steam_name}: {discord_name}",
|
||||
english: "Currently registered for user {steam_name}: {discord_name}"
|
||||
},
|
||||
fmt_registered_for_discord: {
|
||||
german: "Aktuell registriert für User {discord_name}: {steam_name}",
|
||||
english: "Currently registered for user {discord_name}: {steam_name}"
|
||||
},
|
||||
user_was_not_registered: {
|
||||
german: "Du warst gar nicht registriert.",
|
||||
english: "You weren't even registered.",
|
||||
},
|
||||
user_has_been_removed: {
|
||||
german: "Du wurdest aus der Liste entfernt.",
|
||||
english: "You were removed from the list.",
|
||||
},
|
||||
currently_registered: {
|
||||
german: "Aktuell registriert: \n {playerlist}",
|
||||
english: "Currently registered: \n {playerlist}"
|
||||
},
|
||||
troll_rejection: {
|
||||
german: "Nöööö, du nicht...",
|
||||
english: "Naaaah, not you..."
|
||||
},
|
||||
troll_rejection_second_part: {
|
||||
german: "Spaß, hab dich registriert: :P",
|
||||
english: "Just kidding, you are registered: :P"
|
||||
}
|
||||
}
|
33
server/eventHandler.ts
Normal file
33
server/eventHandler.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { Collection, Guild, GuildScheduledEvent, Snowflake } from "discord.js";
|
||||
|
||||
export default class eventHandler {
|
||||
|
||||
public constructor() {
|
||||
console.log('constructed')
|
||||
}
|
||||
public getNextEvent(guild: Guild): string {
|
||||
const eventManager = guild.scheduledEvents
|
||||
const events = eventManager.cache
|
||||
const sortedEvents = events.sort(function(a, b) { return Number(a.scheduledStartAt) - Number(b.scheduledStartAt) })
|
||||
|
||||
console.log(JSON.stringify(events))
|
||||
console.log(JSON.stringify(sortedEvents))
|
||||
|
||||
return sortedEvents.first()?.toString() ?? ""
|
||||
}
|
||||
public listAllEvents(guild: Guild): string {
|
||||
|
||||
const eventManager = guild.scheduledEvents
|
||||
const events: Collection<Snowflake, GuildScheduledEvent> = eventManager.cache
|
||||
const entries = events.values()
|
||||
let output = ""
|
||||
|
||||
for (const e of entries) {
|
||||
console.log(e)
|
||||
output += "\n"
|
||||
output += e.toString()
|
||||
}
|
||||
console.log(output)
|
||||
return output
|
||||
}
|
||||
}
|
12
server/events/guildMemberUpdate.ts
Normal file
12
server/events/guildMemberUpdate.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { GuildMember } from "discord.js"
|
||||
|
||||
export const name = 'guildMemberUpdate'
|
||||
export async function execute(oldMember: GuildMember, newMember: GuildMember) {
|
||||
try {
|
||||
console.log(`User ${oldMember.displayName} has been updated`)
|
||||
console.log(`OldRoles: ${JSON.stringify(oldMember.roles.cache.map(x => x.name))}`)
|
||||
console.log(`NewRoles: ${JSON.stringify(newMember.roles.cache.map(x => x.name))}`)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
6
server/events/messageCreate.ts
Normal file
6
server/events/messageCreate.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { Message } from "discord.js"
|
||||
|
||||
export const name = 'messageCreate'
|
||||
export function execute(message: Message) {
|
||||
console.log(`${JSON.stringify(message)} has been created`)
|
||||
}
|
4
server/events/ready.ts
Normal file
4
server/events/ready.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export const name = 'ready'
|
||||
export function execute(client: any) {
|
||||
//console.log(`Processing ready: ${JSON.stringify(client)} has been created.`)
|
||||
}
|
13
server/handler/addUserAccount.ts
Normal file
13
server/handler/addUserAccount.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { User as DiscordUser } from "discord.js";
|
||||
import { AuthenticateUserByNameRequest, JellyfinAPI } from "../../jellyfin/api";
|
||||
import { UserApi } from "../../jellyfin/api/userApi";
|
||||
import { config } from "../configuration";
|
||||
|
||||
export async function add(discordUser: DiscordUser): Promise<any> {
|
||||
const jfUserAPI: UserApi = new UserApi(config.jellyfin_url)
|
||||
|
||||
|
||||
|
||||
return
|
||||
|
||||
}
|
119
server/handler/repeatingEvents/helper.ts
Normal file
119
server/handler/repeatingEvents/helper.ts
Normal 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<GuildScheduledEvent> {
|
||||
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)
|
||||
}
|
||||
}
|
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)
|
||||
}
|
29
server/interfaces.ts
Normal file
29
server/interfaces.ts
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
export type Maybe<T> = T | undefined
|
||||
export interface Player {
|
||||
name: string
|
||||
}
|
||||
export type supported_languages = "german" | "english"
|
||||
export interface localized_string {
|
||||
[k: 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",
|
||||
|
||||
}
|
27
server/logger.ts
Normal file
27
server/logger.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { createLogger, format, transports } from "winston"
|
||||
import { config } from "./configuration"
|
||||
|
||||
|
||||
const printFn = format.printf(({ guildId, level, message, errorCode, requestId, timestamp: logTimestamp }: { [k: string]: string }) => {
|
||||
return `[${guildId}][${level}][${logTimestamp}][${errorCode
|
||||
? `[${errorCode}]` : `[]`}][${requestId
|
||||
? `[${requestId}]` : `[]`}]:${message}`
|
||||
})
|
||||
|
||||
const logFormat = format.combine(
|
||||
format.timestamp(),
|
||||
printFn
|
||||
)
|
||||
|
||||
const consoleTransports = [
|
||||
new transports.Console({
|
||||
format: logFormat
|
||||
})
|
||||
]
|
||||
const logger = createLogger({
|
||||
level: config.debug ? 'debug' : 'info',
|
||||
format: logFormat,
|
||||
silent: config.silent,
|
||||
transports: consoleTransports
|
||||
})
|
||||
logger.info("text", { requestId: "2", guildId: "asd" })
|
95
server/structures/client.ts
Normal file
95
server/structures/client.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import { ApplicationCommandDataResolvable, Client, ClientOptions, Collection, GatewayIntentBits, Guild, IntentsBitField, 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() {
|
||||
const intents: IntentsBitField = new IntentsBitField()
|
||||
intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages)
|
||||
const options: ClientOptions = { intents }
|
||||
super(options)
|
||||
}
|
||||
public start() {
|
||||
if (process.env.NODE_ENV === 'test') return
|
||||
const promises: Promise<any>[] = Array()
|
||||
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.name}|${guild.id}`)
|
||||
})
|
||||
} else {
|
||||
this.application?.commands.set(cmds)
|
||||
console.log(`Registering global commands`)
|
||||
}
|
||||
return
|
||||
}
|
||||
public async registerSlashCommands(): Promise<void> {
|
||||
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)
|
||||
this.cacheUsers(guilds)
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(`Error refreshing slash commands: ${error}`)
|
||||
}
|
||||
}
|
||||
public async cacheUsers(guilds: Collection<Snowflake, Guild>) {
|
||||
guilds.forEach((guild:Guild, id:Snowflake) => {
|
||||
console.log(`Fetching members for ${guild.name}|${id}`)
|
||||
guild.members.fetch()
|
||||
console.log(`Fetched: ${guild.memberCount} members`)
|
||||
})
|
||||
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
7
server/structures/command.ts
Normal file
7
server/structures/command.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { CommandType } from "../types/commandTypes";
|
||||
|
||||
export class Command {
|
||||
constructor(commandOptions: CommandType) {
|
||||
Object.assign(this, commandOptions)
|
||||
}
|
||||
}
|
8
server/structures/event.ts
Normal file
8
server/structures/event.ts
Normal 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]) => unknown
|
||||
) { }
|
||||
}
|
6
server/types/clients.ts
Normal file
6
server/types/clients.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { ApplicationCommandDataResolvable } from "discord.js"
|
||||
|
||||
export interface RegisterCommandOptions {
|
||||
guildId?: string
|
||||
commands: ApplicationCommandDataResolvable[]
|
||||
}
|
17
server/types/commandTypes.ts
Normal file
17
server/types/commandTypes.ts
Normal 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) => unknown
|
||||
export type CommandType = {
|
||||
userPermissions?: PermissionResolvable[]
|
||||
run: RunFunction
|
||||
} & ChatInputApplicationCommandData
|
101
server/types/scheduledEventTypes.ts
Normal file
101
server/types/scheduledEventTypes.ts
Normal file
@ -0,0 +1,101 @@
|
||||
import { add } from "date-fns"
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user