mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
fix: Updated logging to pino for index.js
This commit is contained in:
1 parent
4a2b60e3ba
commit
2fe5fce324
1 file changed
+35
-79
@@ -16,13 +16,6 @@ const logger = pino({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.trace('Hello, World!');
|
|
||||||
logger.debug('Hello, World!');
|
|
||||||
logger.info('Hello, World!');
|
|
||||||
logger.warn('Hello, World!');
|
|
||||||
logger.error('Hello, World!');
|
|
||||||
logger.fatal('Hello, World!');
|
|
||||||
|
|
||||||
// Setup required permissions for the bot to work
|
// Setup required permissions for the bot to work
|
||||||
const client = new Discord.Client({
|
const client = new Discord.Client({
|
||||||
intents: [
|
intents: [
|
||||||
@@ -32,6 +25,7 @@ const client = new Discord.Client({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Setup commands collection and load commands
|
// Setup commands collection and load commands
|
||||||
|
// todo: extract this logic to a separate file
|
||||||
client.commands = new Discord.Collection();
|
client.commands = new Discord.Collection();
|
||||||
const commandFiles = fs
|
const commandFiles = fs
|
||||||
.readdirSync('./commands')
|
.readdirSync('./commands')
|
||||||
@@ -49,7 +43,6 @@ for (const file of systemCommandFiles) {
|
|||||||
client.commands.set(systemCommand.data.name, systemCommand);
|
client.commands.set(systemCommand.data.name, systemCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new Player, and attach it to the bot client.
|
|
||||||
const player = new Player(client, {
|
const player = new Player(client, {
|
||||||
useLegacyFFmpeg: false
|
useLegacyFFmpeg: false
|
||||||
});
|
});
|
||||||
@@ -70,80 +63,58 @@ onBeforeCreateStream(async (track) => {
|
|||||||
|
|
||||||
player.events.on('error', (queue, error) => {
|
player.events.on('error', (queue, error) => {
|
||||||
// Emitted when the player queue encounters error
|
// Emitted when the player queue encounters error
|
||||||
console.error(
|
logger.error(error, 'Player queue error event');
|
||||||
`${new Date()
|
|
||||||
.toISOString()
|
|
||||||
.substring(11, 19)}: Error: 🚨 General player error event: ${
|
|
||||||
error.message
|
|
||||||
}\n`
|
|
||||||
);
|
|
||||||
console.error(error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
player.events.on('playerError', (queue, error) => {
|
player.events.on('playerError', (queue, error) => {
|
||||||
// Emitted when the audio player errors while streaming audio track
|
// Emitted when the audio player errors while streaming audio track
|
||||||
console.error(
|
logger.error(error, 'Player audio stream error event');
|
||||||
`${new Date()
|
|
||||||
.toISOString()
|
|
||||||
.substring(11, 19)}: Error: 🚨 Player error event: ${
|
|
||||||
error.message
|
|
||||||
}\n`
|
|
||||||
);
|
|
||||||
console.error(error);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
client.once('ready', async () => {
|
client.once('ready', async () => {
|
||||||
console.log(
|
|
||||||
`${new Date().toISOString().substring(11, 19)}: Info: Logged in as ${
|
|
||||||
client.user.tag
|
|
||||||
}!`
|
|
||||||
);
|
|
||||||
|
|
||||||
// This method will load all the extractors from the @discord-player/extractor package
|
// This method will load all the extractors from the @discord-player/extractor package
|
||||||
await player.extractors.loadDefault();
|
await player.extractors.loadDefault();
|
||||||
console.log(player.scanDeps());
|
|
||||||
|
|
||||||
// Set Discord status
|
logger.info(`discord-player loaded dependencies:\n${player.scanDeps()}`);
|
||||||
|
logger.info(`Logged in as ${client.user.tag}!`);
|
||||||
|
logger.info(
|
||||||
|
`${client.user.tag} is currently added in ${client.guilds.cache.size} guilds`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set Discord presence status for bot
|
||||||
client.user.setActivity('/help', {
|
client.user.setActivity('/help', {
|
||||||
type: Discord.ActivityType.Watching,
|
type: Discord.ActivityType.Watching,
|
||||||
name: '/help'
|
name: '/help'
|
||||||
});
|
});
|
||||||
|
|
||||||
// Show how many guilds the bot is added to
|
|
||||||
console.log(
|
|
||||||
`${new Date().toISOString().substring(11, 19)}: Info: ${
|
|
||||||
client.user.tag
|
|
||||||
} is currently added in ${client.guilds.cache.size} guilds!`
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
client.once('reconnecting', () => {
|
client.once('reconnecting', () => {
|
||||||
console.log('Reconnecting!');
|
logger.warn('${client.user.tag} is reconnecting to Discord APIs.');
|
||||||
});
|
});
|
||||||
|
|
||||||
client.once('disconnect', () => {
|
client.once('disconnect', () => {
|
||||||
console.log('Disconnected');
|
logger.warn(
|
||||||
|
'${client.user.tag} lost connection to Discord APIs. Disconnected.'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('warn', (info) => {
|
client.on('warn', (warning) => {
|
||||||
console.log(info);
|
logger.warn(warning, 'Discord client warning event');
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('error', console.error);
|
client.on('error', (error) => {
|
||||||
|
logger.error(error, 'Discord client error event');
|
||||||
|
});
|
||||||
|
|
||||||
client.on('guildCreate', (guild) => {
|
client.on('guildCreate', (guild) => {
|
||||||
console.log(
|
logger.info(
|
||||||
`${new Date().toISOString().substring(11, 19)}: Info: 🟢 ${
|
`Added to guild '${guild.name}' with ${guild.memberCount} members.`
|
||||||
client.user.tag
|
|
||||||
} has been added to server '${guild.name} (#${guild.memberCount})'!`
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('guildDelete', (guild) => {
|
client.on('guildDelete', (guild) => {
|
||||||
console.log(
|
logger.info(
|
||||||
`${new Date().toISOString().substring(11, 19)}: Info: 🔴 ${
|
`Removed from guild '${guild.name}' with ${guild.memberCount} members.`
|
||||||
client.user.tag
|
|
||||||
} was removed from server '${guild.name} (#${guild.memberCount})'!`
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -165,22 +136,14 @@ client.on('interactionCreate', async (interaction) => {
|
|||||||
if (executionTime > 20000) {
|
if (executionTime > 20000) {
|
||||||
// don't send warning message for filters command, as collector timeout happens after 60 seconds
|
// don't send warning message for filters command, as collector timeout happens after 60 seconds
|
||||||
if (command.name === 'filters' && executionTime > 55000) {
|
if (command.name === 'filters' && executionTime > 55000) {
|
||||||
console.log(
|
logger.info(
|
||||||
`${new Date().toISOString().substring(11, 19)}: Info: ${
|
`${interaction.guild.name} (${interaction.guild.memberCount})> Command '${interaction}' executed in ${executionTime} ms.`
|
||||||
interaction.guild.name
|
|
||||||
} (#${
|
|
||||||
interaction.guild.memberCount
|
|
||||||
})> Command '${interaction}' executed in ${executionTime} ms.`
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
logger.warn(
|
||||||
`${new Date().toISOString().substring(11, 19)}: Warning: ⚠️ ${
|
`${interaction.guild.name} (${interaction.guild.memberCount})> Command '${interaction}' took ${executionTime} ms to execute.`
|
||||||
interaction.guild.name
|
|
||||||
} (#${
|
|
||||||
interaction.guild.memberCount
|
|
||||||
})> Command '${interaction}' took ${executionTime} ms to execute.`
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return await interaction.editReply({
|
return await interaction.editReply({
|
||||||
@@ -195,26 +158,19 @@ client.on('interactionCreate', async (interaction) => {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
logger.info(
|
||||||
`${new Date().toISOString().substring(11, 19)}: Info: ${
|
`${interaction.guild.name} (${interaction.guild.memberCount})> Command '${interaction}' executed in ${executionTime} ms.`
|
||||||
interaction.guild.name
|
|
||||||
} (#${
|
|
||||||
interaction.guild.memberCount
|
|
||||||
})> Command '${interaction}' executed in ${executionTime} ms.`
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// log error to console with full object depth
|
logger.error(
|
||||||
console.dir(error, { depth: 5 });
|
error,
|
||||||
console.error(
|
`${interaction.guild.name} (${interaction.guild.memberCount})> Command '${interaction}' failed to execute.`
|
||||||
`${new Date().toISOString().substring(11, 19)}: Error: 🚨 ${
|
|
||||||
interaction.guild.name
|
|
||||||
} (#${
|
|
||||||
interaction.guild.memberCount
|
|
||||||
})> Command '${interaction}' failed unexpectedly.`
|
|
||||||
);
|
);
|
||||||
console.error(`Interaction input:\n${interaction}`);
|
|
||||||
|
|
||||||
|
// todo: using interaction.editReply() might lead to "unknown interaction" error
|
||||||
|
// it might already have been replied to or deferred
|
||||||
|
// solution might be to use interaction.followUp() instead or send a message to the channel?
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
|
|||||||
Reference in new issue
Block a user