diff --git a/README.md b/README.md index dbc8d73..f09ce34 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,10 @@ 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 - - [x] Common markdown (bold, italic, etc) - - [x] Figure out how spoilers would work + - [ ] Common markdown (bold, italic, etc) + - [ ] Figure out how spoilers would work - [x] Emotes????? - [x] Timestamp parsing - [x] Mentions parsing diff --git a/src/commands/history.js b/src/commands/history.js index 80ba873..7067b07 100644 --- a/src/commands/history.js +++ b/src/commands/history.js @@ -1,27 +1,24 @@ const {addCommand} = require("../lib/command"); const {startPrompt} = require("../lib/prompt"); const {processMessage} = require("../lib/messages"); -const {listChannels} = require("./listChannels"); -async function getHistory(limit = 20, channel = null) { - if (!channel && !comcord.state.currentChannel) { +async function getHistory(limit = 20) { + if (!comcord.state.currentChannel) { console.log(""); return; } const messages = await comcord.client.getMessages( - channel ?? comcord.state.currentChannel + comcord.state.currentChannel, + {limit} ); messages.reverse(); console.log("--Beginning-Review".padEnd(72, "-")); - const lines = []; for (const msg of messages) { - const processedLines = processMessage(msg, {noColor: true, history: true}); - if (processedLines) lines.push(...processedLines); + processMessage(msg, {noColor: true, history: true}); } - console.log(lines.slice(-limit).join("\n")); console.log("--Review-Complete".padEnd(73, "-")); } @@ -47,35 +44,3 @@ 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/commands/quit.js b/src/commands/quit.js index 3adb706..931d5a2 100644 --- a/src/commands/quit.js +++ b/src/commands/quit.js @@ -1,7 +1,6 @@ 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 86aa96f..ccbb7f1 100644 --- a/src/index.js +++ b/src/index.js @@ -31,7 +31,6 @@ process.title = "comcord"; global.comcord = { config, state: { - connected: true, rpcConnected: false, startTime: Date.now(), currentGuild: null, @@ -119,17 +118,6 @@ client.once("ready", function () { } }); client.on("error", function () {}); -client.on("ready", function () { - if (comcord.state.connected === false) { - console.log("% Reconnected"); - } -}); -client.on("disconnect", function () { - if (!comcord.state.quitting) { - comcord.state.connected = false; - console.log("% Disconnected, retrying..."); - } -}); rpc.on("connected", function () { comcord.state.rpcConnected = true; @@ -159,21 +147,6 @@ 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; @@ -267,7 +240,6 @@ 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}*`, }; diff --git a/src/lib/messages.js b/src/lib/messages.js index c63394c..d4917f8 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; @@ -11,16 +11,6 @@ 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; @@ -164,37 +154,6 @@ 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, @@ -204,7 +163,6 @@ function formatMessage({ stickers, reply, timestamp, - mention = false, noColor = false, dump = false, history = false, @@ -217,16 +175,6 @@ 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; @@ -264,11 +212,13 @@ function formatMessage({ console.log( chalk.bold.white(" \u250d ") + nameColor(`[${reply.author.username}] `) + - `${ - length > 79 - ? replyContent.substring(0, 79 - headerLength) + "\u2026" - : replyContent - }` + chalk.reset( + `${ + length > 79 + ? replyContent.substring(0, 79 - headerLength) + "\u2026" + : replyContent + }` + ) ); } } @@ -311,46 +261,18 @@ 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 { - 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.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("")}>` + console.log( + chalk.bold.red(`*${name}*`) + chalk.reset(" " + content + "\x07") ); } + } else if ( + (content.length > 1 && + content.startsWith("*") && + content.endsWith("*")) || + (content.startsWith("_") && content.endsWith("_")) + ) { const str = `<${name} ${content.substring(1, content.length - 1)}>`; if (noColor) { console.log(str); @@ -373,36 +295,19 @@ 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)) )} ${content}` ); } else { - const nameColor = mention - ? chalk.bold.red - : bot - ? chalk.bold.yellow - : chalk.bold.cyan; - - content = replaceStyledMarkdown(content); + const nameColor = bot ? chalk.bold.yellow : chalk.bold.cyan; + // TODO: markdown console.log( - `${nameColor(`[${name}]`)}${" ".repeat( - Math.abs(comcord.state.nameLength - (name.length + 2)) - )} ${content}${mention ? "\x07" : ""}` + nameColor(`[${name}]`) + + " ".repeat(Math.abs(comcord.state.nameLength - (name.length + 2))) + + chalk.reset(" " + content) ); } } @@ -433,11 +338,6 @@ function formatMessage({ } } } - - if (history) { - return lines; - } - return null; } function processMessage(msg, options = {}) { @@ -456,67 +356,46 @@ function processMessage(msg, options = {}) { if (msg.time) { console.log(msg.content); - return null; - } else if (msg.ping) { - console.log( - chalk.bold.red( - `**mentioned by ${msg.author?.username ?? ""} in #${ - msg.channel?.name ?? "" - } 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({ + formatMessage({ channel: msg.channel, name: msg.author.username, bot: msg.author.bot, content: msg.content.replace( REGEX_CODEBLOCK_GLOBAL, - (_, content) => content + (_, lang, 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, }); } else { const lines = msg.content.split("\n"); - const outLines = []; for (const index in lines) { const line = lines[index]; - 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, - }) - ); + 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, + ...options, + }); } - return outLines; } } else { - return formatMessage({ + formatMessage({ channel: msg.channel, name: msg.author.username, bot: msg.author.bot, @@ -525,9 +404,6 @@ 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, }); }