Discord notifications
Install steps
npm i @penseapp/discord-notification
or
yarn add @penseapp/discord-notification
How to use
Import the package:
import { DiscordNotification } from '@penseapp/discord-notification'
Instantiate a new class of DiscordNotification passing the name and discord webhook
// Name of the microservice / webhook of discord
export const discordNotification = new DiscordNotification('my-name', 'https://discordapp.com/api/webhooks/qNbqLQDB5mD7Rxr6')
Call discordNotification obj, see the example above
// Get the variable in others files
discordNotification
.sucessfulMessage()
.addTitle('My title')
.addDescription('My description')
.addField({name: 'Field 1', value: 'Content #1', inline: false }) //breakline
.addField({name: 'Field 2', value: 'Content #2' })
.addField({name: 'Field 3', value: 'Content #3' })
.addFooter('My footer') // Small text at the end of discord notification
.sendMessage()
The output on discord will be:
messageTypes
There are 5 different messages
discordNotification.message() // Shows message without color
discordNotification.sucessfulMessage() // Shows green message
discordNotification.infoMessage() // Shows Blue message
discordNotification.errorMessage() // Shows red message
discordNotification.warningMessage() // Shows orange message
Examples
import { DiscordNotification } from "./DiscordNotification";
export const discordNotification = new DiscordNotification('my-name', 'https://discordapp.com/api/webhooks/qNbqLQDB5mD7Rxr6')
discordNotification
.sucessfulMessage()
.addTitle('My title')
.addDescription('My description')
.addField({name: 'Field 1', value: 'Content #1', inline: false }) //breakline
.addField({name: 'Field 2', value: 'Content #2' })
.addField({name: 'Field 3', value: 'Content #3' })
.addFooter('My footer') // Small text at the end of discord notification
.sendMessage()
addContent
you can use @here or @everyone if is something critical using discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('@everyone')
.sendMessage()
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('@here looks here!')
.sendMessage()
Send formatted JSON
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('```json'+ '\n' + JSON.stringify({ "myjson": "bla" }) +'```')
.sendMessage()
Send formatted Stack error message
try {
throw new Error('My error')
} catch (error) {
const e: Error = error as Error
discordNotification
.errorMessage()
.addTitle(e.message)
.addContent('```json'+ '\n' + JSON.stringify({
"teste": "teste"
}) +'```')
.sendMessage()
}
Other examples
discordNotification
.sucessfulMessage()
.addTitle('Testing color green (success)')
.sendMessage()
discordNotification
.errorMessage()
.addTitle('Testing color red (error)')
.sendMessage()
discordNotification
.warningMessage()
.addTitle('Testing color orange (warning)')
.sendMessage()
discordNotification
.infoMessage()
.addTitle('Testing color blue (info)')
.sendMessage()
discordNotification
.message()
.addTitle('Testing no color')
.sendMessage()
Discord webhook
Open Discord -> select the channel -> Click on configuration -> Integrations -> View webhooks -> New webhook
Create a new webhook called Discord notification
and add on Github secrets with a name of DISCORD_CHANNEL_WEBHOOK
, like the GIF bellow:
Now, you will receive the notifications on the desired discord channel.
Information
The tests does not pass on node version 10.* Use at your own risk.
The versions:
- 12.x
- 14.x
- 16.x
- 18.x
Are working.