diff --git a/.env.template b/.env.template index 175a02a..6676a03 100644 --- a/.env.template +++ b/.env.template @@ -1,3 +1,4 @@ DATABASE_URL="postgresql://user:password@localhost/cosmic" MPPNET_TOKEN="" CHATBOX_LICENSE_KEY="" +DISCORD_TOKEN="bot token" diff --git a/bun.lockb b/bun.lockb index 18433ab..b800941 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/config/ascii.txt b/config/ascii.txt new file mode 100644 index 0000000..cf7b1dd --- /dev/null +++ b/config/ascii.txt @@ -0,0 +1,10 @@ + d8, + `8P + + .d888b,?88 d8P?88,.d88b, d8888b 88bd88b d8888b d8888b .d888b, 88bd8b,d88b 88b d8888b + ?8b, d88 88 `?88' ?88d8b_,dP 88P' `d8P' `Pd8P' ?88 ?8b, 88P'`?8P'?8b 88Pd8P' `P + `?8b ?8( d88 88b d8P88b d88 88b 88b d88 `?8b d88 d88 88P d88 88b +`?888P' `?88P'?8b 888888P'`?888P'd88' `?888P'`?8888P'`?888P' d88' d88' 88bd88' `?888P' + 88P' + d88 + ?8P diff --git a/config/ascii.yml b/config/ascii.yml new file mode 100644 index 0000000..5a6b698 --- /dev/null +++ b/config/ascii.yml @@ -0,0 +1 @@ +path: config/ascii.txt diff --git a/config/services.yml b/config/services.yml index cf4b624..1163dc4 100644 --- a/config/services.yml +++ b/config/services.yml @@ -1,4 +1,4 @@ enableConsole: true -enableMPP: true -enableDiscord: false +enableMPP: false +enableDiscord: true enableSwitchChat: false diff --git a/package.json b/package.json index ee1dbc3..ee7f5cc 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "@prisma/client": "^5.5.2", "@t3-oss/env-core": "^0.7.1", + "discord.js": "^14.14.1", "dotenv": "^16.3.1", "hyperimport": "^0.1.0", "mathjs": "^11.11.2", diff --git a/src/index.ts b/src/index.ts index 9577e05..0b53c51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,9 @@ import { loadCommands } from "./commands"; import { loadRoleConfig } from "./permissions"; import { ServiceLoader } from "./services"; +import { printStartupASCII } from "./util/ascii"; +printStartupASCII(); loadRoleConfig(); loadCommands(); ServiceLoader.loadServices(); diff --git a/src/services/discord/index.ts b/src/services/discord/index.ts new file mode 100644 index 0000000..a0d57ec --- /dev/null +++ b/src/services/discord/index.ts @@ -0,0 +1,102 @@ +import Discord from "discord.js"; +import { ServiceAgent } from "../ServiceAgent"; +import { ChatMessage } from "../console/MicroHandler"; +import { CommandHandler } from "../../commands/CommandHandler"; + +export class DiscordAgent extends ServiceAgent { + constructor(private token: string) { + const cl = new Discord.Client({ + intents: ["Guilds", "GuildMessages", "MessageContent"] + }); + + super("discord", cl); + } + + private wasDestroyed = false; + + public start(): void { + this.wasDestroyed + ? (this.client = new Discord.Client({ + intents: ["Guilds", "GuildMessages", "MessageContent"] + })) + : undefined; + + this.wasDestroyed = false; + + this.client.login(this.token); + } + + public stop(): void { + this.client.destroy(); + this.wasDestroyed = true; + } + + private lastChannelId: string | undefined; + + public getLastChannelId() { + return this.lastChannelId; + } + + protected bindEventListeners(): void { + this.client.on("ready", () => {}); + + this.client.on("messageCreate", async msg => { + const _id = "DISCORD_" + msg.author.id; + + this.emit("chat", { + m: "a", + a: msg.content, + p: { + _id, + name: msg.author.username, + color: msg.author.hexAccentColor || "#000000", + platformId: msg.author.id + } + } as ChatMessage); + + let args = msg.content.split(" "); + + const str = await CommandHandler.handleCommand( + { + m: "command", + a: msg.content, + argc: args.length, + argv: args, + p: { + _id, + name: msg.author.username, + color: msg.author.hexAccentColor || "#000000", + platformId: msg.author.id + }, + originalMessage: msg + }, + this + ); + + if (str) { + if (typeof str == "string") { + const channel = await this.client.channels.fetch( + msg.channelId + ); + + if (!channel) return; + if (!channel.isTextBased()) return; + channel.send(`\u034f${str}`); + } + } + }); + + this.on("send chat", async text => { + if (!this.lastChannelId) return; + + const channel = await this.client.channels.fetch( + this.lastChannelId + ); + + if (!channel) return; + if (!channel.isTextBased()) return; + + channel.send(`\u034f${text}`); + }); + } +} diff --git a/src/services/index.ts b/src/services/index.ts index c3348c6..f2ac3cc 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -4,6 +4,7 @@ import { ServiceAgent } from "./ServiceAgent"; import { loadConfig } from "../util/config"; import { SwitchChatAgent } from "./switchchat"; import { ConsoleAgent } from "./console"; +import { DiscordAgent } from "./discord"; /** * Services are anything (any platforms or environments) that the bot will directly communicate to users with @@ -79,6 +80,13 @@ export class ServiceLoader { this.addAgent(switchChatAgent); } + if (config.enableDiscord) { + const discordAgent = new DiscordAgent(env.DISCORD_TOKEN); + + discordAgent.start(); + this.addAgent(discordAgent); + } + if (config.enableConsole) { const consoleAgent = new ConsoleAgent(); diff --git a/src/services/mpp/index.ts b/src/services/mpp/index.ts index e329c7e..6c3164c 100644 --- a/src/services/mpp/index.ts +++ b/src/services/mpp/index.ts @@ -73,7 +73,6 @@ export class MPPAgent extends ServiceAgent { ); if (str) { - this.logger.debug(str); if (typeof str == "string") { if (str.includes("\n")) { let sp = str.split("\n"); diff --git a/src/util/ascii.ts b/src/util/ascii.ts new file mode 100644 index 0000000..29f8e06 --- /dev/null +++ b/src/util/ascii.ts @@ -0,0 +1,18 @@ +import { loadConfig } from "./config"; +import { readFileSync } from "fs"; + +const config = loadConfig("config/ascii.yml", { + path: "config/ascii.txt" +}); + +export function printStartupASCII() { + const data = readFileSync(config.path); + + // To make a compromise with PM2's logs, + // we will attempt to write every line + // with a color character so the entire + // file is printed in color. + for (const line of data.toString().split("\n")) { + process.stdout.write(`\x1b[35m${line}\x1b[0m\n`); + } +} diff --git a/src/util/env.ts b/src/util/env.ts index f4be196..1de7e20 100644 --- a/src/util/env.ts +++ b/src/util/env.ts @@ -8,7 +8,8 @@ export const env = createEnv({ isServer: true, server: { MPPNET_TOKEN: z.string(), - CHATBOX_LICENSE_KEY: z.string() + CHATBOX_LICENSE_KEY: z.string(), + DISCORD_TOKEN: z.string() }, runtimeEnv: process.env });