Skip to content

Commit 771346f

Browse files
committed
replace default logger with pino
1 parent 345d7ac commit 771346f

39 files changed

+815
-1196
lines changed

.eslintrc.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ module.exports = {
44
env: {
55
node: true,
66
},
7-
extends: ["plugin:@typescript-eslint/recommended", "plugin:prettier/recommended", "prettier"],
8-
plugins: ["sort-imports-es6-autofix"],
7+
extends: ["eslint:recommended, plugin:@typescript-eslint/recommended", "plugin:prettier/recommended", "prettier"],
98
// add your custom rules here
109
rules: {
11-
"nuxt/no-cjs-in-config": "off",
10+
"@typescript-eslint/no-explicit-any": "off",
1211
"no-console": "off",
13-
"max-len": ["off", 80, 4],
1412
"no-prototype-builtins": "off",
15-
"require-await": "off",
13+
"nuxt/no-cjs-in-config": "off",
1614
"one-var": "off",
17-
"sort-imports-es6-autofix/sort-imports-es6": "warn",
18-
"@typescript-eslint/no-explicit-any": "off",
15+
"require-await": "off",
1916
},
2017
};

app/services/Data.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { logger } from "@/utils.js";
12
import { Container, Service } from "../Container.js";
23
import { Rule } from "./discord/index.js";
34
import { promises as fs } from "fs";
45
import path from "path";
56

7+
const log = logger(import.meta);
8+
69
export class Data extends Service {
710
name = "Data";
811
private dataPath = path.join(process.cwd(), "data");
@@ -71,7 +74,7 @@ export class Data extends Service {
7174
} catch (err) {
7275
data = {};
7376
}
74-
console.log(`Loaded ${filePath} with`, data);
77+
log.info({ file, data });
7578
this[path.basename(filePath, ".json")] = data;
7679
}
7780
}

app/services/DiscordMetadata.ts

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Bans, DiscordBot, SQL } from "./index.js";
22
import { Container, Service } from "../Container.js";
33
import { DiscordErrorData, OAuthErrorData } from "discord.js";
4-
import { isAdmin } from "@/utils.js";
4+
import { isAdmin, logger } from "@/utils.js";
55
import { revokeOAuthToken } from "./webapp/api/discord-oauth.js";
66
import SteamID from "steamid";
77
import axios, { AxiosError } from "axios";
88
import config from "@/config/metadata.json" with { type: "json" };
99

10+
const log = logger(import.meta);
11+
1012
export type MetaMetadata = {
1113
banned?: 1 | 0;
1214
dev?: 1 | 0;
@@ -102,17 +104,12 @@ export class DiscordMetadata extends Service {
102104
// invalid, expired, revoked, doesn't match redirection
103105
// URI, or was issued to another client.
104106
const res = await revokeOAuthToken(data.access_token);
105-
console.warn(
106-
`[Metadata] InValID_GraNT revoking token (${res})! ${userId} [${
107-
err.code
108-
}] ${JSON.stringify(discordResponse)}`
107+
log.warn(
108+
discordResponse,
109+
`InValID_GraNT revoking token (${res})! ${userId} [${err.code}]`
109110
);
110111
} else {
111-
console.error(
112-
`[Metadata] failed fetching tokens: [${err.code}] ${JSON.stringify(
113-
discordResponse
114-
)}`
115-
);
112+
log.error(discordResponse, `failed fetching tokens: [${err.code}]}`);
116113
}
117114
});
118115

@@ -130,11 +127,7 @@ export class DiscordMetadata extends Service {
130127
);
131128
return token.access_token;
132129
}
133-
console.error(
134-
`[Metadata] failed to get access token for ${userId} data: ${JSON.stringify(
135-
data
136-
)} res: ${JSON.stringify(res)}`
137-
);
130+
log.error({ data, res }, "failed to get access token");
138131
}
139132

