A lightweight logger for Discord.js events – now with a new generic API for flexible event registration!
Version 5 of discordjs-logger introduces a simplified and flexible API. Rather than having separate methods for every event (as in v4), you now register events via a generic interface. You can:
-
Register a single event with a custom handler using
on()
. -
Bulk-register events with default logging or custom handlers via
registerEvents()
. -
Log all available events from Discord.js with a single call using
logAllEvents()
.
This approach reduces boilerplate and makes your code easier to maintain.
To install discordjs-logger v5, update your package.json to use version 5:
npm i discordjs-logger@^5.0.0
yarn add discordjs-logger@^5.0.0
Note: If you’re still on v4, your package-lock or yarn.lock will prevent automatic upgrade. Please refer to the Migration Guide below.
Below are some examples demonstrating the new API.
import { Client, GatewayIntentBits, Events } from "discord.js";
import DiscordEventHandler from "discordjs-logger";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
// Add other intents as needed...
],
});
const eventHandler = new DiscordEventHandler(client);
// Register a few events with the default handler.
// The default handler logs the event name and its arguments.
eventHandler.registerEvents([
Events.MessageCreate,
Events.GuildCreate,
Events.ClientReady, // equivalent to 'ready'
]);
// Alternatively, register specific events with custom handlers:
eventHandler.registerEvents([
[
Events.MessageCreate,
(message) => {
console.log("Custom handler for MessageCreate:", message.content);
},
],
]);
// To log all available events from Discord.js:
eventHandler.logAllEvents();
client.login("YOUR_DISCORD_APP_TOKEN");
Registers a specific event with your custom handler.
Example:
eventHandler.on(Events.GuildCreate, (guild) => {
console.log("Guild created:", guild.name);
});
Overloaded to support two usages:
-
Default handler:
Pass an array of events (from the Discord.jsEvents
enum) to register them with a default logger.eventHandler.registerEvents([Events.MessageCreate, Events.GuildCreate]);
-
Custom handlers:
Pass an array of tuples[event, handler]
to register each event with a custom handler.eventHandler.registerEvents([ [ Events.MessageCreate, (message) => { console.log("Custom MessageCreate:", message.content); }, ], ]);
Registers all available events (retrieved from the Discord.js Events
enum) with the default logging handler.
eventHandler.logAllEvents();
-
New API Structure:
-
v4: Provided individual methods for each event (e.g.
channelCreate()
,guildCreate()
, etc.). -
v5: Uses a generic API:
on()
,registerEvents()
, andlogAllEvents()
. -
Impact: If your code directly calls methods like
channelCreate()
, you will need to refactor them to use the new API.
-
v4: Provided individual methods for each event (e.g.
-
Event Registration:
- v4: Separate event listener methods.
-
v5: Bulk registration is now available through
registerEvents()
, and a default logging handler is provided. - Impact: You must update your event registration logic to match the new format.
-
Update your dependency:
- Change your
package.json
to use discordjs-logger v5 (^5.0.0
). - With semver, users locked to
"^4.x.x"
will not automatically update to v5.
- Change your
-
Refactor event registration:
-
From v4 Example:
// v4 approach: const logger = new CDiscordEvent(client); logger.channelCreate(); logger.guildCreate(); // etc.
-
To v5 Approach:
import { Events } from "discord.js"; import DiscordEventHandler from "discordjs-logger"; const eventHandler = new DiscordEventHandler(client); // Register specific events with default logging: eventHandler.registerEvents([Events.ChannelCreate, Events.GuildCreate]); // Or use custom handlers: eventHandler.registerEvents([ [ Events.MessageCreate, (message) => { console.log("Custom handler for MessageCreate:", message.content); }, ], ]);
-
-
Review your logging behavior:
- v5’s default handlers log every event triggered. If this is too verbose for production, consider using custom handlers to control logging output.
-
Testing:
- Thoroughly test your bot with the new version in a development environment to ensure all events are logged and handled as expected.
- Version 5: Designed for use with Discord.js v14 or greater.
-
Version 4: Continue to use for Discord.js v13 or lower.
(Please refer to the previous documentation for v4 if needed.)
- Discord.js Documentation: https://discord.js.org/#/docs/main/stable/class/Client