From e3e6fe419cc924eaa74935c9027548d6c7661237 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 20 Jun 2023 20:48:58 -0600 Subject: [PATCH 1/6] reconnection messages --- src/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/index.js b/src/index.js index ccbb7f1..ffd4ef7 100644 --- a/src/index.js +++ b/src/index.js @@ -31,6 +31,7 @@ process.title = "comcord"; global.comcord = { config, state: { + connected: true, rpcConnected: false, startTime: Date.now(), currentGuild: null, @@ -118,6 +119,15 @@ client.once("ready", function () { } }); client.on("error", function () {}); +client.on("ready", function () { + if (comcord.state.connected === false) { + console.log("% Reconnected"); + } +}); +client.on("disconnect", function () { + comcord.state.connected = false; + console.log("% Disconnected, retrying..."); +}); rpc.on("connected", function () { comcord.state.rpcConnected = true; From b988416ae3747bf2f0d4adac54e71a2a02a495c7 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 20 Jun 2023 20:49:24 -0600 Subject: [PATCH 2/6] mentions --- src/index.js | 15 +++++++++++++++ src/lib/messages.js | 33 ++++++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index ffd4ef7..b51dac1 100644 --- a/src/index.js +++ b/src/index.js @@ -157,6 +157,21 @@ rpc.once("ready", function () { rpc.on("error", function () {}); client.on("messageCreate", async function (msg) { + if ( + (msg.mentions.find((user) => user.id == client.user.id) || + msg.mentionEveryone) && + msg.channel.id != comcord.state.currentChannel && + msg.channel.type !== Constants.ChannelTypes.DM && + msg.channel.type !== Constants.ChannelTypes.GROUP_DM + ) { + const data = {ping: true, channel: msg.channel, author: msg.author}; + if (comcord.state.inPrompt) { + comcord.state.messageQueue.push(data); + } else { + processMessage(data); + } + } + if (!msg.author) return; if (msg.author.id === client.user.id) return; diff --git a/src/lib/messages.js b/src/lib/messages.js index d4917f8..9c9519f 100644 --- a/src/lib/messages.js +++ b/src/lib/messages.js @@ -3,7 +3,7 @@ const chalk = require("chalk"); const REGEX_CODEBLOCK = /```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)\n*```/i; const REGEX_CODEBLOCK_GLOBAL = - /```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)\n*```/gi; + /```(?:[a-z0-9_+\-.]+?\n)?\n*([^\n][^]*?)\n*```/gi; const REGEX_MENTION = /<@!?(\d+)>/g; const REGEX_ROLE_MENTION = /<@&?(\d+)>/g; @@ -163,6 +163,7 @@ function formatMessage({ stickers, reply, timestamp, + mention = false, noColor = false, dump = false, history = false, @@ -301,13 +302,17 @@ function formatMessage({ )} ${content}` ); } else { - const nameColor = bot ? chalk.bold.yellow : chalk.bold.cyan; + const nameColor = mention + ? chalk.bold.red + : bot + ? chalk.bold.yellow + : chalk.bold.cyan; - // TODO: markdown console.log( nameColor(`[${name}]`) + " ".repeat(Math.abs(comcord.state.nameLength - (name.length + 2))) + - chalk.reset(" " + content) + chalk.reset(" " + content) + + (mention ? "\x07" : "") ); } } @@ -356,6 +361,14 @@ function processMessage(msg, options = {}) { if (msg.time) { console.log(msg.content); + } else if (msg.ping) { + console.log( + chalk.bold.red( + `**mentioned by ${msg.author?.username ?? ""} in #${ + msg.channel?.name ?? "" + } in ${msg.channel.guild?.name ?? ""}**\x07` + ) + ); } else if (msg.content && msg.content.indexOf("\n") > -1) { if (msg.content.match(REGEX_CODEBLOCK)) { formatMessage({ @@ -364,12 +377,15 @@ function processMessage(msg, options = {}) { bot: msg.author.bot, content: msg.content.replace( REGEX_CODEBLOCK_GLOBAL, - (_, lang, content) => content + (_, content) => content ), attachments: msg.attachments, stickers: msg.stickerItems, reply: msg.referencedMessage, timestamp: msg.timestamp, + mention: + msg.mentionsEveryone || + msg.mentions.find((user) => user.id == comcord.client.user.id), dump: true, ...options, }); @@ -390,6 +406,10 @@ function processMessage(msg, options = {}) { stickers: index == lines.length - 1 ? msg.stickerItems : [], reply: index == 0 ? msg.referencedMessage : null, timestamp: msg.timestamp, + mention: + index == 0 && + (msg.mentionsEveryone || + msg.mentions.find((user) => user.id == comcord.client.user.id)), ...options, }); } @@ -404,6 +424,9 @@ function processMessage(msg, options = {}) { stickers: msg.stickerItems, reply: msg.referencedMessage, timestamp: msg.timestamp, + mention: + msg.mentionsEveryone || + msg.mentions.find((user) => user.id == comcord.client.user.id), ...options, }); } From 8ea671bad384109b89f51b665cc5039e3df69882 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 20 Jun 2023 20:56:13 -0600 Subject: [PATCH 3/6] dont print disconnected if quitting --- src/commands/quit.js | 1 + src/index.js | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/commands/quit.js b/src/commands/quit.js index 931d5a2..3adb706 100644 --- a/src/commands/quit.js +++ b/src/commands/quit.js @@ -1,6 +1,7 @@ const {addCommand} = require("../lib/command"); addCommand("q", "quit comcord", function () { + comcord.state.quitting = true; comcord.client.disconnect({reconnect: false}); process.exit(0); }); diff --git a/src/index.js b/src/index.js index b51dac1..e6a19f9 100644 --- a/src/index.js +++ b/src/index.js @@ -125,8 +125,10 @@ client.on("ready", function () { } }); client.on("disconnect", function () { - comcord.state.connected = false; - console.log("% Disconnected, retrying..."); + if (!comcord.state.quitting) { + comcord.state.connected = false; + console.log("% Disconnected, retrying..."); + } }); rpc.on("connected", function () { From b67347b18e87a3b825b83ee80b3cbfaed20ff935 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 20 Jun 2023 21:08:47 -0600 Subject: [PATCH 4/6] add peek --- src/commands/history.js | 39 ++++++++++++++++++++++++++++++++++++--- src/index.js | 1 + 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/commands/history.js b/src/commands/history.js index 7067b07..890e6ba 100644 --- a/src/commands/history.js +++ b/src/commands/history.js @@ -1,15 +1,16 @@ const {addCommand} = require("../lib/command"); const {startPrompt} = require("../lib/prompt"); const {processMessage} = require("../lib/messages"); +const {listChannels} = require("./listChannels"); -async function getHistory(limit = 20) { - if (!comcord.state.currentChannel) { +async function getHistory(limit = 20, channel = null) { + if (!channel && !comcord.state.currentChannel) { console.log(""); return; } const messages = await comcord.client.getMessages( - comcord.state.currentChannel, + channel ?? comcord.state.currentChannel, {limit} ); messages.reverse(); @@ -44,3 +45,35 @@ addCommand("R", "extended history", function () { await getExtendedHistory(input); }); }); +addCommand("p", "peek at channel", function () { + if (!comcord.state.currentGuild) { + console.log(""); + return; + } + + listChannels(); + startPrompt(":peek> ", async function (input) { + console.log(""); + if (input == "") { + return; + } + let target; + + const guild = comcord.client.guilds.get(comcord.state.currentGuild); + const channels = [...guild.channels.values()].filter((c) => c.type == 0); + channels.sort((a, b) => a.position - b.position); + + for (const channel of channels) { + if (channel.name.toLowerCase().indexOf(input.toLowerCase()) > -1) { + target = channel.id; + break; + } + } + + if (target == null) { + console.log(""); + } else { + await getHistory(20, target); + } + }); +}); diff --git a/src/index.js b/src/index.js index e6a19f9..86aa96f 100644 --- a/src/index.js +++ b/src/index.js @@ -267,6 +267,7 @@ client.on("messageReactionAdd", async function (msg, emoji, reactor) { referencedMessage: reply, author: reactor?.user ?? client.users.get(reactor.id), timestamp: Date.now(), + mentions: [], content: `*reacted with ${emoji.id ? `:${emoji.name}:` : emoji.name}*`, }; From 5a0c0e9b67f15bf097564bac891bb4efe6bcb14d Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 20 Jun 2023 21:31:20 -0600 Subject: [PATCH 5/6] history now does lines and not messages --- src/commands/history.js | 8 +++--- src/lib/messages.js | 59 ++++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/commands/history.js b/src/commands/history.js index 890e6ba..80ba873 100644 --- a/src/commands/history.js +++ b/src/commands/history.js @@ -10,16 +10,18 @@ async function getHistory(limit = 20, channel = null) { } const messages = await comcord.client.getMessages( - channel ?? comcord.state.currentChannel, - {limit} + channel ?? comcord.state.currentChannel ); messages.reverse(); console.log("--Beginning-Review".padEnd(72, "-")); + const lines = []; for (const msg of messages) { - processMessage(msg, {noColor: true, history: true}); + const processedLines = processMessage(msg, {noColor: true, history: true}); + if (processedLines) lines.push(...processedLines); } + console.log(lines.slice(-limit).join("\n")); console.log("--Review-Complete".padEnd(73, "-")); } diff --git a/src/lib/messages.js b/src/lib/messages.js index 9c9519f..9523c53 100644 --- a/src/lib/messages.js +++ b/src/lib/messages.js @@ -176,6 +176,16 @@ function formatMessage({ minutes = dateObj.getUTCMinutes().toString().padStart(2, "0"), seconds = dateObj.getUTCSeconds().toString().padStart(2, "0"); + let console = global.console; + const lines = []; + if (history) { + console = { + log: function (...args) { + lines.push(...args.join(" ").split("\n")); + }, + }; + } + if (name.length + 2 > comcord.state.nameLength) comcord.state.nameLength = name.length + 2; @@ -343,6 +353,10 @@ function formatMessage({ } } } + + if (history) { + return lines; + } } function processMessage(msg, options = {}) { @@ -371,7 +385,7 @@ function processMessage(msg, options = {}) { ); } else if (msg.content && msg.content.indexOf("\n") > -1) { if (msg.content.match(REGEX_CODEBLOCK)) { - formatMessage({ + return formatMessage({ channel: msg.channel, name: msg.author.username, bot: msg.author.bot, @@ -391,31 +405,34 @@ function processMessage(msg, options = {}) { }); } else { const lines = msg.content.split("\n"); + const outLines = []; for (const index in lines) { const line = lines[index]; - formatMessage({ - channel: msg.channel, - 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 : [], - stickers: index == lines.length - 1 ? msg.stickerItems : [], - reply: index == 0 ? msg.referencedMessage : null, - timestamp: msg.timestamp, - mention: - index == 0 && - (msg.mentionsEveryone || - msg.mentions.find((user) => user.id == comcord.client.user.id)), - ...options, - }); + outLines.push( + formatMessage({ + channel: msg.channel, + 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 : [], + stickers: index == lines.length - 1 ? msg.stickerItems : [], + reply: index == 0 ? msg.referencedMessage : null, + timestamp: msg.timestamp, + mention: + index == 0 && + (msg.mentionsEveryone || + msg.mentions.find((user) => user.id == comcord.client.user.id)), + ...options, + }) + ); } } } else { - formatMessage({ + return formatMessage({ channel: msg.channel, name: msg.author.username, bot: msg.author.bot, From e0e4f3802c90f48c5c07c263f50dd1a78d3cb686 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 20 Jun 2023 22:34:42 -0600 Subject: [PATCH 6/6] markdown --- README.md | 5 +- src/lib/messages.js | 120 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 105 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index f09ce34..dbc8d73 100644 --- a/README.md +++ b/README.md @@ -50,10 +50,11 @@ You **MUST** grant your bot all Privileged Gateway Intents. - [x] AFK toggle (A) - [x] Send DM (s) - [x] Answer DM (a) + - [x] Peek (p) - [x] Message Receiving - [x] Markdown styling - - [ ] Common markdown (bold, italic, etc) - - [ ] Figure out how spoilers would work + - [x] Common markdown (bold, italic, etc) + - [x] Figure out how spoilers would work - [x] Emotes????? - [x] Timestamp parsing - [x] Mentions parsing diff --git a/src/lib/messages.js b/src/lib/messages.js index 9523c53..c63394c 100644 --- a/src/lib/messages.js +++ b/src/lib/messages.js @@ -11,6 +11,16 @@ const REGEX_CHANNEL = /<#(\d+)>/g; const REGEX_EMOTE = /<(?:\u200b|&)?a?:(\w+):(\d+)>/g; const REGEX_COMMAND = /<\/([^\s]+?):(\d+)>/g; +const REGEX_BLOCKQUOTE = /^ *>>?>? +/; +const REGEX_GREENTEXT = /^(>.+?)(?:\n|$)/; +const REGEX_SPOILER = /\|\|(.+?)\|\|/; +const REGEX_BOLD = /\*\*(.+?)\*\*/g; +const REGEX_UNDERLINE = /__(.+?)__/g; +const REGEX_ITALIC_1 = /\*(.+?)\*/g; +const REGEX_ITALIC_2 = /_(.+?)_/g; +const REGEX_STRIKE = /~~(.+?)~~/g; +const REGEX_3Y3 = /[\u{e0020}-\u{e007e}]{1,}/gu; + function readableTime(time) { const seconds = time / 1000; const minutes = seconds / 60; @@ -154,6 +164,37 @@ function replaceTimestamps(_, time, format = "f") { return TIME_FORMATS[format](time * 1000); } +function replaceStyledMarkdown(content) { + content = content.replace(REGEX_BLOCKQUOTE, chalk.blackBright("\u258e")); + content = content.replace(REGEX_GREENTEXT, (orig) => chalk.green(orig)); + + if (comcord.config.enable3y3) { + content = content.replace(REGEX_3Y3, (text) => + chalk.italic.magenta( + [...text] + .map((char) => String.fromCodePoint(char.codePointAt(0) - 0xe0000)) + .join("") + ) + ); + } + + content = content.replace(REGEX_SPOILER, (_, text) => + chalk.bgBlack.black(text) + ); + content = content.replace(REGEX_STRIKE, (_, text) => + chalk.strikethrough(text) + ); + content = content.replace(REGEX_BOLD, (_, text) => chalk.bold(text)); + content = content.replace(REGEX_UNDERLINE, (_, text) => + chalk.underline(text) + ); + content = content + .replace(REGEX_ITALIC_1, (_, text) => chalk.italic(text)) + .replace(REGEX_ITALIC_2, (_, text) => chalk.italic(text)); + + return content; +} + function formatMessage({ channel, name, @@ -223,13 +264,11 @@ function formatMessage({ console.log( chalk.bold.white(" \u250d ") + nameColor(`[${reply.author.username}] `) + - chalk.reset( - `${ - length > 79 - ? replyContent.substring(0, 79 - headerLength) + "\u2026" - : replyContent - }` - ) + `${ + length > 79 + ? replyContent.substring(0, 79 - headerLength) + "\u2026" + : replyContent + }` ); } } @@ -272,18 +311,46 @@ function formatMessage({ if (dm) { if (noColor) { + if (comcord.config.enable3y3) { + content = content.replace( + REGEX_3Y3, + (text) => + `<3y3:${[...text] + .map((char) => + String.fromCodePoint(char.codePointAt(0) - 0xe0000) + ) + .join("")}>` + ); + } + console.log(`*${name}* ${content}\x07`); } else { - console.log( - chalk.bold.red(`*${name}*`) + chalk.reset(" " + content + "\x07") - ); + content = replaceStyledMarkdown(content); + + console.log(`${chalk.bold.red(`*${name}*`)} ${content}\x07`); } } else if ( - (content.length > 1 && - content.startsWith("*") && - content.endsWith("*")) || - (content.startsWith("_") && content.endsWith("_")) + content.length > 1 && + ((content.startsWith("*") && + content.endsWith("*") && + !content.startsWith("**") && + !content.endsWith("**")) || + (content.startsWith("_") && + content.endsWith("_") && + !content.startsWith("__") && + !content.endsWith("__"))) ) { + if (comcord.config.enable3y3) { + content = content.replace( + REGEX_3Y3, + (text) => + `<3y3:${[...text] + .map((char) => + String.fromCodePoint(char.codePointAt(0) - 0xe0000) + ) + .join("")}>` + ); + } const str = `<${name} ${content.substring(1, content.length - 1)}>`; if (noColor) { console.log(str); @@ -306,6 +373,18 @@ function formatMessage({ } } else { if (noColor) { + if (comcord.config.enable3y3) { + content = content.replace( + REGEX_3Y3, + (text) => + `<3y3:${[...text] + .map((char) => + String.fromCodePoint(char.codePointAt(0) - 0xe0000) + ) + .join("")}>` + ); + } + console.log( `[${name}]${" ".repeat( Math.abs(comcord.state.nameLength - (name.length + 2)) @@ -318,11 +397,12 @@ function formatMessage({ ? chalk.bold.yellow : chalk.bold.cyan; + content = replaceStyledMarkdown(content); + console.log( - nameColor(`[${name}]`) + - " ".repeat(Math.abs(comcord.state.nameLength - (name.length + 2))) + - chalk.reset(" " + content) + - (mention ? "\x07" : "") + `${nameColor(`[${name}]`)}${" ".repeat( + Math.abs(comcord.state.nameLength - (name.length + 2)) + )} ${content}${mention ? "\x07" : ""}` ); } } @@ -357,6 +437,7 @@ function formatMessage({ if (history) { return lines; } + return null; } function processMessage(msg, options = {}) { @@ -375,6 +456,7 @@ function processMessage(msg, options = {}) { if (msg.time) { console.log(msg.content); + return null; } else if (msg.ping) { console.log( chalk.bold.red( @@ -383,6 +465,7 @@ function processMessage(msg, options = {}) { } in ${msg.channel.guild?.name ?? ""}**\x07` ) ); + return null; } else if (msg.content && msg.content.indexOf("\n") > -1) { if (msg.content.match(REGEX_CODEBLOCK)) { return formatMessage({ @@ -430,6 +513,7 @@ function processMessage(msg, options = {}) { }) ); } + return outLines; } } else { return formatMessage({