Skip to content

Commit

Permalink
updated bot.ts & replaced some setIntervals by cron jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
GhomKrosmonaute committed Nov 5, 2024
1 parent 7404359 commit 9c187c2
Show file tree
Hide file tree
Showing 52 changed files with 536 additions and 342 deletions.
38 changes: 34 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@
"tims": "^2.1.0",
"types-package-json": "^2.0.39",
"yargs-parser": "^21.0.1",
"zod": "^3.23.8"
"zod": "^3.23.8",
"node-cron": "^3.0.3"
},
"devDependencies": {
"@esbuild/linux-x64": "^0.24.0",
"@eslint/compat": "^1.2.0",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.12.0",
"@ghom/bot.ts-cli": "^8.2.0",
"@ghom/bot.ts-cli": "^8.3.1",
"@types/boxen": "^3.0.1",
"@types/cron": "^1.7.3",
"@types/dotenv": "^8.2.0",
Expand Down Expand Up @@ -103,7 +104,8 @@
"make-bot.ts": "^6.0.5",
"nodemon": "^2.0.19",
"typescript": "^5.4.0-beta",
"vinyl-paths": "^4.0.0"
"vinyl-paths": "^4.0.0",
"@types/node-cron": "^3.0.11"
},
"engines": {
"node": ">=22.x.x",
Expand All @@ -126,4 +128,4 @@
"@types/pg": "^8.11.10",
"through2": "^4.0.2"
}
}
}
2 changes: 2 additions & 0 deletions src/app.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./app/config.ts"
export * from "./app/logger.ts"
export * from "./app/slash.ts"
export * from "./app/util.ts"
export * from "./app/cron.ts"
export * from "./config.ts"
export * from "./types.ts"

Expand All @@ -18,6 +19,7 @@ export * as command from "./app/command.ts"
export * as button from "./app/button.ts"
export * as slash from "./app/slash.ts"
export * as util from "./app/util.ts"
export * as cron from "./app/cron.ts"

export { default as env } from "#env"
export { default as client } from "#client"
Expand Down
21 changes: 11 additions & 10 deletions src/app/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const buttonHandler = new handler.Handler<IButton>(
pattern: /\.js$/,
loader: async (filepath) => {
const file = await import(url.pathToFileURL(filepath).href)
return file.default as IButton
if (file.default instanceof Button) return file.default
throw new Error(`${filepath}: default export must be a Button instance`)
},
onLoad: async (filepath, button) => {
button.native = filepath.endsWith(".native.js")
Expand All @@ -31,22 +32,22 @@ export const buttons = new (class ButtonCollection extends discord.Collection<
> {
add(button: IButton): this {
this.validate(button)
return super.set(button.options.key, button)
return this.set(button.options.name, button)
}

validate(button: IButton): void | never {
if (this.has(button.options.key)) {
throw new Error(`Button key "${button.options.key}" is not unique.`)
if (this.has(button.options.name)) {
throw new Error(`Button key "${button.options.name}" is not unique.`)
}

util.validateCooldown(
button.options.cooldown,
button.options.run,
button.options.key,
button.options.name,
)

logger.log(
`loaded button ${util.styleText("blueBright", button.options.key)}${
`loaded button ${util.styleText("blueBright", button.options.name)}${
button.native ? ` ${util.styleText("green", "native")}` : ""
} ${util.styleText("grey", button.options.description)}`,
)
Expand All @@ -55,7 +56,7 @@ export const buttons = new (class ButtonCollection extends discord.Collection<

export interface IButton {
options: {
key: string
name: string
description: string
guildOnly?: boolean
adminOnly?: boolean
Expand All @@ -79,7 +80,7 @@ export interface IButton {
export type ButtonParams = Record<string, string | boolean | number> | null

export interface ButtonOptions<Params extends ButtonParams> {
key: string
name: string
description: string
guildOnly?: boolean
adminOnly?: boolean
Expand Down Expand Up @@ -133,7 +134,7 @@ export function createButton<Params extends ButtonParams>(
params: Params,
): discord.ButtonBuilder {
const button = new discord.ButtonBuilder()
.setCustomId(encodeButtonCustomId(handler.options.key, params))
.setCustomId(encodeButtonCustomId(handler.options.name, params))
.setStyle(discord.ButtonStyle.Primary)

handler.options.builder?.(button, params)
Expand All @@ -147,7 +148,7 @@ export async function prepareButton(
): Promise<util.SystemMessage | void> {
const error = await util.checkCooldown(
button.options.cooldown,
`${button.options.key} button`,
`${button.options.name} button`,
{
authorId: interaction.user.id,
channelId: interaction.channelId,
Expand Down
5 changes: 3 additions & 2 deletions src/app/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import config from "#config"
import { filename } from "dirname-filename-esm"
const __filename = filename(import.meta)

export const commandHandler = new handler.Handler(
export const commandHandler = new handler.Handler<ICommand>(
path.join(process.cwd(), "dist", "commands"),
{
pattern: /\.js$/,
loader: async (filepath) => {
const file = await import(url.pathToFileURL(filepath).href)
return file.default as ICommand
if (file.default instanceof Command) return file.default
throw new Error(`${filepath}: default export must be a Command instance`)
},
onLoad: async (filepath, command) => {
command.native = filepath.endsWith(".native.js")
Expand Down
Loading

0 comments on commit 9c187c2

Please sign in to comment.