new messageEdit flow
diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts
index 6a79447..5277383 100644
--- a/src/events/interactionCreate.ts
+++ b/src/events/interactionCreate.ts
@@ -46,6 +46,14 @@
: false;
return await modifySuggestion(interaction, value);
}
+ if (interaction.customId.startsWith("log:edit:")) {
+ const messageId = interaction.customId.split(":")[2];
+ const message = await interaction.channel?.messages.fetch(messageId!);
+ const attachment = message?.attachments.find((a) => a.name === "log.json");
+ if (!attachment) return;
+ const log = JSON.parse(Buffer.from(await (await fetch(attachment.url)).text(), 'base64').toString('binary'));
+ console.log(log);
+ }
switch (interaction.customId) {
case "rolemenu": {
return await roleMenu(interaction);
diff --git a/src/events/messageEdit.ts b/src/events/messageEdit.ts
index f41901c..5964df9 100644
--- a/src/events/messageEdit.ts
+++ b/src/events/messageEdit.ts
@@ -1,7 +1,8 @@
import type { NucleusClient } from "../utils/client.js";
-import { AttachmentBuilder, Message, MessageReference } from "discord.js";
+import { Message, MessageReference, ButtonStyle } from "discord.js";
import type Discord from "discord.js";
import * as diff from "diff";
+import addPlural from "../utils/plurals.js";
export const event = "messageUpdate";
@@ -9,8 +10,17 @@
if (newMessage.author.id === client.user!.id) return;
if (newMessage.author.bot) return;
if (!newMessage.guild) return;
- const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } =
- client.logger;
+ const {
+ log,
+ isLogging,
+ NucleusColors,
+ entry,
+ renderUser,
+ renderDelta,
+ renderNumberDelta,
+ renderChannel,
+ preLog
+ } = client.logger;
const replyTo: MessageReference | null = newMessage.reference;
const newContent = newMessage.cleanContent.replaceAll("`", "‘");
const oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
@@ -63,47 +73,10 @@
return;
}
const differences = diff.diffChars(oldContent, newContent);
- const green = "\x1B[36m";
- const red = "\x1B[41m";
- const skipped = "\x1B[40;33m";
- const reset = "\x1B[0m";
- const bold = "\x1B[1m";
- // console.log(differences);
- // let contentAdd = "";
- // let contentRemove = "";
- // if (differences.map((d) => (d.added || d.removed ? 1 : 0)).filter((f) => f === 1).length > 0) {
- // const cutoff = 20;
- // differences.forEach((part) => {
- // if (!part.added && !part.removed && part.value.length > cutoff) {
- // contentAdd +=
- // reset +
- // part.value.slice(0, cutoff / 2) +
- // skipped +
- // `(${part.value.length - cutoff} more)` +
- // reset +
- // part.value.slice(-(cutoff / 2));
- // contentRemove +=
- // reset +
- // part.value.slice(0, cutoff / 2) +
- // skipped +
- // `(${part.value.length - cutoff} more)` +
- // reset +
- // part.value.slice(-(cutoff / 2));
- // } else {
- // if (part.added || part.removed) {
- // part.value = part.value.replaceAll(" ", "▁");
- // }
- // if (part.added) {
- // contentAdd += green + part.value + reset;
- // } else if (part.removed) {
- // contentRemove += red + part.value + reset;
- // } else {
- // contentAdd += part.value;
- // contentRemove += part.value;
- // }
- // }
- // });
- const key = `\n\n${bold}Key:${reset} ${green}Added${reset} | ${red}Removed${reset} | ${skipped}Skipped${reset}`;
+ const charsAdded = differences.filter((x) => x.added).length;
+ const charsRemoved = differences.filter((x) => x.removed).length;
+ const preLogMessage = await preLog(newMessage.guild.id, JSON.stringify(differences, null, 2));
+ if (!preLogMessage) return;
const data = {
meta: {
type: "messageUpdate",
@@ -112,16 +85,10 @@
color: NucleusColors.yellow,
emoji: "MESSAGE.EDIT",
timestamp: newMessage.editedTimestamp,
- files: [
- new AttachmentBuilder(Buffer.from(JSON.stringify(differences), "base64"), {
- name: "diff.json",
- description: "A JSON file containing the differences between the two messages."
- })
- ],
- showDetails: true
+ changes: { messageId: `${preLogMessage.id}`, buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:edit:${preLogMessage.id}` }
},
separate: {
- start: `To read the full log press the button below.\n\`\`\`ansi\n${key}\`\`\``,
+ start: `${charsAdded} ${addPlural(charsAdded, "character")} added, ${charsRemoved} ${addPlural(charsRemoved, "character")} removed`,
end: `[[Jump to message]](${newMessage.url})`
},
list: {
diff --git a/src/utils/log.ts b/src/utils/log.ts
index 30804b3..325aaa8 100644
--- a/src/utils/log.ts
+++ b/src/utils/log.ts
@@ -106,6 +106,22 @@
throw e;
}
},
+ async preLog(guild: string, file: string): Promise<Discord.Message | void> {
+ const config = await client.database.guilds.read(guild);
+ if (!config.logging.logs.channel) return;
+ const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
+ if (!channel) return;
+ const message = await channel.send({
+ files: [
+ {
+ attachment: Buffer.from(file, "base64"),
+ name: "log.json"
+ }
+ ],
+ flags: ["SuppressEmbeds"]
+ });
+ return message;
+ },
async log(log: LoggerOptions): Promise<void> {
if (!(await isLogging(log.hidden.guild, log.meta.calculateType))) return;
const config = await client.database.guilds.read(log.hidden.guild);