mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
Rearrange and clean up more webhook stuff
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -279,6 +279,15 @@ class Client extends EventEmitter {
|
|||||||
return this.rest.methods.getInvite(code);
|
return this.rest.methods.getInvite(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch a webhook by ID.
|
||||||
|
* @param {string} id ID of the webhook
|
||||||
|
* @returns {Promise<Webhook>}
|
||||||
|
*/
|
||||||
|
fetchWebhook(id) {
|
||||||
|
return this.rest.methods.getWebhook(id);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sweeps all channels' messages and removes the ones older than the max message lifetime.
|
* Sweeps all channels' messages and removes the ones older than the max message lifetime.
|
||||||
* If the message has been edited, the time of the edit is used rather than the time of the original message.
|
* If the message has been edited, the time of the edit is used rather than the time of the original message.
|
||||||
|
|||||||
@@ -539,7 +539,16 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchGuildWebhooks(guild) {
|
getWebhook(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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getGuildWebhooks(guild) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.rest.makeRequest('get', Constants.Endpoints.guildWebhooks(guild.id), true)
|
this.rest.makeRequest('get', Constants.Endpoints.guildWebhooks(guild.id), true)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
@@ -552,7 +561,7 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchChannelWebhooks(channel) {
|
getChannelWebhooks(channel) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.rest.makeRequest('get', Constants.Endpoints.channelWebhooks(channel.id), true)
|
this.rest.makeRequest('get', Constants.Endpoints.channelWebhooks(channel.id), true)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
@@ -565,19 +574,11 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchWebhook(id, token) {
|
createWebhook(channel, name, avatar) {
|
||||||
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.rest.makeRequest('post', Constants.Endpoints.channelWebhooks(channel.id), true, {
|
this.rest.makeRequest('post', Constants.Endpoints.channelWebhooks(channel.id), true, {
|
||||||
name: name, avatar: avatar,
|
name,
|
||||||
|
avatar,
|
||||||
})
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
resolve(new Webhook(this.rest.client, data));
|
resolve(new Webhook(this.rest.client, data));
|
||||||
@@ -585,22 +586,15 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteChannelWebhook(webhook) {
|
editWebhook(webhook, name, avatar) {
|
||||||
return new Promise((resolve, reject) => {
|
return this.rest.makeRequest('patch', Constants.Endpoints.webhook(webhook.id, webhook.token), false, {
|
||||||
this.rest.makeRequest('delete', Constants.Endpoints.webhook(webhook.id, webhook.token), false)
|
name,
|
||||||
.then(resolve).catch(reject);
|
avatar,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
editChannelWebhook(webhook, name, avatar) {
|
deleteWebhook(webhook) {
|
||||||
return new Promise((resolve, reject) => {
|
return this.rest.makeRequest('delete', Constants.Endpoints.webhook(webhook.id, webhook.token), false);
|
||||||
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) {
|
sendWebhookMessage(webhook, content, { avatarURL, tts, disableEveryone, embeds } = {}, file = null) {
|
||||||
|
|||||||
@@ -301,7 +301,7 @@ class Guild {
|
|||||||
* @returns {Collection<Webhook>}
|
* @returns {Collection<Webhook>}
|
||||||
*/
|
*/
|
||||||
fetchWebhooks() {
|
fetchWebhooks() {
|
||||||
return this.client.rest.methods.fetchGuildWebhooks(this);
|
return this.client.rest.methods.getGuildWebhooks(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -42,6 +42,38 @@ class TextChannel extends GuildChannel {
|
|||||||
return members;
|
return members;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all webhooks for the channel.
|
||||||
|
* @returns {Promise<Collection<string, Webhook>>}
|
||||||
|
*/
|
||||||
|
fetchWebhooks() {
|
||||||
|
return this.client.rest.methods.getChannelWebhooks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a webhook for the channel.
|
||||||
|
* @param {string} name The name of the webhook.
|
||||||
|
* @param {FileResolvable} avatar The avatar for the webhook.
|
||||||
|
* @returns {Promise<Webhook>} webhook The created webhook.
|
||||||
|
* @example
|
||||||
|
* channel.createWebhook('Snek', 'http://snek.s3.amazonaws.com/topSnek.png')
|
||||||
|
* .then(webhook => console.log(`Created Webhook ${webhook}`))
|
||||||
|
* .catch(console.log)
|
||||||
|
*/
|
||||||
|
createWebhook(name, avatar) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (avatar) {
|
||||||
|
this.client.resolver.resolveFile(avatar).then(file => {
|
||||||
|
let base64 = new Buffer(file, 'binary').toString('base64');
|
||||||
|
let dataURI = `data:;base64,${base64}`;
|
||||||
|
this.client.rest.methods.createWebhook(this, name, dataURI).then(resolve).catch(reject);
|
||||||
|
}).catch(reject);
|
||||||
|
} else {
|
||||||
|
this.client.rest.methods.createWebhook(this, name).then(resolve).catch(reject);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// These are here only for documentation purposes - they are implemented by TextBasedChannel
|
// These are here only for documentation purposes - they are implemented by TextBasedChannel
|
||||||
sendMessage() { return; }
|
sendMessage() { return; }
|
||||||
sendTTSMessage() { return; }
|
sendTTSMessage() { return; }
|
||||||
@@ -56,9 +88,6 @@ class TextChannel extends GuildChannel {
|
|||||||
get typingCount() { return; }
|
get typingCount() { return; }
|
||||||
createCollector() { return; }
|
createCollector() { return; }
|
||||||
awaitMessages() { return; }
|
awaitMessages() { return; }
|
||||||
fetchWebhook() { return; }
|
|
||||||
fetchWebhooks() { return; }
|
|
||||||
createWebhook() { return; }
|
|
||||||
bulkDelete() { return; }
|
bulkDelete() { return; }
|
||||||
_cacheMessage() { return; }
|
_cacheMessage() { return; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,11 +161,11 @@ class Webhook {
|
|||||||
this.client.resolver.resolveFile(avatar).then(file => {
|
this.client.resolver.resolveFile(avatar).then(file => {
|
||||||
let base64 = new Buffer(file, 'binary').toString('base64');
|
let base64 = new Buffer(file, 'binary').toString('base64');
|
||||||
let dataURI = `data:;base64,${base64}`;
|
let dataURI = `data:;base64,${base64}`;
|
||||||
this.client.rest.methods.editChannelWebhook(this, name, dataURI)
|
this.client.rest.methods.editWebhook(this, name, dataURI)
|
||||||
.then(resolve).catch(reject);
|
.then(resolve).catch(reject);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
} else {
|
} else {
|
||||||
this.client.rest.methods.editChannelWebhook(this, name)
|
this.client.rest.methods.editWebhook(this, name)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
this.setup(data);
|
this.setup(data);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
@@ -178,7 +178,7 @@ class Webhook {
|
|||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
delete() {
|
delete() {
|
||||||
return this.client.rest.methods.deleteChannelWebhook(this);
|
return this.client.rest.methods.deleteWebhook(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -309,49 +309,6 @@ class TextBasedChannel {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch a webhook by ID
|
|
||||||
* @param {string} id The id of the webhook.
|
|
||||||
* @returns {Promise<Webhook>}
|
|
||||||
*/
|
|
||||||
fetchWebhook(id) {
|
|
||||||
return this.client.rest.methods.fetchWebhook(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch all webhooks for the channel.
|
|
||||||
* @returns {Collection<Webhook>}
|
|
||||||
*/
|
|
||||||
fetchWebhooks() {
|
|
||||||
return this.client.rest.methods.fetchChannelWebhooks(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a webhook for the channel.
|
|
||||||
* @param {string} name The name of the webhook.
|
|
||||||
* @param {FileResolvable=} avatar The avatar for the webhook.
|
|
||||||
* @returns {Webhook} webhook The created webhook.
|
|
||||||
* @example
|
|
||||||
* channel.createWebhook('Snek', 'http://snek.s3.amazonaws.com/topSnek.png')
|
|
||||||
* .then(webhook => console.log(`Created Webhook ${webhook}`))
|
|
||||||
* .catch(console.log)
|
|
||||||
*/
|
|
||||||
createWebhook(name, avatar) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (avatar) {
|
|
||||||
this.client.resolver.resolveFile(avatar).then(file => {
|
|
||||||
let base64 = new Buffer(file, 'binary').toString('base64');
|
|
||||||
let dataURI = `data:;base64,${base64}`;
|
|
||||||
this.client.rest.methods.createChannelWebhook(this, name, dataURI)
|
|
||||||
.then(resolve).catch(reject);
|
|
||||||
}).catch(reject);
|
|
||||||
} else {
|
|
||||||
this.client.rest.methods.createChannelWebhook(this, name)
|
|
||||||
.then(resolve).catch(reject);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bulk delete a given Collection or Array of messages in one go. Returns the deleted messages after.
|
* Bulk delete a given Collection or Array of messages in one go. Returns the deleted messages after.
|
||||||
* Only OAuth Bot accounts may use this method.
|
* Only OAuth Bot accounts may use this method.
|
||||||
@@ -388,9 +345,6 @@ exports.applyToClass = (structure, full = false) => {
|
|||||||
props.push('fetchPinnedMessages');
|
props.push('fetchPinnedMessages');
|
||||||
props.push('createCollector');
|
props.push('createCollector');
|
||||||
props.push('awaitMessages');
|
props.push('awaitMessages');
|
||||||
props.push('fetchWebhook');
|
|
||||||
props.push('fetchWebhooks');
|
|
||||||
props.push('createWebhook');
|
|
||||||
}
|
}
|
||||||
for (const prop of props) applyProp(structure, prop);
|
for (const prop of props) applyProp(structure, prop);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user