comcord/src/index.js

179 lines
4.3 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: {
2022-10-02 16:25:59 +00:00
startTime: Date.now(),
currentGuild: null,
currentChannel: null,
nameLength: 2,
inPrompt: false,
messageQueue: [],
lastChannel: new Map(),
2022-10-02 16:25:59 +00:00
afk: false,
},
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
2022-10-02 16:25:59 +00:00
require("./commands/afk");
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-10-02 16:25:59 +00:00
client.editStatus("online", [
{
application_id: "1026163285877325874",
name: "comcord",
timestamps: {
start: comcord.state.startTime,
},
},
]);
2022-08-27 23:27:05 +00:00
});
client.on("error", function () {});
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 {
processMessage(msg);
}
}
});
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 {
processMessage(msg);
}
}
});
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");
2022-09-03 00:21:25 +00:00
const dateObj = new Date();
let sentTime = false;
const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
setInterval(function () {
dateObj.setTime(Date.now());
const hour = dateObj.getUTCHours(),
minutes = dateObj.getUTCMinutes(),
seconds = dateObj.getUTCSeconds(),
day = dateObj.getUTCDate(),
month = dateObj.getUTCMonth(),
year = dateObj.getUTCFullYear(),
weekDay = dateObj.getUTCDay();
const timeString = `[${weekdays[weekDay]} ${day
.toString()
.padStart(2, "0")}-${months[month]}-${year
.toString()
.substring(2, 4)} ${hour.toString().padStart(2, "0")}:${minutes
.toString()
.padStart(2, "0")}:${seconds.toString().padStart(2, "0")}]`;
if (minutes % 15 == 0 && seconds < 2 && !sentTime) {
if (comcord.state.inPrompt == true) {
comcord.state.messageQueue.push({time: true, content: timeString});
} else {
console.log(timeString);
}
sentTime = true;
} else if (seconds > 2 && sentTime) {
sentTime = false;
}
}, 500);