From bd8936ad132da8bc689b2b83c8d4cd5032b963f3 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sat, 27 Aug 2022 18:14:10 -0600 Subject: [PATCH] command system, message sending and receiving (untested) --- README.md | 13 +++--- src/index.js | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 122bfc6..59069c3 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ You **MUST** grant your bot all Privileged Gateway Intents. * "Jank" by design. While I don't expect anyone to actually use comcord on serial terminals or teletypes other than for meme factor, the option is still there. ## TODO -- [ ] Commands - - [ ] Quit (q) +- [x] Commands + - [x] Quit (q) - [ ] Switch guilds (G) - [ ] Switch channels (g) - [ ] List online users in guild (w) @@ -33,20 +33,21 @@ You **MUST** grant your bot all Privileged Gateway Intents. - [ ] Creation date, join date, ID, etc - [ ] Room history (r) - [ ] Extended room history (R) -- [ ] Message Receiving +- [x] Message Receiving - [ ] Markdown styling - [ ] Common markdown (bold, italic, etc) - [ ] Figure out how spoilers would work - [ ] Emotes????? - [ ] Timestamp parsing - [ ] Embeds in the style of commode's posted links - - [ ] Messages wrapped in `*`'s or `_`'s parsed as emotes + - [x] Messages wrapped in `*`'s or `_`'s parsed as emotes - [ ] Inline DMs to replicate commode's private messages - [ ] Replies -- [ ] Message sending - - [ ] Puts incoming messages into queue whilst in send mode +- [x] Message sending + - [x] Puts incoming messages into queue whilst in send mode - [ ] Mentions - [ ] Replies - [ ] Configuration - [ ] Default guild/channel - [ ] Threads +- [ ] Not have the token just be in argv diff --git a/src/index.js b/src/index.js index 1f518d9..0601c3d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,19 @@ const Eris = require("eris"); +const chalk = require("chalk"); +const {inspect} = require("util"); + const token = process.argv[2]; +const stdin = process.stdin; +const stdout = process.stdout; + +stdin.setRawMode(true); +stdin.resume(); +stdin.setEncoding("utf8"); let currentGuild, currentChannel, - inSendMode = false; + inSendMode = false, + nameLength = 2; const messageQueue = []; @@ -15,15 +25,131 @@ const client = new Eris("Bot " + token, { client.once("ready", function () { console.log( - `Logged in as: ${client.user.username}#${client.user.discriminator} (${client.user.id})` + "Logged in as: " + + chalk.yellow( + `${client.user.username}#${client.user.discriminator} (${client.user.id})` + ) ); + nameLength = client.user.username.length + 2; }); +function processMessage({name, content, bot}) { + if (name.length + 2 > nameLength) nameLength = name.length + 2; + + if ( + (content.startsWith("*") && content.endsWith("*")) || + (content.startsWith("_") && content.endsWith("_")) + ) { + console.log(chalk.bold.green(`<${name} ${content}>`)); + } else { + // TODO: markdown + console.log( + chalk.bold.cyan(`[${name}]` + " ".rep(nameLength - (name.length + 2))) + + chalk.reset(" " + content) + ); + } +} + +function processQueue() { + for (const msg of messageQueue) { + if (msg.content.indexOf("\n") > -1) { + const lines = msg.content.split("\n"); + for (const line of lines) { + processMessage({ + name: msg.author.name, + bot: msg.author.bot, + content: line, + }); + } + } else { + processMessage({ + name: msg.author.name, + bot: msg.author.bot, + content: msg.content, + }); + } + } +} + client.on("messageCreate", function (msg) { if (msg.channel.id == currentChannel) { if (inSendMode) { messageQueue.push(msg); } else { + if (msg.content.indexOf("\n") > -1) { + const lines = msg.content.split("\n"); + for (const line of lines) { + processMessage({ + name: msg.author.name, + bot: msg.author.bot, + content: line, + }); + } + } else { + processMessage({ + name: msg.author.name, + bot: msg.author.bot, + content: msg.content, + }); + } + } + } +}); + +let toSend = ""; +function setupSendMode() { + inSendMode = true; + toSend = ""; + const name = `[${client.user.username}]`; + stdout.write( + chalk.bold.cyan(name) + " ".rep(nameLength - name.length) + chalk.reset(" ") + ); +} +function sendMessage() { + toSend = toSend.trim(); + if (toSend === "") { + stdout.write("\n"); + } else { + client.createMessage(currentChannel, toSend); + } + inSendMode = false; + processQueue(); +} + +stdin.on("data", function (key) { + if (inSendMode) { + if (key === "\r") { + sendMessage(); + } else { + if (key === "\b") { + if (toSend.length > 0) { + stdout.moveCursor(-1); + stdout.write(" "); + stdout.moveCursor(-1); + toSend = toSend.substring(0, toSend.length - 1); + } + } else { + stdout.write(key); + toSend += key; + } + } + } else { + switch (key) { + case "\u0003": + case "q": + client.disconnect(false); + process.exit(0); + break; + case " ": + case "\r": + default: { + if (currentChannel == null) { + console.log("not in a channel"); + break; + } + setupSendMode(); + break; + } } } });