This commit is contained in:
Fran Jurmanović
2022-03-10 18:53:49 +01:00
commit 6ef57924ea
22 changed files with 2084 additions and 0 deletions

45
common/chat.ts Normal file
View File

@@ -0,0 +1,45 @@
import type { Client, Message } from "discord.js";
export default class Chat {
private commands: any[] = [];
private prefix: string = "!";
constructor(private client: Client) {}
public registerPrefix = (prefix: string): void => {
this.prefix = prefix;
};
public register = (token: string): void => {
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: Function): void => {
this.commands = [
...this.commands,
{
name,
callback,
},
];
};
}

33
common/getSettings.ts Normal file
View File

@@ -0,0 +1,33 @@
import { readFileSync } from "fs";
export default function getSettings(environment: string) {
let _returnValue = null;
if (environment === "development") {
try {
_returnValue = safelyJsonParse(readFileSync("./.configs/development/config.json", "utf-8"));
} catch (err) {
_returnValue = null;
}
} else if (environment === "testing") {
try {
_returnValue = safelyJsonParse(readFileSync("./.configs/testing/config.json", "utf-8"));
} catch (err) {
_returnValue = null;
}
} else if (environment === "production") {
try {
_returnValue = safelyJsonParse(readFileSync("./.configs/production/config.json", "utf-8"));
} catch (err) {
_returnValue = null;
}
}
return _returnValue;
}
function safelyJsonParse(data: any) {
try {
return JSON.parse(data);
} catch (err) {
return "";
}
}

2
common/index.ts Normal file
View File

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