comcord/src/index.js

152 lines
4.1 KiB
JavaScript

const Eris = require("eris");
const chalk = require("chalk");
const token = process.argv[2];
global.comcord = {
state: {
currentGuild: null,
currentChannel: null,
nameLength: 2,
inPrompt: false,
messageQueue: [],
lastChannel: new Map(),
},
commands: {},
};
const client = new Eris("Bot " + token, {
defaultImageFormat: "png",
defaultImageSize: 1024,
intents: Eris.Constants.Intents.all,
});
comcord.client = client;
const {finalizePrompt} = require("./lib/prompt");
const {processMessage, processQueue} = require("./lib/messages");
require("./commands/quit");
require("./commands/clear");
require("./commands/help");
const {sendMode} = require("./commands/send");
require("./commands/emote");
const {listGuilds} = require("./commands/listGuilds");
require("./commands/switchGuild"); // loads listChannels and listUsers
require("./commands/switchChannel"); //loads listUsers
require("./commands/history"); // includes extended history
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding("utf8");
client.once("ready", function () {
console.log(
"Logged in as: " +
chalk.yellow(
`${client.user.username}#${client.user.discriminator} (${client.user.id})`
)
);
comcord.state.nameLength = client.user.username.length + 2;
listGuilds();
});
client.on("messageCreate", function (msg) {
if (msg.author.id === client.user.id) return;
if (msg.channel.id == comcord.state.currentChannel) {
if (comcord.state.inPrompt) {
comcord.state.messageQueue.push(msg);
} else {
if (msg.content.indexOf("\n") > -1) {
const lines = msg.content.split("\n");
for (const index in lines) {
const line = lines[index];
processMessage({
name: msg.author.username,
bot: msg.author.bot,
content: line,
attachments: index == lines.length - 1 ? msg.attachments : [],
reply: index == 0 ? msg.referencedMessage : null,
});
}
} else {
processMessage({
name: msg.author.username,
bot: msg.author.bot,
content: msg.content,
attachments: msg.attachments,
reply: msg.referencedMessage,
});
}
}
}
});
client.on("messageUpdate", function (msg, old) {
if (msg.author.id === client.user.id) return;
if (msg.channel.id == comcord.state.currentChannel) {
if (msg.content == old.content) return;
if (comcord.state.inPrompt) {
comcord.state.messageQueue.push(msg);
} else {
if (msg.content.indexOf("\n") > -1) {
const lines = msg.content.split("\n");
for (const index in lines) {
const line = lines[index];
processMessage({
name: msg.author.username,
bot: msg.author.bot,
content: line + (index == lines.length - 1 ? " (edited)" : ""),
attachments: index == lines.length - 1 ? msg.attachments : [],
reply: index == 0 ? msg.referencedMessage : null,
});
}
} else {
processMessage({
name: msg.author.username,
bot: msg.author.bot,
content: msg.content + " (edited)",
attachments: msg.attachments,
reply: msg.referencedMessage,
});
}
}
}
});
process.stdin.on("data", async function (key) {
if (comcord.state.inPrompt) {
if (key === "\r") {
await finalizePrompt();
processQueue();
} else {
if (key === "\b") {
if (comcord.state.promptInput.length > 0) {
process.stdout.moveCursor(-1);
process.stdout.write(" ");
process.stdout.moveCursor(-1);
comcord.state.promptInput = comcord.state.promptInput.substring(
0,
comcord.state.promptInput.length - 1
);
}
} else {
process.stdout.write(key);
comcord.state.promptInput += key;
}
}
} else {
if (comcord.commands[key]) {
comcord.commands[key].callback();
} else {
sendMode();
}
}
});
client.connect();
console.log("COMcord (c)left 2022");
console.log("Type 'h' for Commands");