140133
return data.access_token;
@@ -159,11 +152,7 @@ export class DiscordMetadata extends Service {
159152
},
160153
})
161154
.catch((err: AxiosError<DiscordErrorData>) => {
162-
console.error(
163-
`[Metadata] failed getting discord metadata: [${err.code}] ${JSON.stringify(
164-
err.response?.data
165-
)}`
166-
);
155+
log.error(err, "failed to get metadata");
167156
});
168157
if (res) {
169158
this.ARCOCache[userId] = res.data;
@@ -238,9 +227,7 @@ export class DiscordMetadata extends Service {
238227
const body = { platform_name: "Metastruct", platform_username: userName, metadata };
239228

240229
if (!accessToken) {
241-
console.error(
242-
`[Metadata] failed pushing discord metadata invalid Accesstoken?: ${userName}(${userId})`
243-
);
230+
log.error({ userId, userName }, "accesstoken missing?");
244231
return false;
245232
}
246233

@@ -253,13 +240,10 @@ export class DiscordMetadata extends Service {
253240
.catch((err: AxiosError<DiscordErrorData>) => {
254241
if (err.response?.status === 401) {
255242
// unauthorised, user probably revoked the token.
243+
log.info({ err, accessToken }, "unauthorized removing token");
256244
revokeOAuthToken(accessToken, true);
257245
} else {
258-
console.error(
259-
`[Metadata] failed pushing discord metadata: [${err.code}] ${JSON.stringify(
260-
err.response?.data
261-
)}`
262-
);
246+
log.error(err, "metadata push failed.");
263247
}
264248
return false;
265249
});

app/services/IRC.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import * as Discord from "discord.js";
22
import { Container, Service } from "../Container.js";
33
import config from "@/config/irc.json" with { type: "json" };
44
import nIRC from "irc-upd";
5+
import { logger } from "@/utils.js";
6+
7+
const log = logger(import.meta);
58

69
type message = {
710
prefix: string;
@@ -45,7 +48,7 @@ export class IRC extends Service {
4548
avatarURL: `https://robohash.org/${nick}.png`,
4649
allowedMentions: { parse: ["users", "roles"] },
4750
})
48-
.catch(console.error);
51+
.catch(log.error);
4952
}
5053

5154
private relayIRC(text: string): void {
@@ -88,7 +91,7 @@ export class IRC extends Service {
8891
}
8992
);
9093

91-
this.client.on("error", msg => console.error(msg));
94+
this.client.on("error", err => log.error(err));
9295
}
9396
}
9497

app/services/Markov.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// thank u mr swadical https://github.com/SwadicalRag/node-markov-lite
2+
import { logger } from "@/utils.js";
23
import { Container, Service } from "../Container.js";
34
import sqlite3 from "sqlite3";
45

6+
const log = logger(import.meta);
7+
58
export interface IGenerateOptions {
69
depth?: number;
710
length?: number;
@@ -229,7 +232,7 @@ export class Markov extends Service {
229232
options?.continuation
230233
);
231234
} catch (err) {
232-
console.error(err);
235+
log.error(err);
233236
return;
234237
}
235238
}

app/services/Resonite.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { Container, Service } from "../Container.js";
22
import { createHash, randomUUID } from "crypto";
33
import axios from "axios";
44
import config from "@/config/resonite.json" with { type: "json" };
5+
import { logger } from "@/utils.js";
6+
7+
const log = logger(import.meta);
58

69
export type ResoniteSignalLSessionResponse = {
710
entity: {
@@ -152,10 +155,10 @@ export class Resonite extends Service {
152155
res.data.entity.created
153156
).getTime();
154157
await data.save();
155-
console.log("Retrieved and saved Resonite Token!");
158+
log.info("Retrieved and saved Resonite Token!");
156159
}
157160
} catch (error) {
158-
console.error(error);
161+
log.error(error);
159162
}
160163
}
161164
this.ResoniteToken = lastToken;

app/services/Starboard.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { Container, Service } from "../Container.js";
33
import { DiscordBot, SQL } from "./index.js";
44
import config from "@/config/starboard.json" with { type: "json" };
55
import discordConfig from "@/config/discord.json" with { type: "json" };
6+
import { logger } from "@/utils.js";
7+
8+
const log = logger(import.meta);
69

