command system, message sending and receiving (untested)

This commit is contained in:
Cynthia Foxwell 2022-08-27 18:14:10 -06:00
parent c1fcde1b3b
commit bd8936ad13
2 changed files with 135 additions and 8 deletions

View file

@ -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. * "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 ## TODO
- [ ] Commands - [x] Commands
- [ ] Quit (q) - [x] Quit (q)
- [ ] Switch guilds (G) - [ ] Switch guilds (G)
- [ ] Switch channels (g) - [ ] Switch channels (g)
- [ ] List online users in guild (w) - [ ] 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 - [ ] Creation date, join date, ID, etc
- [ ] Room history (r) - [ ] Room history (r)
- [ ] Extended room history (R) - [ ] Extended room history (R)
- [ ] Message Receiving - [x] Message Receiving
- [ ] Markdown styling - [ ] Markdown styling
- [ ] Common markdown (bold, italic, etc) - [ ] Common markdown (bold, italic, etc)
- [ ] Figure out how spoilers would work - [ ] Figure out how spoilers would work
- [ ] Emotes????? - [ ] Emotes?????
- [ ] Timestamp parsing - [ ] Timestamp parsing
- [ ] Embeds in the style of commode's posted links - [ ] 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 - [ ] Inline DMs to replicate commode's private messages
- [ ] Replies - [ ] Replies
- [ ] Message sending - [x] Message sending
- [ ] Puts incoming messages into queue whilst in send mode - [x] Puts incoming messages into queue whilst in send mode
- [ ] Mentions - [ ] Mentions
- [ ] Replies - [ ] Replies
- [ ] Configuration - [ ] Configuration
- [ ] Default guild/channel - [ ] Default guild/channel
- [ ] Threads - [ ] Threads
- [ ] Not have the token just be in argv

View file

@ -1,9 +1,19 @@
const Eris = require("eris"); const Eris = require("eris");
const chalk = require("chalk");
const {inspect} = require("util");
const token = process.argv[2]; const token = process.argv[2];
const stdin = process.stdin;
const stdout = process.stdout;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding("utf8");
let currentGuild, let currentGuild,
currentChannel, currentChannel,
inSendMode = false; inSendMode = false,
nameLength = 2;
const messageQueue = []; const messageQueue = [];
@ -15,15 +25,131 @@ const client = new Eris("Bot " + token, {
client.once("ready", function () { client.once("ready", function () {
console.log( 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) { client.on("messageCreate", function (msg) {
if (msg.channel.id == currentChannel) { if (msg.channel.id == currentChannel) {
if (inSendMode) { if (inSendMode) {
messageQueue.push(msg); messageQueue.push(msg);
} else { } 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("<no message sent>\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;
}
} }
} }
}); });