GithubHelp home page GithubHelp logo

marcos1305 / discord-notification Goto Github PK

View Code? Open in Web Editor NEW

This project forked from penseapp/discord-notification

0.0 0.0 0.0 646 KB

A package to send formatted discord notifications

License: MIT License

Shell 1.40% JavaScript 6.09% TypeScript 91.99% Dockerfile 0.52%

discord-notification's Introduction

Discord notifications

npm
npm version test GitHub Workflow Status (event) Coverage Status License Status

Issues Status Tag Status Languages Status

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:

image

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()

you can use @here or @everyone if is something critical using addContent

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:

Peek 2021-06-02 22-21

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.

discord-notification's People

Contributors

joaomantovani avatar marcos1305 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.