comcord/src/index.js

155 lines
4.2 KiB
JavaScript
Raw Normal View History

2022-08-27 23:27:05 +00:00
const Eris = require("eris");
const chalk = require("chalk");
2022-08-27 23:27:05 +00:00
const token = process.argv[2];
2022-08-28 23:59:33 +00:00
process.title = "comcord";
global.comcord = {
state: {
currentGuild: null,
currentChannel: null,
nameLength: 2,
inPrompt: false,
messageQueue: [],
lastChannel: new Map(),
},
commands: {},
2022-08-28 00:49:02 +00:00
};
2022-08-27 23:27:05 +00:00
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");
2022-08-27 23:27:05 +00:00
client.once("ready", function () {
console.log(
"Logged in as: " +
chalk.yellow(
`${client.user.username}#${client.user.discriminator} (${client.user.id})`
)
2022-08-27 23:27:05 +00:00
);
comcord.state.nameLength = client.user.username.length + 2;
2022-08-28 01:53:55 +00:00
listGuilds();
2022-08-27 23:27:05 +00:00
});
client.on("messageCreate", function (msg) {
2022-08-28 02:13:25 +00:00
if (msg.author.id === client.user.id) return;
if (msg.channel.id == comcord.state.currentChannel) {
if (comcord.state.inPrompt) {
comcord.state.messageQueue.push(msg);
2022-08-27 23:27:05 +00:00
} else {
if (msg.content.indexOf("\n") > -1) {
const lines = msg.content.split("\n");
for (const index in lines) {
const line = lines[index];
processMessage({
2022-08-28 02:13:25 +00:00
name: msg.author.username,
bot: msg.author.bot,
content: line,
2022-08-28 15:12:26 +00:00
attachments: index == lines.length - 1 ? msg.attachments : [],
2022-08-28 02:57:40 +00:00
reply: index == 0 ? msg.referencedMessage : null,
});
}
} else {
processMessage({
2022-08-28 02:13:25 +00:00
name: msg.author.username,
bot: msg.author.bot,
content: msg.content,
attachments: msg.attachments,
2022-08-28 02:57:40 +00:00
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,
2022-08-28 15:12:26 +00:00
content: line + (index == lines.length - 1 ? " (edited)" : ""),
attachments: index == lines.length - 1 ? msg.attachments : [],
2022-08-28 02:57:40 +00:00
reply: index == 0 ? msg.referencedMessage : null,
});
}
} else {
processMessage({
name: msg.author.username,
bot: msg.author.bot,
content: msg.content + " (edited)",
attachments: msg.attachments,
2022-08-28 02:57:40 +00:00
reply: msg.referencedMessage,
});
}
}
}
});
process.stdin.on("data", async function (key) {
if (comcord.state.inPrompt) {
2022-08-28 03:22:06 +00:00
if (key === "\r") {
await finalizePrompt();
processQueue();
2022-08-28 03:22:06 +00:00
} else {
2022-08-28 23:20:01 +00:00
if (key === "\b" || key === "\u007f") {
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
);
2022-08-28 03:22:06 +00:00
}
} else {
2022-09-03 00:21:00 +00:00
key = key.replace("\u001b", "");
process.stdout.write(key);
comcord.state.promptInput += key;
2022-08-28 03:22:06 +00:00
}
}
} else {
if (comcord.commands[key]) {
comcord.commands[key].callback();
} else {
sendMode();
2022-08-27 23:27:05 +00:00
}
}
});
client.connect();
2022-08-28 00:49:02 +00:00
console.log("COMcord (c)left 2022");
console.log("Type 'h' for Commands");