https://github.com/penseapp/discord-notification
A package to send formatted discord notifications
https://github.com/penseapp/discord-notification
discord discord-notification nodejs typescript
Last synced: over 1 year ago
JSON representation
A package to send formatted discord notifications
- Host: GitHub
- URL: https://github.com/penseapp/discord-notification
- Owner: penseapp
- License: mit
- Created: 2021-01-26T18:27:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-07T13:13:58.000Z (over 3 years ago)
- Last Synced: 2025-03-25T21:13:21.119Z (over 1 year ago)
- Topics: discord, discord-notification, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 711 KB
- Stars: 10
- Watchers: 3
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Discord notifications

[](https://badge.fury.io/js/@penseapp%2Fdiscord-notification)


[](https://coveralls.io/github/penseapp/discord-notification?branch=master)
[](https://img.shields.io/github/license/penseapp/discord-notification)
[](https://img.shields.io/github/issues/penseapp/discord-notification)
[](https://img.shields.io/github/v/tag/penseapp/discord-notification)
[](https://img.shields.io/github/languages/count/penseapp/discord-notification)
## Install steps
```sh
npm i @penseapp/discord-notification
```
or
```sh
yarn add @penseapp/discord-notification
```
## How to use
Import the package:
```js
import { DiscordNotification } from '@penseapp/discord-notification'
```
Instantiate a new class of DiscordNotification passing the name and discord webhook
```js
// 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
```js
// 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
```js
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
```js
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()
```
#### you can use @here or @everyone if is something critical using `addContent`
```js
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('@everyone')
.sendMessage()
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('@here looks here!')
.sendMessage()
```
#### Send formatted JSON
```js
discordNotification
.errorMessage()
.addTitle('Error bla')
.addContent('```json'+ '\n' + JSON.stringify({ "myjson": "bla" }) +'```')
.sendMessage()
```
#### Send formatted Stack error message
```js
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
```js
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.