710
const STARBOARD_CONFIG = {
811
MESSAGE_AGE_LIMIT_MS: 3 * 28 * 24 * 60 * 60 * 1000, // 3 months
@@ -40,16 +43,16 @@ export class Starboard extends Service {
4043
if (originalAuthorID && originalAuthorID !== interaction.user.id) return;
4144

4245
try {
43-
const res = await interaction.message.delete().catch(console.error);
46+
const res = await interaction.message.delete().catch(log.error);
4447
if (res) {
4548
this.bot
4649
.getTextChannel(this.bot.config.channels.log)
4750
?.send(
4851
`Highlighted Message in ${interaction.channel} deleted by ${interaction.user} (${interaction.user.id}) -> https://discord.com/channels/${interaction.guildId}/${originalChannelID}/${originalMsgID}`
4952
);
5053
}
51-
} catch (error) {
52-
console.error("[Starboard] Error deleting message:", error);
54+
} catch (err) {
55+
log.error(err, "failed to delete message");
5356
}
5457
});
5558
}
@@ -136,7 +139,7 @@ export class Starboard extends Service {
136139
this.isBusy = true;
137140
const msg = await message.fetch();
138141
if (!msg) {
139-
console.error("[Starboard] couldn't fetch message", reaction);
142+
log.error(reaction, "fetch message failed.");
140143
this.isBusy = false;
141144
return;
142145
}
@@ -145,7 +148,7 @@ export class Starboard extends Service {
145148
targetChannel = client.channels.cache.get(discordConfig.channels.hBot);
146149

147150
if (!targetChannel) {
148-
console.error("[Starboard] wtf invalid channel", reaction);
151+
log.error(reaction, "invalid channel?");
149152
this.isBusy = false;
150153
return;
151154
}
@@ -235,8 +238,8 @@ export class Starboard extends Service {
235238

236239
this.isBusy = false;
237240
}
238-
} catch (error) {
239-
console.error("[Starboard] Error handling reaction:", error);
241+
} catch (err) {
242+
log.error(err);
240243
} finally {
241244
this.isBusy = false;
242245
}

app/services/discord/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as Discord from "discord.js";
22
import { Container, Service } from "@/app/Container.js";
33
import { Data, GameBridge } from "@/app/services/index.js";
4-
import { getAsBase64 } from "@/utils.js";
4+
import { getAsBase64, logger } from "@/utils.js";
55
import { getEventIcon } from "./modules/discord-guild-icon.js";
66
import DiscordConfig from "@/config/discord.json" with { type: "json" };
77
import modules from "./modules/index.js";
88
import motdConfig from "@/config/motd.json" with { type: "json" };
99

10+
const log = logger("DiscordBot");
11+
1012
export type Rule = {
1113
title: string;
1214
description?: string;
@@ -58,14 +60,14 @@ export class DiscordBot extends Service {
5860

5961
this.discord.on("clientReady", async client => {
6062
this.ready = true;
61-
console.log(`'${client.user.username}' Discord Bot has logged in`);
63+
log.info(`'${client.user.username}' Discord Bot has logged in`);
6264
});
6365

6466
this.discord.on("shardDisconnect", () => {
6567
this.ready = false;
6668
});
6769

68-
this.discord.on("warn", console.log);
70+
this.discord.on("warn", log.warn);
6971

7072
for (const loadModule of modules) {
7173
loadModule(this);

app/services/discord/modules/commands/RemoveHighlightMessage.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as Discord from "discord.js";
22
import { EphemeralResponse, MenuCommand } from "@/extensions/discord.js";
33
import discordConfig from "@/config/discord.json" with { type: "json" };
4+
import { logger } from "@/utils.js";
5+
6+
const log = logger(import.meta);
47

58
export const MenuRemoveHighlightMessageCommand: MenuCommand = {
69
options: {
@@ -52,7 +55,7 @@ export const MenuRemoveHighlightMessageCommand: MenuCommand = {
5255
m.attachments.size > 0 &&
5356
m.attachments.first()?.name === ctx.targetMessage.attachments.first()?.name
5457
);
55-
const deleted = await targetMessage?.delete().catch(console.error);
58+
const deleted = await targetMessage?.delete().catch(log.error);
5659

5760
if (deleted) {
5861
await ctx.reply(EphemeralResponse("👍"));
@@ -66,8 +69,8 @@ export const MenuRemoveHighlightMessageCommand: MenuCommand = {
6669
)
6770
);
6871
}
69-
} catch (error) {
70-
console.error("[RemoveHighlightMessage]", error);
72+
} catch (err) {
73+
log.error(err);
7174
await ctx.reply(
7275
EphemeralResponse(
7376
"something went wrong with deleting your message :( ping @techbot if you want it removed"

app/services/discord/modules/commands/Role.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import * as Discord from "discord.js";
22
import { EphemeralResponse, SlashCommand } from "@/extensions/discord.js";
33
import DiscordConfig from "@/config/discord.json" with { type: "json" };
44
import axios from "axios";
5+
import { logger } from "@/utils.js";
6+
7+
const log = logger(import.meta);
58

69
const ROLE_IDENTIFIER = DiscordConfig.bot.roleIdentifier;
710
const IMG_TYPES = ["image/png", "image/gif", "image/jpeg"];
@@ -81,8 +84,8 @@ const setRoleColorSpecial = async (
8184
`👍\nhere is your old role color if you want to change back: Primary: \`${role.hexColor}\`${role.colors.secondaryColor ? ` Secondary: ${`#${role.colors.secondaryColor.toString(16).padStart(6, "0")}`}` : ""}${role.colors.tertiaryColor ? ` Tertiary: ${`#${role.colors.tertiaryColor.toString(16).padStart(6, "0")}`}` : ""}`
8285
)
8386
);
84-
} catch (error) {
85-
console.error(error);
87+
} catch (err) {
88+
log.error(err);
8689
await ctx.followUp(EphemeralResponse("Something went wrong trying to add the gradient :("));
8790
}
8891
};
@@ -249,8 +252,8 @@ const setRole = async (ctx: Discord.ChatInputCommandInteraction): Promise<any> =
249252
{ primaryColor: roleColor },
250253
"Updated role color via command"
251254
);
252-
} catch (ex) {
253-
console.error(ex);
255+
} catch (err) {
256+
log.error(err);
254257
await ctx.followUp(
255258
EphemeralResponse("Something went wrong changing the color of the role :(")
256259
);
@@ -400,7 +403,7 @@ export const SlashRoleCommand: SlashCommand = {
400403
break;
401404
}
402405
} catch (err) {
403-
console.error(err);
406+
log.error(err);
404407
await ctx.followUp(
405408
EphemeralResponse(`Something went wrong adding your role :(\n` + err)
406409
);

0 commit comments

Comments
 (0)