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) - [ ] Finger (f)
- [ ] Shows presence data if available - [ ] Shows presence data if available
- [ ] Creation date, join date, ID, etc - [ ] Creation date, join date, ID, etc
- [ ] Room history (r) - [x] Room history (r)
- [ ] Extended room history (R) - [x] Extended room history (R)
- [x] Message Receiving - [x] Message Receiving
- [ ] Markdown styling - [ ] Markdown styling
- [ ] Common markdown (bold, italic, etc) - [ ] 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 - [ ] Embeds in the style of commode's posted links
- [x] 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 - [x] Replies
- [x] Message sending - [x] Message sending
- [x] Puts incoming messages into queue whilst in send mode - [x] Puts incoming messages into queue whilst in send mode
- [ ] Mentions - [ ] Mentions

View file

@ -15,6 +15,7 @@ let currentGuild,
inEmoteMode = false, inEmoteMode = false,
guildSwitch = false, guildSwitch = false,
channelSwitch = false, channelSwitch = false,
extendedHistory = false,
nameLength = 2; nameLength = 2;
const messageQueue = []; const messageQueue = [];
@ -52,7 +53,14 @@ client.once("ready", function () {
listGuilds(); 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 (name.length + 2 > nameLength) nameLength = name.length + 2;
if (reply) { if (reply) {
@ -61,39 +69,65 @@ function processMessage({name, content, bot, attachments, reply}) {
const headerLength = 5 + reply.author.username.length; const headerLength = 5 + reply.author.username.length;
const length = headerLength + reply.content.length; const length = headerLength + reply.content.length;
console.log( if (isHistory) {
chalk.bold.white(" \u250d ") + console.log(
nameColor(`[${reply.author.username}] `) + ` \u250d [${reply.author.username}] ${
chalk.reset( length > 79
`${ ? reply.content.substring(0, length - headerLength) + "\u2026"
length > 79 : reply.content
? 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 ( if (
(content.startsWith("*") && content.endsWith("*")) || (content.startsWith("*") && content.endsWith("*")) ||
(content.startsWith("_") && content.endsWith("_")) (content.startsWith("_") && content.endsWith("_"))
) { ) {
console.log( if (isHistory) {
chalk.bold.green(`<${name} ${content.substring(1, content.length - 1)}>`) console.log(`<${name} ${content.subString(1, content.length - 1)}>`);
); } else {
console.log(
chalk.bold.green(
`<${name} ${content.substring(1, content.length - 1)}>`
)
);
}
} else { } 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 // TODO: markdown
console.log( console.log(
nameColor(`[${name}]`) + nameColor(`[${name}]`) +
" ".repeat(nameLength - (name.length + 2)) + " ".repeat(nameLength - (name.length + 2)) +
chalk.reset(" " + content) chalk.reset(" " + content)
); );
}
} }
for (const attachment of attachments) { 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() { function switchGuild() {
targetGuild = targetGuild.trim();
if (targetGuild == "") { if (targetGuild == "") {
listUsers(); listUsers();
guildSwitch = false; guildSwitch = false;
@ -397,6 +432,7 @@ function gotoChannel() {
} }
function switchChannel() { function switchChannel() {
targetChannel = targetChannel.trim();
if (targetChannel == "") { if (targetChannel == "") {
listUsers(); listUsers();
channelSwitch = false; channelSwitch = false;
@ -449,6 +485,71 @@ async function sendEmote() {
processQueue(); 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) { stdin.on("data", function (key) {
if (guildSwitch) { if (guildSwitch) {
if (key === "\r") { if (key === "\r") {
@ -517,6 +618,23 @@ stdin.on("data", function (key) {
toSend += 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 { } else {
switch (key) { switch (key) {
case "\u0003": case "\u0003":
@ -568,6 +686,22 @@ stdin.on("data", function (key) {
startEmote(); startEmote();
break; 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 " ":
case "\r": case "\r":
default: { default: {