Add Discord support

This commit is contained in:
Hri7566 2023-12-05 23:57:59 -05:00
parent af9772c702
commit 72d2316b33
12 changed files with 147 additions and 4 deletions

View File

@ -1,3 +1,4 @@
DATABASE_URL="postgresql://user:password@localhost/cosmic"
MPPNET_TOKEN=""
CHATBOX_LICENSE_KEY=""
DISCORD_TOKEN="bot token"

BIN
bun.lockb

Binary file not shown.

10
config/ascii.txt Normal file
View File

@ -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

1
config/ascii.yml Normal file
View File

@ -0,0 +1 @@
path: config/ascii.txt

View File

@ -1,4 +1,4 @@
enableConsole: true
enableMPP: true
enableDiscord: false
enableMPP: false
enableDiscord: true
enableSwitchChat: false

View File

@ -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",

View File

@ -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();

View File

@ -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<Discord.Client> {
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}`);
});
}
}

View File

@ -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();

View File

@ -73,7 +73,6 @@ export class MPPAgent extends ServiceAgent<Client> {
);
if (str) {
this.logger.debug(str);
if (typeof str == "string") {
if (str.includes("\n")) {
let sp = str.split("\n");

18
src/util/ascii.ts Normal file
View File

@ -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`);
}
}

View File

@ -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
});