Almost finished logs

pull/104/head
pineafan 2 years ago
parent c3195b5c3f
commit 67c9f1f4bb

@ -1 +0,0 @@
/nix/store/fmvzckvxnqrfbgv0mvzghgi87602pgcm-nucleus-1.1.0

@ -61,7 +61,7 @@ export default {
stats: {}, stats: {},
logging: { logging: {
logs: { logs: {
enabled: false, enabled: true,
channel: null, channel: null,
toLog: "3fffff" toLog: "3fffff"
}, },

@ -46,13 +46,16 @@ async function interactionCreate(interaction: Interaction) {
: false; : false;
return await modifySuggestion(interaction, value); return await modifySuggestion(interaction, value);
} }
if (interaction.customId.startsWith("log:edit:")) { if (interaction.customId === "log:edit") {
const messageId = interaction.customId.split(":")[2]; const attachment = interaction.message.embeds[0]?.image;
const message = await interaction.channel?.messages.fetch(messageId!); console.log(attachment)
const attachment = message?.attachments.find((a) => a.name === "log.json");
if (!attachment) return; if (!attachment) return;
const log = JSON.parse(Buffer.from(await (await fetch(attachment.url)).text(), 'base64').toString('binary')); const attachmentData = await (await fetch(attachment.url)).text()
console.log(log); console.log(attachmentData)
const decoded = atob(attachmentData);
console.log("decoded", decoded)
const json = JSON.parse(decoded);
console.log("json", json)
} }
switch (interaction.customId) { switch (interaction.customId) {
case "rolemenu": { case "rolemenu": {

@ -18,8 +18,7 @@ export async function callback(client: NucleusClient, oldMessage: Message, newMe
renderUser, renderUser,
renderDelta, renderDelta,
renderNumberDelta, renderNumberDelta,
renderChannel, renderChannel
preLog
} = client.logger; } = client.logger;
const replyTo: MessageReference | null = newMessage.reference; const replyTo: MessageReference | null = newMessage.reference;
const newContent = newMessage.cleanContent.replaceAll("`", ""); const newContent = newMessage.cleanContent.replaceAll("`", "");
@ -73,10 +72,14 @@ export async function callback(client: NucleusClient, oldMessage: Message, newMe
return; return;
} }
const differences = diff.diffChars(oldContent, newContent); const differences = diff.diffChars(oldContent, newContent);
const charsAdded = differences.filter((x) => x.added).length; const charsAdded = (differences.filter((d) => d.added).map((d) => d.count)).reduce((a, b) => a! + b!, 0)!;
const charsRemoved = differences.filter((x) => x.removed).length; const charsRemoved = (differences.filter((d) => d.removed).map((d) => d.count)).reduce((a, b) => a! + b!, 0)!;
const preLogMessage = await preLog(newMessage.guild.id, JSON.stringify(differences, null, 2)); const imageData = JSON.stringify({data: differences, extra: "The image in this embed contains data about the below log.\n" +
if (!preLogMessage) return; "It isn't designed to be read by humans, but you can decode " +
"it with any base64 decoder, and then read it as JSON.\n" +
"We use base 64 to get around people using virus tests and the file being blocked, and an image to have the embed hidden (files can't be suppressed)\n" +
"If you've got to this point and are reading this hidden message, you should come and work with us " +
"at https://discord.gg/w35pXdrxKW (Internal development server) and let us know how you got here."}, null, 2)
const data = { const data = {
meta: { meta: {
type: "messageUpdate", type: "messageUpdate",
@ -85,10 +88,11 @@ export async function callback(client: NucleusClient, oldMessage: Message, newMe
color: NucleusColors.yellow, color: NucleusColors.yellow,
emoji: "MESSAGE.EDIT", emoji: "MESSAGE.EDIT",
timestamp: newMessage.editedTimestamp, timestamp: newMessage.editedTimestamp,
changes: { messageId: `${preLogMessage.id}`, buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:edit:${preLogMessage.id}` } buttons: [{ buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:edit` }],
imageData: imageData
}, },
separate: { separate: {
start: `${charsAdded} ${addPlural(charsAdded, "character")} added, ${charsRemoved} ${addPlural(charsRemoved, "character")} removed`, start: `${addPlural(charsAdded, "character")} added, ${addPlural(charsRemoved, "character")} removed`,
end: `[[Jump to message]](${newMessage.url})` end: `[[Jump to message]](${newMessage.url})`
}, },
list: { list: {

@ -4,7 +4,6 @@ import { promisify } from "util";
import generateKeyValueList from "./generateKeyValueList.js"; import generateKeyValueList from "./generateKeyValueList.js";
import client from "./client.js"; import client from "./client.js";
import { DiscordAPIError } from "discord.js"; import { DiscordAPIError } from "discord.js";
import { Stream } from "node:stream";
import EmojiEmbed from "./generateEmojiEmbed.js"; import EmojiEmbed from "./generateEmojiEmbed.js";
const wait = promisify(setTimeout); const wait = promisify(setTimeout);
@ -17,15 +16,8 @@ export interface LoggerOptions {
color: number; color: number;
emoji: string; emoji: string;
timestamp: number; timestamp: number;
files?: ( buttons?: { buttonText: string, buttonId: string, buttonStyle: Discord.ButtonStyle }[];
| Discord.BufferResolvable imageData?: string;
| Stream
| Discord.JSONEncodable<Discord.APIAttachment>
| Discord.Attachment
| Discord.AttachmentBuilder
| Discord.AttachmentPayload
)[];
showDetails?: boolean;
}; };
list: Record<string | symbol | number, unknown>; list: Record<string | symbol | number, unknown>;
hidden: { hidden: {
@ -47,6 +39,13 @@ async function isLogging(guild: string, type: string): Promise<boolean> {
return true; return true;
} }
const NucleusColors = {
red: 0xf27878,
yellow: 0xf2d478,
green: 0x68d49e,
blue: 0x72aef5,
};
export const Logger = { export const Logger = {
renderUser(user: Discord.User | string) { renderUser(user: Discord.User | string) {
if (typeof user === "string") user = client.users.cache.get(user)!; if (typeof user === "string") user = client.users.cache.get(user)!;
@ -86,11 +85,7 @@ export const Logger = {
renderEmoji(emoji: Discord.GuildEmoji) { renderEmoji(emoji: Discord.GuildEmoji) {
return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`; return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
}, },
NucleusColors: { NucleusColors,
red: 0xf27878,
yellow: 0xf2d478,
green: 0x68d49e
},
async getAuditLog( async getAuditLog(
guild: Discord.Guild, guild: Discord.Guild,
event: Discord.GuildAuditLogsResolvable, event: Discord.GuildAuditLogsResolvable,
@ -106,22 +101,7 @@ export const Logger = {
throw e; 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> { async log(log: LoggerOptions): Promise<void> {
if (!(await isLogging(log.hidden.guild, log.meta.calculateType))) return; if (!(await isLogging(log.hidden.guild, log.meta.calculateType))) return;
const config = await client.database.guilds.read(log.hidden.guild); const config = await client.database.guilds.read(log.hidden.guild);
@ -139,6 +119,7 @@ export const Logger = {
description[key] = value; description[key] = value;
} }
}); });
console.log("imageData", log.meta.imageData)
if (channel) { if (channel) {
log.separate = log.separate ?? {}; log.separate = log.separate ?? {};
const messageOptions: Parameters<Discord.TextChannel["send"]>[0] = {}; const messageOptions: Parameters<Discord.TextChannel["send"]>[0] = {};
@ -154,17 +135,29 @@ export const Logger = {
) )
.setTimestamp(log.meta.timestamp) .setTimestamp(log.meta.timestamp)
.setColor(log.meta.color) .setColor(log.meta.color)
.setImage(log.meta.imageData ? "attachment://extra_log_data.json.base64" : null)
]; ];
if (log.meta.files) messageOptions.files = log.meta.files; if (log.meta.buttons) {
if (log.meta.showDetails) { const buttons = []
components.addComponents( for (const button of log.meta.buttons) {
new Discord.ButtonBuilder() buttons.push(
.setCustomId("log:showDetails") new Discord.ButtonBuilder()
.setLabel("Show Details") .setCustomId(button.buttonId)
.setStyle(Discord.ButtonStyle.Primary) .setLabel(button.buttonText)
); .setStyle(button.buttonStyle)
)
}
components.addComponents(buttons);
messageOptions.components = [components]; messageOptions.components = [components];
} }
if (log.meta.imageData) {
messageOptions.files = [
{
attachment: Buffer.from(btoa(log.meta.imageData), "utf-8"), // Use base 64 to prevent virus scanning (EICAR)Buffer.from(log.meta.imageData, "base64"),
name: "extra_log_data.json.base64"
}
];
}
await channel.send(messageOptions); await channel.send(messageOptions);
} }
} }

Loading…
Cancel
Save