mirror of
https://github.com/System-End/slack-end.git
synced 2026-04-19 22:05:10 +00:00
feat!: commands folder
This commit is contained in:
parent
39910789bb
commit
aff6b12561
4 changed files with 93 additions and 7 deletions
20
src/commands/ping.ts
Normal file
20
src/commands/ping.ts
Normal 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 => {
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
18
src/index.ts
18
src/index.ts
|
|
@ -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 {
|
||||
|
|
|
|||
12
src/modules/BaseCommand.ts
Normal file
12
src/modules/BaseCommand.ts
Normal 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
|
||||
}
|
||||
}
|
||||
50
src/modules/CommandLoader.ts
Normal file
50
src/modules/CommandLoader.ts
Normal 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}`)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue