feat!: commands folder

This commit is contained in:
Saahil 2024-09-08 20:46:54 -04:00
parent 39910789bb
commit aff6b12561
No known key found for this signature in database
GPG key ID: 8A8B64515254CFC6
4 changed files with 93 additions and 7 deletions

20
src/commands/ping.ts Normal file
View file

@ -0,0 +1,20 @@
import { App } from "@slack/bolt";
import { Command } from "../modules/BaseCommand";
export default class Ping implements Command {
name: string;
description: string;
constructor() {
this.name = `/ping`
this.description = `Pings zeon`
}
run(app: App) {
// app.command()
app.command('/ping',async ({ command, ack, respond }) => {
const stamp = Date.now()
await ack()
respond(`Pong took: \`${Date.now() - stamp}ms\``).then(d => {
})
})
}
}

View file

@ -3,17 +3,21 @@ import "./modules/watch-git"
// import "./modules/smee"
import app from './modules/slackapp'
import { View } from "@slack/bolt"
import Loader from "./modules/CommandLoader"
import path from "path"
app.start(process.env.PORT || 3000).then((d) => {
console.log(`App is UP (please help)`)
})
const cmdLoader = new Loader(app, path.join(__dirname, 'commands'))
// this is temp i swear
app.command('/ping',async ({ command, ack, respond }) => {
const stamp = Date.now()
await ack()
respond(`Pong took: \`${Date.now() - stamp}ms\``).then(d => {
})
})
cmdLoader.runQuery()
// app.command('/ping',async ({ command, ack, respond }) => {
// const stamp = Date.now()
// await ack()
// respond(`Pong took: \`${Date.now() - stamp}ms\``).then(d => {
// })
// })
// Listen for users opening your App Home
app.event('app_home_opened', async ({ event, client, logger }) => {
try {

View file

@ -0,0 +1,12 @@
import { App } from "@slack/bolt";
export interface Command {
run(app: App): void
onload?: () => void
name: string;
description: string
usage?: string
custom_properties?: {
[k: string]: any
}
}

View file

@ -0,0 +1,50 @@
import { App } from "@slack/bolt";
import { Command } from "./BaseCommand";
import { readdirSync } from "fs";
import path from "path";
export default class CommandLoader {
private _app: App
private _commands: Map<string, Command>;
public readonly dir: string;
public constructor(app: App, dir: string) {
this._app = app;
this._commands = new Map()
this.dir = dir
}
private commandsInArray(): Command[] {
return Array.from(this._commands.values())
}
public getSlackCommandData():{
name: string;
description: string
usage?: string
}[] {
return this.commandsInArray().map(e => {
return {
name: e.name,
description: e.description,
usage: e.usage
}
})
}
private getFiles(): string[] {
return readdirSync(this.dir)
}
public async runQuery() {
const files = this.getFiles()
for (const file of files) {
try {
const commandClass = await import(path.join(this.dir, file))
console.log(commandClass)
const cmd = new commandClass.default()
cmd.run(this._app)
} catch (e) {
console.error(e)
console.error(`Failed to load ${file}`)
}
}
}
}