process codeblocks as dumps, minor internal message processing changes
This commit is contained in:
		
							parent
							
								
									ae5f412d4b
								
							
						
					
					
						commit
						1ec0eb8a6b
					
				
					 3 changed files with 120 additions and 119 deletions
				
			
		| 
						 | 
					@ -17,33 +17,7 @@ async function getHistory(limit = 20) {
 | 
				
			||||||
  console.log("--Beginning-Review".padEnd(72, "-"));
 | 
					  console.log("--Beginning-Review".padEnd(72, "-"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  for (const msg of messages) {
 | 
					  for (const msg of messages) {
 | 
				
			||||||
    if (msg.content.indexOf("\n") > -1) {
 | 
					    processMessage(msg, {noColor: true, history: true});
 | 
				
			||||||
      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,
 | 
					 | 
				
			||||||
          noColor: true,
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      processMessage({
 | 
					 | 
				
			||||||
        name: msg.author.username,
 | 
					 | 
				
			||||||
        bot: msg.author.bot,
 | 
					 | 
				
			||||||
        content: msg.content + (msg.editedTimestamp != null ? " (edited)" : ""),
 | 
					 | 
				
			||||||
        attachments: msg.attachments,
 | 
					 | 
				
			||||||
        reply: msg.referencedMessage,
 | 
					 | 
				
			||||||
        noColor: true,
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  console.log("--Review-Complete".padEnd(73, "-"));
 | 
					  console.log("--Review-Complete".padEnd(73, "-"));
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										44
									
								
								src/index.js
									
										
									
									
									
								
							
							
						
						
									
										44
									
								
								src/index.js
									
										
									
									
									
								
							| 
						 | 
					@ -60,27 +60,7 @@ client.on("messageCreate", function (msg) {
 | 
				
			||||||
    if (comcord.state.inPrompt) {
 | 
					    if (comcord.state.inPrompt) {
 | 
				
			||||||
      comcord.state.messageQueue.push(msg);
 | 
					      comcord.state.messageQueue.push(msg);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      if (msg.content.indexOf("\n") > -1) {
 | 
					      processMessage(msg);
 | 
				
			||||||
        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,
 | 
					 | 
				
			||||||
            attachments: index == lines.length - 1 ? msg.attachments : [],
 | 
					 | 
				
			||||||
            reply: index == 0 ? msg.referencedMessage : null,
 | 
					 | 
				
			||||||
          });
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        processMessage({
 | 
					 | 
				
			||||||
          name: msg.author.username,
 | 
					 | 
				
			||||||
          bot: msg.author.bot,
 | 
					 | 
				
			||||||
          content: msg.content,
 | 
					 | 
				
			||||||
          attachments: msg.attachments,
 | 
					 | 
				
			||||||
          reply: msg.referencedMessage,
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					@ -93,27 +73,7 @@ client.on("messageUpdate", function (msg, old) {
 | 
				
			||||||
    if (comcord.state.inPrompt) {
 | 
					    if (comcord.state.inPrompt) {
 | 
				
			||||||
      comcord.state.messageQueue.push(msg);
 | 
					      comcord.state.messageQueue.push(msg);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      if (msg.content.indexOf("\n") > -1) {
 | 
					      processMessage(msg);
 | 
				
			||||||
        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 + (index == lines.length - 1 ? " (edited)" : ""),
 | 
					 | 
				
			||||||
            attachments: index == lines.length - 1 ? msg.attachments : [],
 | 
					 | 
				
			||||||
            reply: index == 0 ? msg.referencedMessage : null,
 | 
					 | 
				
			||||||
          });
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        processMessage({
 | 
					 | 
				
			||||||
          name: msg.author.username,
 | 
					 | 
				
			||||||
          bot: msg.author.bot,
 | 
					 | 
				
			||||||
          content: msg.content + " (edited)",
 | 
					 | 
				
			||||||
          attachments: msg.attachments,
 | 
					 | 
				
			||||||
          reply: msg.referencedMessage,
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,9 @@
 | 
				
			||||||
const chalk = require("chalk");
 | 
					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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const REGEX_MENTION = /<@!?(\d+)>/g;
 | 
					const REGEX_MENTION = /<@!?(\d+)>/g;
 | 
				
			||||||
const REGEX_ROLE_MENTION = /<@&?(\d+)>/g;
 | 
					const REGEX_ROLE_MENTION = /<@&?(\d+)>/g;
 | 
				
			||||||
const REGEX_CHANNEL = /<#(\d+)>/g;
 | 
					const REGEX_CHANNEL = /<#(\d+)>/g;
 | 
				
			||||||
| 
						 | 
					@ -149,7 +153,7 @@ function replaceTimestamps(_, time, format = "f") {
 | 
				
			||||||
  return TIME_FORMATS[format](time * 1000);
 | 
					  return TIME_FORMATS[format](time * 1000);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function processMessage({
 | 
					function formatMessage({
 | 
				
			||||||
  name,
 | 
					  name,
 | 
				
			||||||
  content,
 | 
					  content,
 | 
				
			||||||
  bot,
 | 
					  bot,
 | 
				
			||||||
| 
						 | 
					@ -157,6 +161,8 @@ function processMessage({
 | 
				
			||||||
  stickers,
 | 
					  stickers,
 | 
				
			||||||
  reply,
 | 
					  reply,
 | 
				
			||||||
  noColor = false,
 | 
					  noColor = false,
 | 
				
			||||||
 | 
					  dump = false,
 | 
				
			||||||
 | 
					  history = false,
 | 
				
			||||||
}) {
 | 
					}) {
 | 
				
			||||||
  if (name.length + 2 > comcord.state.nameLength)
 | 
					  if (name.length + 2 > comcord.state.nameLength)
 | 
				
			||||||
    comcord.state.nameLength = name.length + 2;
 | 
					    comcord.state.nameLength = name.length + 2;
 | 
				
			||||||
| 
						 | 
					@ -165,7 +171,6 @@ function processMessage({
 | 
				
			||||||
    const nameColor = reply.author.bot ? chalk.bold.yellow : chalk.bold.cyan;
 | 
					    const nameColor = reply.author.bot ? chalk.bold.yellow : chalk.bold.cyan;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const headerLength = 5 + reply.author.username.length;
 | 
					    const headerLength = 5 + reply.author.username.length;
 | 
				
			||||||
    const length = headerLength + reply.content.length;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let replyContent = reply.content.replace(/\n/g, " ");
 | 
					    let replyContent = reply.content.replace(/\n/g, " ");
 | 
				
			||||||
    replyContent = replyContent
 | 
					    replyContent = replyContent
 | 
				
			||||||
| 
						 | 
					@ -175,6 +180,14 @@ function processMessage({
 | 
				
			||||||
      .replace(REGEX_EMOTE, replaceEmotes)
 | 
					      .replace(REGEX_EMOTE, replaceEmotes)
 | 
				
			||||||
      .replace(REGEX_COMMAND, replaceCommands)
 | 
					      .replace(REGEX_COMMAND, replaceCommands)
 | 
				
			||||||
      .replace(REGEX_TIMESTAMP, replaceTimestamps);
 | 
					      .replace(REGEX_TIMESTAMP, replaceTimestamps);
 | 
				
			||||||
 | 
					    if (reply.attachments.length > 0) {
 | 
				
			||||||
 | 
					      replyContent += `<${reply.attachments.length} attachment${
 | 
				
			||||||
 | 
					        reply.attachments.length > 1 ? "s" : ""
 | 
				
			||||||
 | 
					      }>`;
 | 
				
			||||||
 | 
					      replyContent = replyContent.trim();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const length = headerLength + replyContent.length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (noColor) {
 | 
					    if (noColor) {
 | 
				
			||||||
      console.log(
 | 
					      console.log(
 | 
				
			||||||
| 
						 | 
					@ -199,43 +212,74 @@ function processMessage({
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  content = content
 | 
					  if (dump) {
 | 
				
			||||||
    .replace(REGEX_MENTION, replaceMentions)
 | 
					    if (history) {
 | 
				
			||||||
    .replace(REGEX_ROLE_MENTION, replaceRoles)
 | 
					      const headerLength = 80 - (name.length + 5);
 | 
				
			||||||
    .replace(REGEX_CHANNEL, replaceChannels)
 | 
					      console.log(`--- ${name} ${"-".repeat(headerLength)}`);
 | 
				
			||||||
    .replace(REGEX_EMOTE, replaceEmotes)
 | 
					      console.log(content);
 | 
				
			||||||
    .replace(REGEX_COMMAND, replaceCommands)
 | 
					      console.log(`--- ${name} ${"-".repeat(headerLength)}`);
 | 
				
			||||||
    .replace(REGEX_TIMESTAMP, replaceTimestamps);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (
 | 
					 | 
				
			||||||
    (content.length > 1 && content.startsWith("*") && content.endsWith("*")) ||
 | 
					 | 
				
			||||||
    (content.startsWith("_") && content.endsWith("_"))
 | 
					 | 
				
			||||||
  ) {
 | 
					 | 
				
			||||||
    if (noColor) {
 | 
					 | 
				
			||||||
      console.log(`<${name} ${content.substring(1, content.length - 1)}>`);
 | 
					 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      console.log(
 | 
					      const wordCount = content.split(" ").length;
 | 
				
			||||||
        chalk.bold.green(
 | 
					      const lineCount = content.split("\n").length;
 | 
				
			||||||
          `<${name} ${content.substring(1, content.length - 1)}>`
 | 
					      if (noColor) {
 | 
				
			||||||
        )
 | 
					        console.log(
 | 
				
			||||||
      );
 | 
					          `<${name} DUMPs in ${content.length} characters of ${wordCount} word${
 | 
				
			||||||
 | 
					            wordCount > 1 ? "s" : ""
 | 
				
			||||||
 | 
					          } in ${lineCount} line${lineCount > 1 ? "s" : ""}>`
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        console.log(
 | 
				
			||||||
 | 
					          chalk.bold.yellow(
 | 
				
			||||||
 | 
					            `<${name} DUMPs in ${
 | 
				
			||||||
 | 
					              content.length
 | 
				
			||||||
 | 
					            } characters of ${wordCount} word${
 | 
				
			||||||
 | 
					              wordCount > 1 ? "s" : ""
 | 
				
			||||||
 | 
					            } in ${lineCount} line${lineCount > 1 ? "s" : ""}>`
 | 
				
			||||||
 | 
					          )
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    if (noColor) {
 | 
					    content = content
 | 
				
			||||||
      console.log(
 | 
					      .replace(REGEX_MENTION, replaceMentions)
 | 
				
			||||||
        `[${name}]${" ".repeat(
 | 
					      .replace(REGEX_ROLE_MENTION, replaceRoles)
 | 
				
			||||||
          Math.abs(comcord.state.nameLength - (name.length + 2))
 | 
					      .replace(REGEX_CHANNEL, replaceChannels)
 | 
				
			||||||
        )} ${content}`
 | 
					      .replace(REGEX_EMOTE, replaceEmotes)
 | 
				
			||||||
      );
 | 
					      .replace(REGEX_COMMAND, replaceCommands)
 | 
				
			||||||
    } else {
 | 
					      .replace(REGEX_TIMESTAMP, replaceTimestamps);
 | 
				
			||||||
      const nameColor = bot ? chalk.bold.yellow : chalk.bold.cyan;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // TODO: markdown
 | 
					    if (
 | 
				
			||||||
      console.log(
 | 
					      (content.length > 1 &&
 | 
				
			||||||
        nameColor(`[${name}]`) +
 | 
					        content.startsWith("*") &&
 | 
				
			||||||
          " ".repeat(Math.abs(comcord.state.nameLength - (name.length + 2))) +
 | 
					        content.endsWith("*")) ||
 | 
				
			||||||
          chalk.reset(" " + content)
 | 
					      (content.startsWith("_") && content.endsWith("_"))
 | 
				
			||||||
      );
 | 
					    ) {
 | 
				
			||||||
 | 
					      if (noColor) {
 | 
				
			||||||
 | 
					        console.log(`<${name} ${content.substring(1, content.length - 1)}>`);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        console.log(
 | 
				
			||||||
 | 
					          chalk.bold.green(
 | 
				
			||||||
 | 
					            `<${name} ${content.substring(1, content.length - 1)}>`
 | 
				
			||||||
 | 
					          )
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      if (noColor) {
 | 
				
			||||||
 | 
					        console.log(
 | 
				
			||||||
 | 
					          `[${name}]${" ".repeat(
 | 
				
			||||||
 | 
					            Math.abs(comcord.state.nameLength - (name.length + 2))
 | 
				
			||||||
 | 
					          )} ${content}`
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        const nameColor = 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)
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -266,33 +310,55 @@ function processMessage({
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function processQueue() {
 | 
					function processMessage(msg, options) {
 | 
				
			||||||
  for (const msg of comcord.state.messageQueue) {
 | 
					  if (msg.time) {
 | 
				
			||||||
    if (msg.time) {
 | 
					    console.log(msg.content);
 | 
				
			||||||
      console.log(msg.content);
 | 
					  } else if (msg.content.indexOf("\n") > -1) {
 | 
				
			||||||
    } else if (msg.content.indexOf("\n") > -1) {
 | 
					    if (msg.content.match(REGEX_CODEBLOCK)) {
 | 
				
			||||||
 | 
					      formatMessage({
 | 
				
			||||||
 | 
					        name: msg.author.username,
 | 
				
			||||||
 | 
					        bot: msg.author.bot,
 | 
				
			||||||
 | 
					        content: msg.content.replace(
 | 
				
			||||||
 | 
					          REGEX_CODEBLOCK_GLOBAL,
 | 
				
			||||||
 | 
					          (_, lang, content) => content
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					        attachments: msg.attachments,
 | 
				
			||||||
 | 
					        stickers: msg.stickerItems,
 | 
				
			||||||
 | 
					        reply: msg.referencedMessage,
 | 
				
			||||||
 | 
					        dump: true,
 | 
				
			||||||
 | 
					        ...options,
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
      const lines = msg.content.split("\n");
 | 
					      const lines = msg.content.split("\n");
 | 
				
			||||||
      for (const index in lines) {
 | 
					      for (const index in lines) {
 | 
				
			||||||
        const line = lines[index];
 | 
					        const line = lines[index];
 | 
				
			||||||
        processMessage({
 | 
					        formatMessage({
 | 
				
			||||||
          name: msg.author.username,
 | 
					          name: msg.author.username,
 | 
				
			||||||
          bot: msg.author.bot,
 | 
					          bot: msg.author.bot,
 | 
				
			||||||
          content: line,
 | 
					          content: line,
 | 
				
			||||||
          attachments: index == lines.length - 1 ? msg.attachments : [],
 | 
					          attachments: index == lines.length - 1 ? msg.attachments : [],
 | 
				
			||||||
          stickers: index == lines.length - 1 ? msg.stickerItems : [],
 | 
					          stickers: index == lines.length - 1 ? msg.stickerItems : [],
 | 
				
			||||||
          reply: index == 0 ? msg.referencedMessage : null,
 | 
					          reply: index == 0 ? msg.referencedMessage : null,
 | 
				
			||||||
 | 
					          ...options,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      processMessage({
 | 
					 | 
				
			||||||
        name: msg.author.username,
 | 
					 | 
				
			||||||
        bot: msg.author.bot,
 | 
					 | 
				
			||||||
        content: msg.content,
 | 
					 | 
				
			||||||
        attachments: msg.attachments,
 | 
					 | 
				
			||||||
        stickers: msg.stickerItems,
 | 
					 | 
				
			||||||
        reply: msg.referencedMessage,
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    formatMessage({
 | 
				
			||||||
 | 
					      name: msg.author.username,
 | 
				
			||||||
 | 
					      bot: msg.author.bot,
 | 
				
			||||||
 | 
					      content: msg.content,
 | 
				
			||||||
 | 
					      attachments: msg.attachments,
 | 
				
			||||||
 | 
					      stickers: msg.stickerItems,
 | 
				
			||||||
 | 
					      reply: msg.referencedMessage,
 | 
				
			||||||
 | 
					      ...options,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function processQueue() {
 | 
				
			||||||
 | 
					  for (const msg of comcord.state.messageQueue) {
 | 
				
			||||||
 | 
					    processMessage(msg);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  comcord.state.messageQueue.splice(0, comcord.state.messageQueue.length);
 | 
					  comcord.state.messageQueue.splice(0, comcord.state.messageQueue.length);
 | 
				
			||||||
| 
						 | 
					@ -301,4 +367,5 @@ function processQueue() {
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
  processMessage,
 | 
					  processMessage,
 | 
				
			||||||
  processQueue,
 | 
					  processQueue,
 | 
				
			||||||
 | 
					  formatMessage,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue