mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 17:43:30 +01:00
adds new WebhookClient and allows you to fetch channel webhooks and such without being "over the top" (#768)
* start blocking out client * proto webhookclient * wee working webhooks * it's all working * run docs * fix jsdoc issues * add example for webhookClient * add example in the examples place * fix docs
This commit is contained in:
@@ -7,6 +7,7 @@ const User = requireStructure('User');
|
||||
const GuildMember = requireStructure('GuildMember');
|
||||
const Role = requireStructure('Role');
|
||||
const Invite = requireStructure('Invite');
|
||||
const Webhook = requireStructure('Webhook');
|
||||
|
||||
class RESTMethods {
|
||||
constructor(restManager) {
|
||||
@@ -537,6 +538,87 @@ class RESTMethods {
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
fetchGuildWebhooks(guild) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('get', Constants.Endpoints.guildWebhooks(guild.id), true)
|
||||
.then(data => {
|
||||
const hooks = new Collection();
|
||||
for (const hook of data) {
|
||||
hooks.set(hook.id, new Webhook(this.rest.client, hook));
|
||||
}
|
||||
resolve(hooks);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
fetchChannelWebhooks(channel) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('get', Constants.Endpoints.channelWebhooks(channel.id), true)
|
||||
.then(data => {
|
||||
const hooks = new Collection();
|
||||
for (const hook of data) {
|
||||
hooks.set(hook.id, new Webhook(this.rest.client, hook));
|
||||
}
|
||||
resolve(hooks);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
fetchWebhook(id, token) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('get', Constants.Endpoints.webhook(id, token), require('util').isUndefined(token))
|
||||
.then(data => {
|
||||
resolve(new Webhook(this.rest.client, data));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
createChannelWebhook(channel, name, avatar) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('post', Constants.Endpoints.channelWebhooks(channel.id), true, {
|
||||
name: name, avatar: avatar,
|
||||
})
|
||||
.then(data => {
|
||||
resolve(new Webhook(this.rest.client, data));
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
deleteChannelWebhook(webhook) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('delete', Constants.Endpoints.webhook(webhook.id, webhook.token), false)
|
||||
.then(resolve).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
editChannelWebhook(webhook, name, avatar) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('patch', Constants.Endpoints.webhook(webhook.id, webhook.token), false, {
|
||||
name: name, avatar: avatar,
|
||||
})
|
||||
.then(data => {
|
||||
resolve(data);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
sendWebhookMessage(webhook, content, { avatarURL, tts, disableEveryone, embeds } = {}, file = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof content !== 'undefined') content = this.rest.client.resolver.resolveString(content);
|
||||
|
||||
if (disableEveryone || (typeof disableEveryone === 'undefined' && this.rest.client.options.disableEveryone)) {
|
||||
content = content.replace('@everyone', '@\u200beveryone').replace('@here', '@\u200bhere');
|
||||
}
|
||||
|
||||
this.rest.makeRequest('post', `${Constants.Endpoints.webhook(webhook.id, webhook.token)}?wait=true`, false, {
|
||||
content: content, username: webhook.name, avatar_url: avatarURL, tts: tts, file: file, embeds: embeds,
|
||||
})
|
||||
.then(data => {
|
||||
resolve(data);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RESTMethods;
|
||||
|
||||
Reference in New Issue
Block a user