2022-08-28 22:01:31 +00:00
|
|
|
const chalk = require("chalk");
|
|
|
|
|
2022-10-02 16:17:39 +00:00
|
|
|
const REGEX_CODEBLOCK = /```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)\n*```/i;
|
|
|
|
const REGEX_CODEBLOCK_GLOBAL =
|
|
|
|
/```(?:([a-z0-9_+\-.]+?)\n)?\n*([^\n][^]*?)\n*```/gi;
|
|
|
|
|
2022-09-30 03:28:29 +00:00
|
|
|
const REGEX_MENTION = /<@!?(\d+)>/g;
|
|
|
|
const REGEX_ROLE_MENTION = /<@&?(\d+)>/g;
|
|
|
|
const REGEX_CHANNEL = /<#(\d+)>/g;
|
|
|
|
const REGEX_EMOTE = /<(?:\u200b|&)?a?:(\w+):(\d+)>/g;
|
|
|
|
const REGEX_COMMAND = /<\/([^\s]+?):(\d+)>/g;
|
|
|
|
|
2022-10-01 19:58:15 +00:00
|
|
|
function readableTime(time) {
|
|
|
|
const seconds = time / 1000;
|
|
|
|
const minutes = seconds / 60;
|
|
|
|
const hours = minutes / 60;
|
|
|
|
const days = hours / 24;
|
|
|
|
const weeks = days / 7;
|
|
|
|
const months = days / 30;
|
|
|
|
const years = days / 365.25;
|
|
|
|
|
|
|
|
if (years >= 1) {
|
|
|
|
return `${years.toFixed(0)} year${years > 1 ? "s" : ""}`;
|
|
|
|
} else if (weeks > 5 && months < 13) {
|
|
|
|
return `${months.toFixed(0)} month${months > 1 ? "s" : ""}`;
|
|
|
|
} else if (days > 7 && weeks < 5) {
|
|
|
|
return `${weeks.toFixed(0)} week${weeks > 1 ? "s" : ""}`;
|
|
|
|
} else if (hours > 24 && days < 7) {
|
|
|
|
return `${days.toFixed(0)} day${days > 1 ? "s" : ""}`;
|
|
|
|
} else if (minutes > 60 && hours < 24) {
|
|
|
|
return `${hours.toFixed(0)} hour${hours > 1 ? "s" : ""}`;
|
|
|
|
} else if (seconds > 60 && minutes < 60) {
|
|
|
|
return `${minutes.toFixed(0)} minute${minutes > 1 ? "s" : ""}`;
|
|
|
|
} else {
|
|
|
|
return `${seconds.toFixed(0)} second${seconds > 1 ? "s" : ""}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MONTH_NAMES = [
|
|
|
|
"January",
|
|
|
|
"Feburary",
|
|
|
|
"March",
|
|
|
|
"April",
|
|
|
|
"May",
|
|
|
|
"June",
|
|
|
|
"July",
|
|
|
|
"August",
|
|
|
|
"September",
|
|
|
|
"October",
|
|
|
|
"November",
|
|
|
|
"December",
|
|
|
|
];
|
|
|
|
const DAY_NAMES = [
|
|
|
|
"Sunday",
|
|
|
|
"Monday",
|
|
|
|
"Tuesday",
|
|
|
|
"Wednesday",
|
|
|
|
"Thursday",
|
|
|
|
"Friday",
|
|
|
|
"Saturday",
|
|
|
|
];
|
|
|
|
const TIME_FORMATS = {
|
|
|
|
t: function (time) {
|
|
|
|
const timeObj = new Date(time);
|
|
|
|
return timeObj.getUTCHours() + 1 + ":" + timeObj.getUTCMinutes();
|
|
|
|
},
|
|
|
|
T: function (time) {
|
|
|
|
const timeObj = new Date(time);
|
|
|
|
return TIME_FORMATS.t(time) + ":" + timeObj.getUTCSeconds();
|
|
|
|
},
|
|
|
|
d: function (time) {
|
|
|
|
const timeObj = new Date(time);
|
|
|
|
return (
|
|
|
|
timeObj.getUTCFullYear() +
|
|
|
|
"/" +
|
|
|
|
(timeObj.getUTCMonth() + 1).toString().padStart(2, "0") +
|
|
|
|
"/" +
|
|
|
|
timeObj.getUTCDate().toString().padStart(2, "0")
|
|
|
|
);
|
|
|
|
},
|
|
|
|
D: function (time) {
|
|
|
|
const timeObj = new Date(time);
|
|
|
|
return (
|
|
|
|
timeObj.getUTCDate() +
|
|
|
|
" " +
|
|
|
|
MONTH_NAMES[timeObj.getUTCMonth()] +
|
|
|
|
" " +
|
|
|
|
timeObj.getUTCFullYear()
|
|
|
|
);
|
|
|
|
},
|
|
|
|
f: function (time) {
|
|
|
|
return TIME_FORMATS.D(time) + " " + TIME_FORMATS.t(time);
|
|
|
|
},
|
|
|
|
F: function (time) {
|
|
|
|
const timeObj = new Date(time);
|
|
|
|
return DAY_NAMES[timeObj.getUTCDay()] + ", " + TIME_FORMATS.f(time);
|
|
|
|
},
|
|
|
|
R: function (time) {
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
if (time > now) {
|
|
|
|
const delta = time - now;
|
|
|
|
return "in " + readableTime(delta);
|
|
|
|
} else {
|
|
|
|
const delta = now - time;
|
|
|
|
return readableTime(delta) + " ago";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const REGEX_TIMESTAMP = new RegExp(
|
|
|
|
`<t:(-?\\d{1,17})(?::(${Object.keys(TIME_FORMATS).join("|")}))?>`,
|
|
|
|
"g"
|
|
|
|
);
|
|
|
|
|
2022-09-30 03:28:29 +00:00
|
|
|
function replaceMentions(_, id) {
|
|
|
|
const user = comcord.client.users.get(id);
|
|
|
|
if (user) {
|
|
|
|
return `@${user.username}`;
|
|
|
|
} else {
|
|
|
|
return "@Unknown User";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function replaceRoles(_, id) {
|
|
|
|
const role = comcord.client.guilds
|
|
|
|
.get(comcord.state.currentGuild)
|
|
|
|
.roles.get(id);
|
|
|
|
if (role) {
|
|
|
|
return `[@${role.name}]`;
|
|
|
|
} else {
|
|
|
|
return "[@Unknown Role]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function replaceChannels(_, id) {
|
|
|
|
const guildForChannel = comcord.client.channelGuildMap[id];
|
|
|
|
if (guildForChannel) {
|
|
|
|
const channel = comcord.client.guilds.get(guildForChannel).channels.get(id);
|
|
|
|
if (channel) {
|
|
|
|
return `#${channel.name}`;
|
|
|
|
} else {
|
|
|
|
return "#unknown-channel";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "#unknown-channel";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function replaceEmotes(_, name, id) {
|
|
|
|
return `:${name}:`;
|
|
|
|
}
|
|
|
|
function replaceCommands(_, name, id) {
|
|
|
|
return `/${name}`;
|
|
|
|
}
|
2022-10-01 19:58:15 +00:00
|
|
|
function replaceTimestamps(_, time, format = "f") {
|
|
|
|
return TIME_FORMATS[format](time * 1000);
|
|
|
|
}
|
2022-09-30 03:28:29 +00:00
|
|
|
|
2022-10-02 16:17:39 +00:00
|
|
|
function formatMessage({
|
2022-08-28 22:01:31 +00:00
|
|
|
name,
|
|
|
|
content,
|
|
|
|
bot,
|
|
|
|
attachments,
|
2022-09-30 03:28:29 +00:00
|
|
|
stickers,
|
2022-08-28 22:01:31 +00:00
|
|
|
reply,
|
|
|
|
noColor = false,
|
2022-10-02 16:17:39 +00:00
|
|
|
dump = false,
|
|
|
|
history = false,
|
2022-10-10 03:15:53 +00:00
|
|
|
dm = false,
|
2022-08-28 22:01:31 +00:00
|
|
|
}) {
|
|
|
|
if (name.length + 2 > comcord.state.nameLength)
|
2022-08-28 23:12:03 +00:00
|
|
|
comcord.state.nameLength = name.length + 2;
|
2022-08-28 22:01:31 +00:00
|
|
|
|
|
|
|
if (reply) {
|
|
|
|
const nameColor = reply.author.bot ? chalk.bold.yellow : chalk.bold.cyan;
|
|
|
|
|
|
|
|
const headerLength = 5 + reply.author.username.length;
|
|
|
|
|
2022-09-30 03:28:29 +00:00
|
|
|
let replyContent = reply.content.replace(/\n/g, " ");
|
|
|
|
replyContent = replyContent
|
|
|
|
.replace(REGEX_MENTION, replaceMentions)
|
|
|
|
.replace(REGEX_ROLE_MENTION, replaceRoles)
|
|
|
|
.replace(REGEX_CHANNEL, replaceChannels)
|
|
|
|
.replace(REGEX_EMOTE, replaceEmotes)
|
2022-10-01 19:58:15 +00:00
|
|
|
.replace(REGEX_COMMAND, replaceCommands)
|
|
|
|
.replace(REGEX_TIMESTAMP, replaceTimestamps);
|
2022-12-21 01:17:25 +00:00
|
|
|
if (reply.attachments.size > 0) {
|
|
|
|
replyContent += ` <${reply.attachments.size} attachment${
|
|
|
|
reply.attachments.size > 1 ? "s" : ""
|
2022-10-02 16:17:39 +00:00
|
|
|
}>`;
|
|
|
|
replyContent = replyContent.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
const length = headerLength + replyContent.length;
|
2022-08-28 23:59:33 +00:00
|
|
|
|
2022-08-28 22:01:31 +00:00
|
|
|
if (noColor) {
|
|
|
|
console.log(
|
|
|
|
` \u250d [${reply.author.username}] ${
|
|
|
|
length > 79
|
2022-09-19 19:19:31 +00:00
|
|
|
? replyContent.substring(0, 79 - headerLength) + "\u2026"
|
2022-08-28 23:59:33 +00:00
|
|
|
: replyContent
|
2022-08-28 22:01:31 +00:00
|
|
|
}`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
chalk.bold.white(" \u250d ") +
|
|
|
|
nameColor(`[${reply.author.username}] `) +
|
|
|
|
chalk.reset(
|
|
|
|
`${
|
|
|
|
length > 79
|
2022-09-19 19:19:31 +00:00
|
|
|
? replyContent.substring(0, 79 - headerLength) + "\u2026"
|
2022-08-28 23:59:33 +00:00
|
|
|
: replyContent
|
2022-08-28 22:01:31 +00:00
|
|
|
}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 16:17:39 +00:00
|
|
|
if (dump) {
|
|
|
|
if (history) {
|
|
|
|
const headerLength = 80 - (name.length + 5);
|
|
|
|
console.log(`--- ${name} ${"-".repeat(headerLength)}`);
|
|
|
|
console.log(content);
|
|
|
|
console.log(`--- ${name} ${"-".repeat(headerLength)}`);
|
2022-08-28 22:01:31 +00:00
|
|
|
} else {
|
2022-10-02 16:17:39 +00:00
|
|
|
const wordCount = content.split(" ").length;
|
|
|
|
const lineCount = content.split("\n").length;
|
|
|
|
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" : ""}>`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-08-28 22:01:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-02 16:17:39 +00:00
|
|
|
content = content
|
|
|
|
.replace(REGEX_MENTION, replaceMentions)
|
|
|
|
.replace(REGEX_ROLE_MENTION, replaceRoles)
|
|
|
|
.replace(REGEX_CHANNEL, replaceChannels)
|
|
|
|
.replace(REGEX_EMOTE, replaceEmotes)
|
|
|
|
.replace(REGEX_COMMAND, replaceCommands)
|
|
|
|
.replace(REGEX_TIMESTAMP, replaceTimestamps);
|
|
|
|
|
2022-10-10 03:15:53 +00:00
|
|
|
if (dm) {
|
|
|
|
if (noColor) {
|
2023-05-31 03:03:44 +00:00
|
|
|
console.log(`*${name}* ${content}\x07`);
|
2022-10-10 03:15:53 +00:00
|
|
|
} else {
|
|
|
|
console.log(
|
2023-05-31 03:03:44 +00:00
|
|
|
chalk.bold.red(`*${name}*`) + chalk.reset(" " + content + "\x07")
|
2022-10-10 03:15:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (
|
2022-10-02 16:17:39 +00:00
|
|
|
(content.length > 1 &&
|
|
|
|
content.startsWith("*") &&
|
|
|
|
content.endsWith("*")) ||
|
|
|
|
(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)}>`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-08-28 22:01:31 +00:00
|
|
|
} else {
|
2022-10-02 16:17:39 +00:00
|
|
|
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;
|
2022-08-28 22:01:31 +00:00
|
|
|
|
2022-10-02 16:17:39 +00:00
|
|
|
// TODO: markdown
|
|
|
|
console.log(
|
|
|
|
nameColor(`[${name}]`) +
|
|
|
|
" ".repeat(Math.abs(comcord.state.nameLength - (name.length + 2))) +
|
|
|
|
chalk.reset(" " + content)
|
|
|
|
);
|
|
|
|
}
|
2022-08-28 22:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachments) {
|
2022-10-09 20:39:15 +00:00
|
|
|
for (const attachment of attachments.values()) {
|
2022-08-28 22:01:31 +00:00
|
|
|
if (noColor) {
|
|
|
|
console.log(`<attachment: ${attachment.url} >`);
|
|
|
|
} else {
|
|
|
|
console.log(chalk.bold.yellow(`<attachment: ${attachment.url} >`));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 03:28:29 +00:00
|
|
|
|
|
|
|
if (stickers) {
|
|
|
|
for (const sticker of stickers) {
|
|
|
|
if (noColor) {
|
|
|
|
console.log(
|
|
|
|
`<sticker: "${sticker.name}" https://media.discordapp.net/stickers/${sticker.id}.png >`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
chalk.bold.yellow(
|
|
|
|
`<sticker: "${sticker.name}" https://media.discordapp.net/stickers/${sticker.id}.png >`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 22:01:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 03:15:53 +00:00
|
|
|
function processMessage(msg, options = {}) {
|
|
|
|
if (msg.channel?.recipient) {
|
|
|
|
options.dm = true;
|
|
|
|
}
|
|
|
|
|
2022-10-02 16:17:39 +00:00
|
|
|
if (msg.time) {
|
|
|
|
console.log(msg.content);
|
2022-10-20 02:23:33 +00:00
|
|
|
} else if (msg.content && msg.content.indexOf("\n") > -1) {
|
2022-10-02 16:17:39 +00:00
|
|
|
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 {
|
2022-08-28 22:01:31 +00:00
|
|
|
const lines = msg.content.split("\n");
|
|
|
|
for (const index in lines) {
|
|
|
|
const line = lines[index];
|
2022-10-02 16:17:39 +00:00
|
|
|
formatMessage({
|
2022-08-28 22:01:31 +00:00
|
|
|
name: msg.author.username,
|
|
|
|
bot: msg.author.bot,
|
2022-10-02 22:32:04 +00:00
|
|
|
content:
|
|
|
|
line +
|
|
|
|
(msg.editedTimestamp != null && index == lines.length - 1
|
|
|
|
? " (edited)"
|
|
|
|
: ""),
|
2022-08-28 22:01:31 +00:00
|
|
|
attachments: index == lines.length - 1 ? msg.attachments : [],
|
2022-09-30 03:28:29 +00:00
|
|
|
stickers: index == lines.length - 1 ? msg.stickerItems : [],
|
2022-08-28 22:01:31 +00:00
|
|
|
reply: index == 0 ? msg.referencedMessage : null,
|
2022-10-02 16:17:39 +00:00
|
|
|
...options,
|
2022-08-28 22:01:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 16:17:39 +00:00
|
|
|
} else {
|
|
|
|
formatMessage({
|
|
|
|
name: msg.author.username,
|
|
|
|
bot: msg.author.bot,
|
2022-10-02 22:32:04 +00:00
|
|
|
content: msg.content + (msg.editedTimestamp != null ? " (edited)" : ""),
|
2022-10-02 16:17:39 +00:00
|
|
|
attachments: msg.attachments,
|
|
|
|
stickers: msg.stickerItems,
|
|
|
|
reply: msg.referencedMessage,
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processQueue() {
|
|
|
|
for (const msg of comcord.state.messageQueue) {
|
|
|
|
processMessage(msg);
|
2022-08-28 22:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
comcord.state.messageQueue.splice(0, comcord.state.messageQueue.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
processMessage,
|
|
|
|
processQueue,
|
2022-10-02 16:17:39 +00:00
|
|
|
formatMessage,
|
2022-08-28 22:01:31 +00:00
|
|
|
};
|