Skip to content

Commit

Permalink
logging commands
Browse files Browse the repository at this point in the history
  • Loading branch information
clementvt committed Nov 14, 2024
1 parent 9ba8aca commit 535cd0b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 31 deletions.
5 changes: 5 additions & 0 deletions bot/src/events/interaction/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import WelcomerClient from "../../structure/WelcomerClient";
import { EventType } from "../../types";
import { sendErrorMessage, sendInteractionMessage } from "../../utils/messages";
import { log } from "../../utils/logger";

export default class InteractionCreateEvent implements EventType {
name = "interactionCreate";
Expand All @@ -33,6 +34,10 @@ export default class InteractionCreateEvent implements EventType {
if (command?.noDefer) return command.execute(interaction, client);
await interaction.deferReply({ ephemeral: command?.ephemeral });
await command.execute(interaction, client);
log(
`Interaction command called: /${interaction.commandName}`,
interaction
);
} catch (error) {
console.error("There was an error on interactionCreate: ", error);
await sendErrorMessage(
Expand Down
10 changes: 10 additions & 0 deletions bot/src/events/shard/shardDeath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EventType } from "../../types";
import { logStatus } from "../../utils/logger";

export default class ShardDisconnect implements EventType {
name = "shardDisconnect";
once = true;
async execute(shardId: number) {
logStatus({ shardId, status: "ready" });
}
}
10 changes: 10 additions & 0 deletions bot/src/events/shard/shardError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EventType } from "../../types";
import { logStatus } from "../../utils/logger";

export default class ShardError implements EventType {
name = "shardError";
once = true;
async execute(error: Error, shardId: number) {
console.log(`Shard ${shardId} has encountered an error`, error);
}
}
11 changes: 11 additions & 0 deletions bot/src/events/shard/shardReady.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EventType } from "../../types";
import { logStatus } from "../../utils/logger";

export default class ShardReady implements EventType {
name = "shardReady";
once = true;
async execute(closeEvent: CloseEvent, shardId: number) {
console.log(`Shard ${shardId} is as disconnected`, closeEvent);
logStatus({ shardId, status: "death" });
}
}
3 changes: 0 additions & 3 deletions bot/src/structure/WelcomerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ export default class WelcomerClient
},
});
this.init();
this.on("shardReady", (id) => {
console.log(`Shard ${id} is ready`);
});
this.images.set(
"banner",
new AttachmentBuilder("banner.png").setFile("assets/banner.png")
Expand Down
45 changes: 29 additions & 16 deletions bot/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const error = (
}
};

export const log = (message: Error, interaction: CommandInteraction) => {
export const log = (message: string, interaction: CommandInteraction) => {
try {
const embed = new EmbedBuilder()
.setTitle(`Log: ${message}`)
Expand All @@ -98,11 +98,11 @@ export const log = (message: Error, interaction: CommandInteraction) => {
embed.addFields(
{
name: "From",
value: interaction.guild?.name || "Unknown Guild",
value: `${interaction.guild?.name || "Unknown Guild"} (${interaction.guild?.id})`,
},
{
name: "Triggered by",
value: interaction.user.tag,
value: `${interaction.user.tag} (${interaction.user.id})`,
}
);
}
Expand Down Expand Up @@ -141,20 +141,22 @@ export const removedGuildMessage = (embed: Embed | APIEmbed) => {
}
};
export const logStatus = ({
cluster,
shard,
clusterId,
shardId,
status,
}: {
cluster: number;
shard: number | string;
clusterId?: number;
shardId: number | string;
status: string;
}) => {
}) => {
if (process.env.NODE_ENV === "development") return;
if (status === "starting") {
statusHook.send({
embeds: [
{
title: `Welcomer is Starting -- Cluster ${cluster}, Shard ${shard} is starting`,
title: `Welcomer is Starting -- ${
clusterId ? `clusterId ${clusterId}` : ""
}, shardId ${shardId} is starting`,
timestamp: new Date(),
},
],
Expand All @@ -164,7 +166,9 @@ export const logStatus = ({
statusHook.send({
embeds: [
{
title: `Welcomer is Online -- Cluster ${cluster}, Shard ${shard} is online`,
title: `Welcomer is Online -- ${
clusterId ? `clusterId ${clusterId}` : ""
}, shardId ${shardId} is online`,
color: 65280,
timestamp: new Date(),
},
Expand All @@ -175,7 +179,9 @@ export const logStatus = ({
statusHook.send({
embeds: [
{
title: `Welcomer is partially offline -- Cluster ${cluster}, Shard ${shard} is offline`,
title: `Welcomer is partially offline -- ${
clusterId ? `clusterId ${clusterId}` : ""
}, shardId ${shardId} is offline`,
color: "16711680",
timestamp: new Date(),
},
Expand All @@ -186,7 +192,9 @@ export const logStatus = ({
statusHook.send({
embeds: [
{
title: `Welcomer is partially offline -- Clusyer ${cluster}, Shard ${shard} is reconnecting`,
title: `Welcomer is partially offline -- ${
clusterId ? `clusterId ${clusterId}` : ""
}, shardId ${shardId} is reconnecting`,
color: "16737024",
timestamp: new Date(),
},
Expand All @@ -197,13 +205,18 @@ export const logStatus = ({
statusHook.send({
embeds: [
{
title: `Welcomer is Online -- Cluster ${cluster}, Shard ${shard} has resumed`,
title: `Welcomer is Online -- ${
clusterId ? `clusterId ${clusterId}` : ""
}, shardId ${shardId} has resumed`,
color: "65280",
timestamp: new Date(),
},
],
});
}
console.log(`Status: ${status} on cluster ${cluster}, shard ${shard}`);

}
console.log(
`Status: ${status} on ${
clusterId ? `clusterId ${clusterId}` : ""
}, shardId ${shardId}`
);
};
26 changes: 14 additions & 12 deletions bot/src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@ export const sendChannelMessage = async (channel: TextBasedChannel, message: Mes
}

export const sendErrorMessage = async (interaction: Exclude<Interaction, AutocompleteInteraction>, error: string) => {
(await interaction.fetchReply()).removeAttachments();
await sendInteractionMessage(
interaction,
{
content: error,
ephemeral: true,
embeds: [],
components: [new ActionRowBuilder<ButtonBuilder>().addComponents(helpButton)],
files: [],
},
true
);
try {
(await interaction.fetchReply()).removeAttachments();
await sendInteractionMessage(
interaction,
{
content: error,
ephemeral: true,
embeds: [],
components: [new ActionRowBuilder<ButtonBuilder>().addComponents(helpButton)],
files: [],
},
true
);
}catch{}
}

export const sendTempMessage = async (interaction: Exclude<Interaction, AutocompleteInteraction>, message: InteractionReplyOptions = baseMessage, follow: boolean=false, time: number = 5000) => {
Expand Down

0 comments on commit 535cd0b

Please sign in to comment.