Updated bot source

This commit is contained in:
RoseFix7 2023-09-18 11:23:10 -04:00
parent 3a6a2763f8
commit 0dd6702e33
19 changed files with 247 additions and 20 deletions

27
source/config.ts Normal file
View file

@ -0,0 +1,27 @@
import * as l_util from "./util";
export class Config<Config_Interface> {
public readonly defaulted: Config_Interface;
#real: Config_Interface;
public events = {
change: [] as unknown as (() => {})[]
};
constructor(
defaulted: Config_Interface,
real: Config_Interface
) {
this.defaulted = defaulted;
this.#real = l_util.object.merge.recursive_merge(defaulted, real);
}
public set real(new_real: Config_Interface) {
this.#real = l_util.object.merge.recursive_merge(this.defaulted, new_real);
this.events.change.forEach((event_callback: any) => event_callback());
}
public get real() {
return this.#real;
}
}

24
source/discord-bot.ts Normal file
View file

@ -0,0 +1,24 @@
import * as config from "./discord-bot/config";
import * as l_config from "./config";
import * as l_discord from "./discord";
import * as l_util from "./util";
import * as c_discord_bot from "../config/discord-bot";
export class DiscordBot extends l_discord.Bot {
public static config: l_config.Config<config.Config_Interface>;
public static discord_bot: l_discord.Bot;
static main(
p_config: config.Config_Interface
) {
this.config = new l_config.Config(
config.defaulted,
p_config
);
this.discord_bot = new l_discord.Bot(this.config.real.discord);
this.discord_bot.running = true;
}
}
DiscordBot.main(c_discord_bot.data);

View file

@ -1,10 +1,15 @@
import * as l_discord from "../discord";
export interface Config_Interface {
discord: {
api_key: string;
application_client_id: string;
guild_id: string;
};
storage: {
}
discord: l_discord.Config_Interface;
storage?: {}
}
export const defaulted: Config_Interface = {
discord: {
api_key: "",
application_client_id: "",
guild_id: ""
},
storage: {}
}

View file

@ -1,2 +0,0 @@
import * as config from "./config";
import * as discord from "./discord";

66
source/discord.ts Normal file
View file

@ -0,0 +1,66 @@
import * as discord from "discord.js";
import * as l_config from "./config";
import * as l_util from "./util";
import * as deasync from "deasync";
export interface Config_Interface {
api_key?: string;
application_client_id?: string;
guild_id?: string;
}
const defaulted_config: Config_Interface = {
api_key: "",
application_client_id: "",
guild_id: ""
}
export class Bot extends l_util.Runable {
public readonly config: l_config.Config<Config_Interface>;
public readonly client: discord.Client;
constructor(
config: Config_Interface
) {
super();
this.config = new l_config.Config(
defaulted_config,
config
);
let intents: discord.GatewayIntentBits[] = [];
Object.keys(discord.GatewayIntentBits).forEach((intent_key: any) => intents.push(discord.GatewayIntentBits[intent_key] as any));
this.client = new discord.Client({
intents
});
}
on_run(): void {
let done = false;
let error = false;
this.client.once(discord.Events.ClientReady, () => {
done = true;
});
this.client.login(this.config.real.api_key);
while (!done) { deasync.sleep(100); }
if (error) {
console.error("FAILED TO LOGIN");
} else {
console.log("ONLINE");
}
}
on_terminate(): void {
}
}
export class Command {
}

17
source/util.ts Normal file
View file

@ -0,0 +1,17 @@
export * as object from "./util/object";
export abstract class Runable {
#running = false;
abstract on_run(): void;
abstract on_terminate(): void;
public set running(value: boolean) {
if (value && !this.#running) this.on_run();
else if (!value && this.#running) this.on_terminate();
}
public get running() {
return this.#running;
}
}

1
source/util/object.ts Normal file
View file

@ -0,0 +1 @@
export * as merge from "./object/merge";

View file

@ -0,0 +1,16 @@
export function recursive_merge(
defaulted: any,
target: any
) {
let output = defaulted;
Object.keys(target).forEach((target_key) => {
if (typeof target_key != "object") {
output[target_key] = target[target_key];
} else {
output[target_key] = recursive_merge(defaulted[target_key], target[target_key]);
}
});
return output;
}