Skip to content

Instantly share code, notes, and snippets.

@bmorrisondev
Last active March 22, 2021 14:42
Show Gist options
  • Save bmorrisondev/226855a500c7733576978dd9abdfabfa to your computer and use it in GitHub Desktop.
Save bmorrisondev/226855a500c7733576978dd9abdfabfa to your computer and use it in GitHub Desktop.
A class that streamlines logging from JavaScript to a Discord WebHook
// Usage:
// const DiscordLogger = require('./path/to/file.js')
// const logger = new DiscordLogger('my app name', 'https://discord.webhook/here');
// await logger.info('some message');
const axios = require('axios');
const COLORS = {
ERROR_RED: 16196387,
DEBUG_PURPLE: 12666618
}
module.exports = class DiscordLogger {
constructor(applicationName, webhookUrl) {
this.applicationName = applicationName
this.url = webhookUrl
}
async _buildEmbed(err, applicationName, message, color) {
let date = new Date();
let embed = {
"timestamp": date.toISOString()
}
let description = ""
if(message) {
description += message
}
if(err != null && err != undefined) {
description += "```\n" + err.stack + "```"
}
if(description !== "") {
embed.description = description
}
if(applicationName) {
embed.author = {
"name": applicationName
}
}
if(color) {
embed.color = color
}
return embed;
}
async _pushMessage(embed) {
let body = {
"embeds": [
embed
]
}
let opts = {
method: "POST",
url: this.url,
headers: {
"content-type": "application/json"
},
data: body
}
await axios(opts);
}
async error(err, message) {
let embed = this._buildEmbed(err, this.applicationName, message, COLORS.ERROR_RED)
await this._pushMessage(embed, this.url);
}
async info(message) {
let embed = this._buildEmbed(null, this.applicationName, message)
await this._pushMessage(embed, this.url);
}
async debug(message) {
let embed = this._buildEmbed(null, this.applicationName, message, COLORS.DEBUG_PURPLE)
await this._pushMessage(embed, this.url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment