npm i @penseapp/discord-notification
or
yarn add @penseapp/discord-notification
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:
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
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()
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('@everyone')
.sendMessage()
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('@here looks here!')
.sendMessage()
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('```json'+ '\n' + JSON.stringify({ "myjson": "bla" }) +'```')
.sendMessage()
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()
}
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()
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.
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.