Skip to content

Commit

Permalink
馃攰 Add more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusOtter committed Mar 29, 2023
1 parent c42f31d commit 7610168
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/NeedleBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,20 @@ export default class NeedleBot {
});
}
}
}

private handleError(reason: unknown): void {
console.error(reason);
this.client.on("debug", msg => console.log(`[${this.getCurrentTime()}] DEBUG: ${msg}`));
this.client.on("shardError", (error, shardId) =>
console.log(`[${this.getCurrentTime()}] SHARD ERROR (id ${shardId}): ${error}`)
);
this.client.on("warn", msg => console.log(`[${this.getCurrentTime()}] WARN: ${msg}`));
}

private getCurrentTime = (): string => {
const now = new Date();
return now.toISOString().substring(11, 19);
};

private handleError = (reason: unknown): void => {
console.error(`[${this.getCurrentTime()}] ERROR:`, reason);
};
}
4 changes: 2 additions & 2 deletions src/ObjectFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export default class ObjectFactory {
return new CommandExecutorService();
}

public static createThreadCreationService(): ThreadCreationService {
return new ThreadCreationService(this.bot);
public static createThreadCreationService(logAmountOfCreatedThreads: boolean): ThreadCreationService {
return new ThreadCreationService(this.bot, logAmountOfCreatedThreads);
}

private static createDiscordClient(): Client {
Expand Down
2 changes: 1 addition & 1 deletion src/eventListeners/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class MessageCreateEventListener extends NeedleEventListener {

constructor(bot: NeedleBot) {
super(bot);
this.threadCreator = ObjectFactory.createThreadCreationService();
this.threadCreator = ObjectFactory.createThreadCreationService(true);
}

public async handle([message]: ClientEvents["messageCreate"]): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/eventListeners/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class ReadyEventListener extends NeedleEventListener {

constructor(bot: NeedleBot) {
super(bot);
this.threadCreator = ObjectFactory.createThreadCreationService();
this.threadCreator = ObjectFactory.createThreadCreationService(false);
}

public async handle([client]: ClientEvents["ready"]): Promise<void> {
Expand Down
34 changes: 32 additions & 2 deletions src/services/ThreadCreationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,26 @@ import ToggleOption from "../models/enums/ToggleOption.js";
import type MessageVariables from "../models/MessageVariables.js";
import type NeedleBot from "../NeedleBot.js";

export default class InformationService {
export default class ThreadCreationService {
private readonly bot: NeedleBot;
private readonly logIntervalMs = 60 * 1000; // 1 minute

constructor(bot: NeedleBot) {
private threadsCreatedCount = 0;
private lastLogTime = Date.now();

constructor(bot: NeedleBot, logAmountOfCreatedThreads: boolean) {
this.bot = bot;

if (logAmountOfCreatedThreads) {
this.scheduleLoggging();
}
}

private scheduleLoggging() {
setTimeout(() => {
this.logThreadsCreated();
this.scheduleLoggging();
}, this.logIntervalMs);
}

public async shouldHaveThread(message: Message): Promise<boolean> {
Expand Down Expand Up @@ -84,11 +99,13 @@ export default class InformationService {
}

const name = await this.getThreadName(message, channelConfig, messageVariables);
if (message.hasThread) return;
const thread = await message.startThread({
name,
rateLimitPerUser: channelConfig.slowmode === 0 ? undefined : channelConfig.slowmode,
autoArchiveDuration: message.channel.defaultAutoArchiveDuration ?? ThreadAutoArchiveDuration.OneDay,
});
this.threadsCreatedCount++;

messageVariables.setThread(thread);

Expand All @@ -114,6 +131,19 @@ export default class InformationService {
// Maybe we should check here if a system message was generated for the thread
}

private logThreadsCreated(): void {
const currentTime = Date.now();
const elapsedTime = (currentTime - this.lastLogTime) / 1000 / 60; // Convert to minutes
console.log(
`[${new Date().toISOString().substring(11, 19)}] Created ${
this.threadsCreatedCount
} threads in the last ${elapsedTime.toFixed(2)} minute(s).`
);

this.threadsCreatedCount = 0;
this.lastLogTime = currentTime;
}

private async getThreadName(
message: Message,
config: AutothreadChannelConfig,
Expand Down

0 comments on commit 7610168

Please sign in to comment.