Skip to content

Commit

Permalink
overhaul/fix logger sorter, better startup text
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Mar 16, 2024
1 parent 7e50c3a commit 3b176f8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 32 deletions.
36 changes: 19 additions & 17 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ export type LogAction =
| "slashCommand"
| "button"
| "selectMenu"
| "modal"
| "modalSubmit"
| "guildMemberUpdate"
| "guildJoined";

export type LogData = Message | Guild | AnyInteraction;

export type Log = {
type: LogAction;
data: any; // technically LogData but TS breaks without this
data: any; // technically LogData but TS union type validation is painful
};

export interface FaithfulGuild {
Expand All @@ -84,9 +84,8 @@ export class ExtendedClient<Ready extends boolean = boolean> extends Client<Read
public readonly tokens: Tokens;
public readonly automation = new Automation(this);

private readonly _logs: Log[] = [];
private maxLogs = 50;
private lastLogIndex = 0;
public readonly logs: Log[] = [];
private readonly maxLogs = 50;

public readonly menus = new Collection<string, Component<StringSelectMenuInteraction>>();
public readonly buttons = new Collection<string, Component<ButtonInteraction>>();
Expand Down Expand Up @@ -120,8 +119,8 @@ export class ExtendedClient<Ready extends boolean = boolean> extends Client<Read
.then(() => {
this.loadSlashCommands();

this.loadComponents();
this.loadEvents();
this.loadComponents();
this.loadCollections();

this.automation.start();
Expand Down Expand Up @@ -163,19 +162,23 @@ export class ExtendedClient<Ready extends boolean = boolean> extends Client<Read
console.log(darkColor` 888 ` + chalk.gray.italic(this.tokens.maintenance === false ? " ~ Made lovingly with pain\n" : " Maintenance mode!\n"));
}

/**
* Convenience method to load all emitting collections at once
* @author Juknum
*/
private loadCollections() {
if (this.verbose) console.log(`${info}Loading collection data...`);
this.loadCollection(this.polls, paths.polls, paths.json);
this.loadCollection(this.commandsProcessed, paths.commandsProcessed, paths.json);
if (this.verbose) console.log(`${info}Loaded collection data`);
return this;
}

/**
* Read and load data from a JSON file into emitting collection with events
* @author Nick, Juknum
* @param collection collection to load into
* @param filename
* @param relativePath
* @param filename file to load data from
* @param relativePath path to load data from
*/
private loadCollection<V>(
collection: EmittingCollection<string, V>,
Expand Down Expand Up @@ -211,7 +214,7 @@ export class ExtendedClient<Ready extends boolean = boolean> extends Client<Read
}

/**
* Remove slash commands from the menu
* Remove slash commands
* @author Nick
*/
public async deleteGlobalSlashCommands() {
Expand Down Expand Up @@ -312,6 +315,7 @@ export class ExtendedClient<Ready extends boolean = boolean> extends Client<Read
this.user.setStatus("idle");
});

if (this.verbose) console.log(`${info}Loading event handlers...`);
const events = walkSync(paths.events).filter((file) => file.endsWith(".ts"));
for (const file of events) {
const event: Event = require(file).default;
Expand All @@ -325,8 +329,8 @@ export class ExtendedClient<Ready extends boolean = boolean> extends Client<Read
* @author Evorp
*/
private loadComponents() {
if (this.verbose) console.log(`${info}Loading Discord components...`);
for (const [key, path] of Object.entries(paths.components)) this.loadComponent(this[key], path);
if (this.verbose) console.log(`${info}Loaded Discord components`);
}

/**
Expand All @@ -350,11 +354,9 @@ export class ExtendedClient<Ready extends boolean = boolean> extends Client<Read
* @param type
* @param data
*/
public storeAction(type: LogAction, data: LogData): void {
this.logs[this.lastLogIndex++ % this.maxLogs] = { type, data };
}

public get logs() {
return this._logs;
public storeAction(type: LogAction, data: LogData) {
// remove from start (oldest messages) on overflow
if (this.logs.length > this.maxLogs) this.logs.shift();
this.logs.push({ type, data });
}
}
3 changes: 1 addition & 2 deletions src/commands/faithful/discords.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { SlashCommand } from "@interfaces/interactions";
import { SlashCommandBuilder } from "discord.js";
import { EmbedBuilder, Message } from "@client";
import { Message } from "@client";
import axios from "axios";
import { colors } from "@utility/colors";
import { FaithfulGuild } from "client/client";
import { toTitleCase } from "@utility/methods";

Expand Down
3 changes: 1 addition & 2 deletions src/events/modalSubmit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Event } from "@interfaces/events";
import { Client, ModalSubmitInteraction } from "@client";

export default {
name: "modalSubmit",
async execute(client, interaction) {
client.storeAction("modal", interaction);
client.storeAction("modalSubmit", interaction);

const modal = client.modals.get(interaction.customId);
if (modal) return modal.execute(client, interaction);
Expand Down
1 change: 0 additions & 1 deletion src/events/selectMenuUsed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Event } from "@interfaces/events";
import { Client, StringSelectMenuInteraction } from "@client";

export default {
name: "selectMenuUsed",
Expand Down
14 changes: 5 additions & 9 deletions src/helpers/functions/handleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DiscordAPIError, AttachmentBuilder } from "discord.js";
import { Client } from "@client";
import { readFileSync } from "fs";
import { devLogger, err } from "@helpers/logger";
import { Log } from "client/client";
import { join } from "path";
import { choice } from "@utility/methods";
import { error as randomSentences } from "@json/quotes.json";
Expand All @@ -20,7 +19,7 @@ const loopLimit = 3; // how many times the same error needs to be made to trigge
*/
export const constructLogFile = (
client: Client,
reason: any = { stack: "You requested it with /logs ¯\\_(ツ)_/¯" },
reason: any = { stack: "Requested with /logs ¯\\_(ツ)_/¯" },
): AttachmentBuilder => {
const logTemplate = readFileSync(join(__dirname, "logTemplate.log"), {
encoding: "utf-8",
Expand All @@ -36,11 +35,12 @@ export const constructLogFile = (
.replace("%randomSentence%", sentence)
.replace("%randomSentenceUnderline%", "-".repeat(sentence.length))
.split("%templateStart%")[0] +
client.logs.reverse().reduce(
(acc, log: Log, index) =>
// reduceRight does reduce and reverse at once
client.logs.reduceRight(
(acc, log, index) =>
acc +
template
.replace("%templateIndex%", `${client.logs.length - index}`)
.replace("%templateIndex%", String(index))
.replace("%templateType%", formatLogType(log))
.replace(
"%templateCreatedTimestamp%",
Expand All @@ -53,10 +53,6 @@ export const constructLogFile = (
})} (UTC)`,
)
.replace("%templateURL%", formatLogURL(log.data))
.replace(
"%templateChannelType%",
log.data.channel ? log.data.channel.type : "Not relevant",
)
.replace("%templateContent%", formatLogContent(log))
.replace(
"%templateEmbeds%",
Expand Down
1 change: 0 additions & 1 deletion src/helpers/functions/logTemplate.log
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Actions:
TYPE : %templateType%
TIMESTAMP : %templateCreatedTimestamp%
URL : %templateURL%
CHANNEL_TYPE : %templateChannelType%
CONTENT : %templateContent%
EMBEDS : %templateEmbeds%
COMPONENTS : %templateComponents%
Expand Down

0 comments on commit 3b176f8

Please sign in to comment.