implement history

This commit is contained in:
Cynthia Foxwell 2022-08-27 21:22:06 -06:00
parent aeca9294af
commit daeed08a6a
2 changed files with 160 additions and 26 deletions

View File

@ -31,8 +31,8 @@ You **MUST** grant your bot all Privileged Gateway Intents.
- [ ] Finger (f)
- [ ] Shows presence data if available
- [ ] Creation date, join date, ID, etc
- [ ] Room history (r)
- [ ] Extended room history (R)
- [x] Room history (r)
- [x] Extended room history (R)
- [x] Message Receiving
- [ ] Markdown styling
- [ ] Common markdown (bold, italic, etc)
@ -42,7 +42,7 @@ You **MUST** grant your bot all Privileged Gateway Intents.
- [ ] Embeds in the style of commode's posted links
- [x] Messages wrapped in `*`'s or `_`'s parsed as emotes
- [ ] Inline DMs to replicate commode's private messages
- [ ] Replies
- [x] Replies
- [x] Message sending
- [x] Puts incoming messages into queue whilst in send mode
- [ ] Mentions

View File

@ -15,6 +15,7 @@ let currentGuild,
inEmoteMode = false,
guildSwitch = false,
channelSwitch = false,
extendedHistory = false,
nameLength = 2;
const messageQueue = [];
@ -52,7 +53,14 @@ client.once("ready", function () {
listGuilds();
});
function processMessage({name, content, bot, attachments, reply}) {
function processMessage({
name,
content,
bot,
attachments,
reply,
isHistory = false,
}) {
if (name.length + 2 > nameLength) nameLength = name.length + 2;
if (reply) {
@ -61,39 +69,65 @@ function processMessage({name, content, bot, attachments, reply}) {
const headerLength = 5 + reply.author.username.length;
const length = headerLength + reply.content.length;
console.log(
chalk.bold.white(" \u250d ") +
nameColor(`[${reply.author.username}] `) +
chalk.reset(
`${
length > 79
? reply.content.substring(0, length - headerLength) + "\u2026"
: reply.content
}`
)
);
if (isHistory) {
console.log(
` \u250d [${reply.author.username}] ${
length > 79
? reply.content.substring(0, length - headerLength) + "\u2026"
: reply.content
}`
);
} else {
console.log(
chalk.bold.white(" \u250d ") +
nameColor(`[${reply.author.username}] `) +
chalk.reset(
`${
length > 79
? reply.content.substring(0, length - headerLength) + "\u2026"
: reply.content
}`
)
);
}
}
if (
(content.startsWith("*") && content.endsWith("*")) ||
(content.startsWith("_") && content.endsWith("_"))
) {
console.log(
chalk.bold.green(`<${name} ${content.substring(1, content.length - 1)}>`)
);
if (isHistory) {
console.log(`<${name} ${content.subString(1, content.length - 1)}>`);
} else {
console.log(
chalk.bold.green(
`<${name} ${content.substring(1, content.length - 1)}>`
)
);
}
} else {
const nameColor = bot ? chalk.bold.yellow : chalk.bold.cyan;
if (isHistory) {
console.log(
`[${name}]${" ".repeat(nameLength - (name.length + 2))} ${content}`
);
} else {
const nameColor = bot ? chalk.bold.yellow : chalk.bold.cyan;
// TODO: markdown
console.log(
nameColor(`[${name}]`) +
" ".repeat(nameLength - (name.length + 2)) +
chalk.reset(" " + content)
);
// TODO: markdown
console.log(
nameColor(`[${name}]`) +
" ".repeat(nameLength - (name.length + 2)) +
chalk.reset(" " + content)
);
}
}
for (const attachment of attachments) {
console.log(chalk.bold.yellow(`<attachment: ${attachment.url} >`));
if (isHistory) {
console.log(`<attachment: ${attachment.url} >`);
} else {
console.log(chalk.bold.yellow(`<attachment: ${attachment.url} >`));
}
}
}
@ -359,6 +393,7 @@ function listUsers() {
}
function switchGuild() {
targetGuild = targetGuild.trim();
if (targetGuild == "") {
listUsers();
guildSwitch = false;
@ -397,6 +432,7 @@ function gotoChannel() {
}
function switchChannel() {
targetChannel = targetChannel.trim();
if (targetChannel == "") {
listUsers();
channelSwitch = false;
@ -449,6 +485,71 @@ async function sendEmote() {
processQueue();
}
async function getHistory(limit = 20) {
const messages = await client.getMessages(currentChannel, {limit});
messages.reverse();
console.log("--Beginning-Review".padEnd(72, "-"));
for (const msg of messages) {
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 +
(msg.editedTimestamp != null && index == lines.length - 1
? " (edited)"
: ""),
attachments: index == lines.length - 1 ? msg.attachments : null,
reply: index == 0 ? msg.referencedMessage : null,
isHistory: true,
});
}
} else {
processMessage({
name: msg.author.username,
bot: msg.author.bot,
content: msg.content + (msg.editedTimestamp != null ? " (edited)" : ""),
attachments: msg.attachments,
reply: msg.referencedMessage,
isHistory: true,
});
}
}
console.log("--Review-Complete".padEnd(73, "-"));
}
let numLines = "";
function startExtendedHistory() {
numLines = "";
extendedHistory = true;
stdout.write(":lines> ");
}
async function getExtendedHistory() {
numLines = numLines.trim();
numLines = parseInt(numLines);
if (isNaN(numLines)) {
console.log("<not a number>");
extendedHistory = false;
return;
}
try {
await getHistory(numLines);
} catch (err) {
console.log("<failed to get history: " + err.message + ">");
}
extendedHistory = false;
}
stdin.on("data", function (key) {
if (guildSwitch) {
if (key === "\r") {
@ -517,6 +618,23 @@ stdin.on("data", function (key) {
toSend += key;
}
}
} else if (extendedHistory) {
if (key === "\r") {
console.log("");
getExtendedHistory();
} else {
if (key === "\b") {
if (numLines.length > 0) {
stdout.moveCursor(-1);
stdout.write(" ");
stdout.moveCursor(-1);
numLines = numLines.substring(0, numLines.length - 1);
}
} else {
stdout.write(key);
numLines += key;
}
}
} else {
switch (key) {
case "\u0003":
@ -568,6 +686,22 @@ stdin.on("data", function (key) {
startEmote();
break;
}
case "r": {
if (currentChannel == null) {
console.log("<not in a channel>");
break;
}
getHistory();
break;
}
case "R": {
if (currentChannel == null) {
console.log("<not in a channel>");
break;
}
startExtendedHistory();
break;
}
case " ":
case "\r":
default: {