diff --git a/src/index.ts b/src/index.ts index b516e8b..eefdd3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,46 +1,4 @@ -import { ExtendedClient as Client } from "./utils/Client" -import { GatewayIntentBits, Partials } from "discord.js" -import config from "./utils/Config" -import { redis } from "./utils/Redis" -import { connect } from "mongoose" -import { loadAllEvents } from "./utils/Events" -import { autoDeployCommands } from "./utils/Autodeploy" +import Illegitimate from "./utils/Illegitimate" +const illegitimate = new Illegitimate() -const client = new Client({ - intents: [ - GatewayIntentBits.Guilds, - GatewayIntentBits.GuildMessages, - GatewayIntentBits.GuildMembers, - GatewayIntentBits.MessageContent, - GatewayIntentBits.DirectMessages, - GatewayIntentBits.GuildVoiceStates, - ], - partials: [ - Partials.GuildMember, - Partials.User, - Partials.Message, - Partials.Channel, - ], -}) - -loadAllEvents(client) - -let token: string -if (process.env.NODE_ENV === "dev") { - console.log("Running in development mode.") - token = config.dev.devtoken - autoDeployCommands() -} else { - console.log("Running in production mode.") - token = config.prod.token -} - -client.login(token) - -redis.on("ready", () => { - console.log("Connected to Redis") -}) - -connect(config.prod.mongoURI, {}).then(() => { - console.log("Connected to MongoDB") -}) +illegitimate.start() diff --git a/src/utils/Client.ts b/src/utils/Client.ts index 9785a3e..1b6463d 100644 --- a/src/utils/Client.ts +++ b/src/utils/Client.ts @@ -1,9 +1,12 @@ -import { Client, Collection } from "discord.js" +import { Client, Collection, GatewayIntentBits, Partials } from "discord.js" import { Command } from "../interfaces" import { ContextMenu } from "../interfaces" import { Button } from "../interfaces" import { Modal } from "../interfaces" import { Autocomplete } from "../interfaces" +import config from "./Config" +import { autoDeployCommands } from "./Autodeploy" +import { loadAllEvents } from "./Events" export class ExtendedClient extends Client { commands: Collection = new Collection() @@ -11,4 +14,39 @@ export class ExtendedClient extends Client { buttons: Collection = new Collection() modals: Collection = new Collection() autocomplete: Collection = new Collection() + + constructor() { + super({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.GuildMembers, + GatewayIntentBits.MessageContent, + GatewayIntentBits.DirectMessages, + GatewayIntentBits.GuildVoiceStates, + ], + partials: [ + Partials.GuildMember, + Partials.User, + Partials.Message, + Partials.Channel, + ], + }) + } + + async start() { + loadAllEvents(this) + + let token: string + if (process.env.NODE_ENV === "dev") { + console.log("Running in development mode.") + token = config.dev.devtoken + autoDeployCommands() + } else { + console.log("Running in production mode.") + token = config.prod.token + } + + this.login(token) + } } diff --git a/src/utils/Illegitimate.ts b/src/utils/Illegitimate.ts new file mode 100644 index 0000000..d93c019 --- /dev/null +++ b/src/utils/Illegitimate.ts @@ -0,0 +1,24 @@ +import { ExtendedClient as Client } from "./Client" +import config from "./Config" +import { redis } from "./Redis" +import { connect } from "mongoose" +import init from "./Init" +const client = new Client() + +export default class Illegitimate { + constructor() {} + + async start() { + init() + + client.start() + + redis.on("ready", () => { + console.log("Connected to Redis") + }) + + connect(config.prod.mongoURI, {}).then(() => { + console.log("Connected to MongoDB") + }) + } +} diff --git a/src/utils/Init.ts b/src/utils/Init.ts new file mode 100644 index 0000000..24f54e8 --- /dev/null +++ b/src/utils/Init.ts @@ -0,0 +1,26 @@ +import config from "./Config" + +const prodValues = config.prod +const devValues = config.dev + +export default function init() { + if (process.env.NODE_ENV === "dev") { + Object.keys(devValues).forEach(key => { + if (!process.env[key]) { + throw new Error(`[DEV] Missing environment variable: ${key}`) + } + }) + + Object.keys(prodValues).forEach(key => { + if (!process.env[key]) { + throw new Error(`[PROD] Missing environment variable: ${key}`) + } + }) + } else { + Object.keys(prodValues).forEach(key => { + if (!process.env[key]) { + throw new Error(`[PROD] Missing environment variable: ${key}`) + } + }) + } +}