Allow sending embeds
This commit is contained in:
parent
86abbaf995
commit
c54abcc387
@ -2,7 +2,7 @@
|
||||
"name": "@navy.gif/discord-webhook",
|
||||
"version": "1.0.0",
|
||||
"description": "Client for interacting with discord webhooks",
|
||||
"main": "index.js",
|
||||
"main": "build/index.js",
|
||||
"license": "MIT",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
|
@ -36,7 +36,7 @@ class HttpClient extends EventEmitter {
|
||||
|
||||
super();
|
||||
|
||||
this.userAgent = userAgent ?? 'WebhookClient (v0.0.1)';
|
||||
this.userAgent = userAgent ?? 'HttpClient (v0.0.1)';
|
||||
this.baseUrl = baseUrl ?? null;
|
||||
if (this.baseUrl?.endsWith('/'))
|
||||
this.baseUrl = this.baseUrl.substring(0, this.baseUrl.length - 1);
|
||||
|
@ -2,7 +2,7 @@ import { EventEmitter } from 'events';
|
||||
import HttpClient from './Http.js';
|
||||
|
||||
// Types
|
||||
import { WebhookClientOptions, WebhookType, WebhookData } from './types/Webhook.js';
|
||||
import { WebhookClientOptions, WebhookType, WebhookData, Payload } from './types/Webhook.js';
|
||||
import { RequestError, WebhookError } from './util/index.js';
|
||||
|
||||
const WebhookDestroyed = new WebhookError('Webhook does not exist');
|
||||
@ -46,7 +46,7 @@ class WebhookClient extends EventEmitter {
|
||||
this.#token = token;
|
||||
this.#id = id;
|
||||
|
||||
this.#http = new HttpClient({ baseUrl: 'https://discord.com/api' });
|
||||
this.#http = new HttpClient({ baseUrl: 'https://discord.com/api', userAgent: 'WebhookClient (v1.0.0)' });
|
||||
this.#http.on('rateLimited', this.emit.bind(this, 'rateLimited'));
|
||||
|
||||
}
|
||||
@ -84,11 +84,18 @@ class WebhookClient extends EventEmitter {
|
||||
return this.#patch(result as WebhookData);
|
||||
}
|
||||
|
||||
async send (content: string) {
|
||||
async send (content: string | Payload) {
|
||||
if (this.#destroyed)
|
||||
throw WebhookDestroyed;
|
||||
|
||||
const body = { content };
|
||||
let body = null;
|
||||
if (typeof content === 'string')
|
||||
body = { content };
|
||||
else
|
||||
body = content;
|
||||
|
||||
if (!body.content && !body.embeds && !body.files)
|
||||
throw new WebhookError('Must supply one of content, embeds or files');
|
||||
const response = await this.#http.post(this.#url, body);
|
||||
return response;
|
||||
}
|
||||
|
@ -21,4 +21,33 @@ type WebhookData = {
|
||||
url: string
|
||||
}
|
||||
|
||||
export { WebhookData, WebhookClientOptions, WebhookType };
|
||||
type Embed = {
|
||||
title?: string,
|
||||
type?: string,
|
||||
description?: string,
|
||||
url?: string,
|
||||
timestamp?: string,
|
||||
color?: number,
|
||||
// TODO: Finish types
|
||||
// footer?: EmbedFooter,
|
||||
|
||||
}
|
||||
|
||||
type File = {
|
||||
[key: string]: string
|
||||
}
|
||||
|
||||
type Payload = {
|
||||
content?: string,
|
||||
username?: string,
|
||||
avatar_url?: string,
|
||||
tts?: boolean,
|
||||
embeds?: Embed[],
|
||||
// allowed_mentions?: AllowedMentions,
|
||||
files?: File[],
|
||||
// attachments?: AttachmentPartial[],
|
||||
flags?: number,
|
||||
thread_name?: string
|
||||
}
|
||||
|
||||
export { WebhookData, WebhookClientOptions, WebhookType, Payload };
|
@ -18,11 +18,20 @@ const webhook = new WebhookClient({ url: 'https://discord.com/api/webhooks/10938
|
||||
|
||||
webhook.on('rateLimited', () => console.log('Webhook was rate limited'));
|
||||
console.log(await webhook.fetch());
|
||||
await webhook.send({
|
||||
content: 'Hi',
|
||||
embeds: [{
|
||||
title: 'Embed test',
|
||||
description: 'Bingus'
|
||||
}]
|
||||
});
|
||||
const promises = [];
|
||||
for (let i = 0; i < 15; i++)
|
||||
promises.push(webhook.send(`Test ${i}`));
|
||||
|
||||
console.log('Loop done');
|
||||
console.log('promises', await Promise.all(promises));
|
||||
console.log('end');
|
||||
|
||||
// const promise = new Promise((resolve) => {
|
||||
// setTimeout(resolve, 5000);
|
||||
|
Loading…
Reference in New Issue
Block a user