feat: better handling for message edits
Some checks failed
Code Analysis / SonarQube (push) Failing after 18s
Node.js CI / Lint and Test (push) Failing after 33s

This commit is contained in:
2025-07-08 13:43:14 -07:00
parent c47c46e69a
commit 91d7a1ef28
4 changed files with 39 additions and 7 deletions

View File

@ -37,6 +37,7 @@
"@nhcarrigan/logger": "1.0.0", "@nhcarrigan/logger": "1.0.0",
"@octokit/rest": "20.1.1", "@octokit/rest": "20.1.1",
"@prisma/client": "5.13.0", "@prisma/client": "5.13.0",
"diff": "8.0.2",
"discord.js": "14.15.2", "discord.js": "14.15.2",
"dotenv": "16.4.5", "dotenv": "16.4.5",
"express": "4.19.2", "express": "4.19.2",

9
pnpm-lock.yaml generated
View File

@ -17,6 +17,9 @@ importers:
'@prisma/client': '@prisma/client':
specifier: 5.13.0 specifier: 5.13.0
version: 5.13.0(prisma@5.13.0) version: 5.13.0(prisma@5.13.0)
diff:
specifier: 8.0.2
version: 8.0.2
discord.js: discord.js:
specifier: 14.15.2 specifier: 14.15.2
version: 14.15.2 version: 14.15.2
@ -766,6 +769,10 @@ packages:
devtools-protocol@0.0.1147663: devtools-protocol@0.0.1147663:
resolution: {integrity: sha512-hyWmRrexdhbZ1tcJUGpO95ivbRhWXz++F4Ko+n21AY5PNln2ovoJw+8ZMNDTtip+CNFQfrtLVh/w4009dXO/eQ==} resolution: {integrity: sha512-hyWmRrexdhbZ1tcJUGpO95ivbRhWXz++F4Ko+n21AY5PNln2ovoJw+8ZMNDTtip+CNFQfrtLVh/w4009dXO/eQ==}
diff@8.0.2:
resolution: {integrity: sha512-sSuxWU5j5SR9QQji/o2qMvqRNYRDOcBTgsJ/DeCf4iSN4gW+gNMXM7wFIP+fdXZxoNiAnHUTGjCr+TSWXdRDKg==}
engines: {node: '>=0.3.1'}
dir-glob@3.0.1: dir-glob@3.0.1:
resolution: {integrity: sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==} resolution: {integrity: sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==}
engines: {node: '>=8'} engines: {node: '>=8'}
@ -3064,6 +3071,8 @@ snapshots:
devtools-protocol@0.0.1147663: {} devtools-protocol@0.0.1147663: {}
diff@8.0.2: {}
dir-glob@3.0.1: dir-glob@3.0.1:
dependencies: dependencies:
path-type: 4.0.0 path-type: 4.0.0

View File

@ -4,6 +4,7 @@ import { ExtendedClient } from "../../interfaces/ExtendedClient";
import { getConfig } from "../../modules/data/getConfig"; import { getConfig } from "../../modules/data/getConfig";
import { customSubstring } from "../../utils/customSubstring"; import { customSubstring } from "../../utils/customSubstring";
import { errorHandler } from "../../utils/errorHandler"; import { errorHandler } from "../../utils/errorHandler";
import { generateDiff } from "../../modules/events/generateDiff";
/** /**
* Handles a message edit event. * Handles a message edit event.
@ -46,16 +47,15 @@ export const onMessageEdit = async (
return; return;
} }
const diffContent
= (oldMessage.content ?? newMessage.content) === null
? "This message appears to have no content."
: generateDiff(oldMessage.content ?? "", newMessage.content ?? "");
await logChannel.send({ await logChannel.send({
content: `${author?.tag} (${author?.id}) edited their message in in <#${ content: `${author?.tag} (${author?.id}) edited their message in in <#${
channel.id channel.id
}>:\n\n**Old Content:**\n\`${customSubstring( }>:\`\`\`diff\n${customSubstring(diffContent, 4000)}\n\`\`\``,
oldMessage.content,
1750
)}\`\n\n**New content:**\n\`${customSubstring(
newMessage.content,
1750
)}\`\n\n${newMessage.url}`,
allowedMentions: { parse: [] } allowedMentions: { parse: [] }
}); });
} catch (err) { } catch (err) {

View File

@ -0,0 +1,22 @@
import { diffSentences } from "diff";
/**
* Module to generate a diff string from two strings.
* @param old - The old string.
* @param updated - The new string.
* @returns The diff string, formatted.
*/
export const generateDiff = (old: string, updated: string): string => {
return diffSentences(old, updated).
map((element) => {
if (element.added) {
return `+ ${element.value}`;
}
if (element.removed) {
return `- ${element.value}`;
}
return "";
}).
filter(Boolean).
join("\n");
};