discord-webhooks-node

Discord Webhooks

Installation

npm install discord-webhooks-node

Usage

Basic Usage

import { Webhook } from 'discord-webhooks-node';

const webhook = new Webhook({
url: 'WEBHOOK_URL',
});

webhook.execute({
content: 'Hello world!',
}).then(() => console.log('Sent!')).catch((err) => console.error('Failed! ', err));

Send files

import { Webhook } from 'discord-webhooks-node';

const webhook = new Webhook({
url: 'WEBHOOK_URL',
});

webhook.execute({
content: 'Hello world!',
files: [
{
name: 'test.txt',
file: Buffer.from('Hello world!'),
},
],
}).then(() => console.log('Sent!')).catch((err) => console.error('Failed! ', err));

As an embed attachment:

import { readFileSync } from 'fs';
import { Embed, Webhook } from 'discord-webhooks-node';

const webhook = new Webhook({
url: 'WEBHOOK_URL',
});

webhook.execute({
content: 'Hello world!',
embeds: [new Embed().setTitle('Hello world!').setDescription('This is a test embed.').setImage('attachment://test.jpeg').toJSON()],
files: [
{
name: 'test.jpeg',
// Read test.jpeg from the current directory
file: readFileSync(__dirname + '/test.jpeg'),
},
],
}).then(() => console.log('Sent!')).catch((err) => console.error('Failed! ', err));

Send embeds

import { readFileSync } from 'fs';
import { Button, Embed, MessageBuilder, Webhook } from '../src';

const webhook = new Webhook({
url: 'WEBHOOK_URL',
});

const msg = new MessageBuilder('Hello World!')
.addEmbed(
new Embed()
.setTitle('Hello World!')
.setDescription('This is a test embed!')
.setColor('#ff0000')
.setFooter('https://discord.com', 'This is a test footer!')
.setTimestamp()
.setThumbnail('https://cdn.discordapp.com/embed/avatars/0.png')
.setAuthor('Test Author', 'https://discord.com', 'https://cdn.discordapp.com/embed/avatars/0.png')
.addField('Test Field', 'This is a test field!', false)
)
.addComponent(new Button().setLabel('Test Button').setStyle(1).setURL('https://discord.com'));

webhook.execute(msg.toJSON()).then(() => console.log('Sent!')).catch((err) => console.error('Failed! ', err));

Config

import { Webhook } from 'discord-webhooks-node';

const webhook = new Webhook({
url: 'WEBHOOK_URL',
})

webhook.setUsername('Test User');
webhook.setAvatar('https://cdn.discordapp.com/embed/avatars/0.png');

License

  • This project is MIT Licensed. Read the license file for more information.

Generated using TypeDoc