restructure and expressjs endpoints

This commit is contained in:
Fran Jurmanović
2023-10-02 21:33:22 +02:00
parent 9a9a4f2ced
commit c07e743480
26 changed files with 232 additions and 181 deletions

48
src/common/chat.ts Normal file
View File

@@ -0,0 +1,48 @@
import { CommandFunction, ICommand } from "@models";
import type { Client, Message } from "discord.js";
export default class Chat {
private prefix: string = "!";
constructor(private client: Client, private commands: ICommand[] = []) {}
public registerPrefix = (prefix: string): void => {
this.prefix = prefix;
};
public register = (token: string): void => {
if (!this.commands) return;
this.client.on("message", (message: Message): void => {
this.commands.forEach((command) => {
if (message?.content === `${this.prefix}${command?.name}`) {
command?.callback?.(message);
} else if (
message?.content?.split?.(/\s/g)?.[0] == `${this.prefix}${command?.name}`
) {
const args = message?.content
?.replace?.(`${this.prefix}${command?.name}`, "")
.trim?.()
?.split?.(/\s(?=(?:[^'"`]*(['"`])[^'"`]*\1)*[^'"`]*$)/g)
.map((d) => {
if (d?.[0] == '"' && d?.[d?.length - 1] == '"') {
return d?.substr?.(1)?.slice?.(0, -1);
}
return d;
})
.filter((d) => d);
command?.callback?.(message, args);
}
});
});
this.client.login(token);
};
public command = (name: string, callback: CommandFunction): void => {
this.commands = [
...this.commands,
{
name,
callback,
},
];
};
}

1
src/common/index.ts Normal file
View File

@@ -0,0 +1 @@
export { default as Chat } from "./chat";