2023-03-14 21:16:54 +01:00
|
|
|
import DiscordApi from "discord.js";
|
2023-09-27 17:14:17 +02:00
|
|
|
import OpenAIApi from "openai";
|
2023-05-02 17:55:48 +02:00
|
|
|
import { PrismaClient } from "@prisma/client";
|
2023-09-18 11:06:15 +02:00
|
|
|
import Typescript from "typescript";
|
|
|
|
import fs from "node:fs";
|
2023-03-14 21:16:54 +01:00
|
|
|
|
2023-05-08 01:30:32 +02:00
|
|
|
import { queueRequest } from "./execution";
|
2023-05-08 08:53:06 +02:00
|
|
|
import InteractionManager from "./interactionManager";
|
2023-09-18 11:06:15 +02:00
|
|
|
import newConfig, { IConfig, IConfigRequired } from "./configDefault";
|
2023-03-14 21:16:54 +01:00
|
|
|
const discord = new DiscordApi.Client({
|
|
|
|
intents: [
|
|
|
|
DiscordApi.GatewayIntentBits.Guilds,
|
|
|
|
DiscordApi.GatewayIntentBits.GuildMessages,
|
|
|
|
DiscordApi.GatewayIntentBits.MessageContent,
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2023-09-18 11:06:15 +02:00
|
|
|
function getConfig() {
|
|
|
|
let fileConfig: IConfig | undefined = undefined;
|
|
|
|
try {
|
|
|
|
fs.statSync("./config.ts");
|
|
|
|
const program = Typescript.createProgram(
|
|
|
|
["./config.ts"],
|
|
|
|
{outDir: "./dist"}
|
|
|
|
);
|
2023-09-21 20:43:43 +02:00
|
|
|
|
|
|
|
program.getSourceFiles()
|
|
|
|
.filter(e => {
|
|
|
|
if (!e.fileName.match(`^${process.cwd()}`)) return true;
|
|
|
|
if (e.fileName.match(`${process.cwd()}/node_modules`)) return false;
|
|
|
|
if (e.fileName.match(`${process.cwd()}/src/`)) return false;
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.forEach(e => program.emit(e));
|
2023-09-18 11:06:15 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
|
2023-09-21 20:43:43 +02:00
|
|
|
fileConfig = require("../config").default as IConfig;
|
2023-09-18 11:06:15 +02:00
|
|
|
} catch (e) {
|
|
|
|
//FIXME: make errors more descriptive to the enduser
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
return fileConfig !== undefined ? newConfig(fileConfig) : newConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
export const config: IConfigRequired = getConfig();
|
|
|
|
|
2023-09-27 17:14:17 +02:00
|
|
|
export const openai = new OpenAIApi({
|
2023-03-14 21:16:54 +01:00
|
|
|
apiKey: config.tokens.OpenAI
|
2023-09-27 17:14:17 +02:00
|
|
|
});
|
2023-03-14 21:16:54 +01:00
|
|
|
|
2023-05-02 17:55:48 +02:00
|
|
|
export const database = new PrismaClient();
|
|
|
|
|
2023-05-08 08:53:06 +02:00
|
|
|
const interactionManager = new InteractionManager();
|
|
|
|
interactionManager.bindClient(discord);
|
|
|
|
|
2023-07-30 22:28:13 +02:00
|
|
|
discord.on("ready", event => {
|
2023-03-14 21:16:54 +01:00
|
|
|
console.log(`Connected to Discord as ${event.user.tag} (${event.user.id})`);
|
|
|
|
});
|
|
|
|
|
2023-07-30 22:28:13 +02:00
|
|
|
discord.on("messageCreate", message => {
|
2023-03-14 21:16:54 +01:00
|
|
|
if (message.author.bot) return;
|
|
|
|
if (!message.mentions.has(message.client.user)) return;
|
|
|
|
|
2023-07-31 12:17:14 +02:00
|
|
|
return queueRequest(message);
|
2023-03-25 11:24:43 +01:00
|
|
|
});
|
|
|
|
|
2023-07-31 12:17:14 +02:00
|
|
|
if (require.main === module) void discord.login(config.tokens.Discord);
|