Rearrange and clean up more webhook stuff

This commit is contained in:
Schuyler Cebulskie
2016-10-09 15:50:50 -04:00
parent e7745a0af5
commit 29b33bffaa
7 changed files with 66 additions and 80 deletions

File diff suppressed because one or more lines are too long

View File

@@ -279,6 +279,15 @@ class Client extends EventEmitter {
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.
* If the message has been edited, the time of the edit is used rather than the time of the original message.

View File

@@ -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) => {
this.rest.makeRequest('get', Constants.Endpoints.guildWebhooks(guild.id), true)
.then(data => {
@@ -552,7 +561,7 @@ class RESTMethods {
});
}
fetchChannelWebhooks(channel) {
getChannelWebhooks(channel) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('get', Constants.Endpoints.channelWebhooks(channel.id), true)
.then(data => {
@@ -565,19 +574,11 @@ class RESTMethods {
});
}
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) {
createWebhook(channel, name, avatar) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('post', Constants.Endpoints.channelWebhooks(channel.id), true, {
name: name, avatar: avatar,
name,
avatar,
})
.then(data => {
resolve(new Webhook(this.rest.client, data));
@@ -585,22 +586,15 @@ class RESTMethods {
});
}
deleteChannelWebhook(webhook) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('delete', Constants.Endpoints.webhook(webhook.id, webhook.token), false)
.then(resolve).catch(reject);
editWebhook(webhook, name, avatar) {
return this.rest.makeRequest('patch', Constants.Endpoints.webhook(webhook.id, webhook.token), false, {
name,
avatar,
});
}
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);
});
deleteWebhook(webhook) {
return this.rest.makeRequest('delete', Constants.Endpoints.webhook(webhook.id, webhook.token), false);
}
sendWebhookMessage(webhook, content, { avatarURL, tts, disableEveryone, embeds } = {}, file = null) {

View File

@@ -301,7 +301,7 @@ class Guild {
* @returns {Collection<Webhook>}
*/
fetchWebhooks() {
return this.client.rest.methods.fetchGuildWebhooks(this);
return this.client.rest.methods.getGuildWebhooks(this);
}
/**

View File

@@ -42,6 +42,38 @@ class TextChannel extends GuildChannel {
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
sendMessage() { return; }
sendTTSMessage() { return; }
@@ -56,9 +88,6 @@ class TextChannel extends GuildChannel {
get typingCount() { return; }
createCollector() { return; }
awaitMessages() { return; }
fetchWebhook() { return; }
fetchWebhooks() { return; }
createWebhook() { return; }
bulkDelete() { return; }
_cacheMessage() { return; }
}

View File

@@ -161,11 +161,11 @@ class Webhook {
this.client.resolver.resolveFile(avatar).then(file => {
let base64 = new Buffer(file, 'binary').toString('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);
}).catch(reject);
} else {
this.client.rest.methods.editChannelWebhook(this, name)
this.client.rest.methods.editWebhook(this, name)
.then(data => {
this.setup(data);
}).catch(reject);
@@ -178,7 +178,7 @@ class Webhook {
* @returns {Promise}
*/
delete() {
return this.client.rest.methods.deleteChannelWebhook(this);
return this.client.rest.methods.deleteWebhook(this);
}
}

View File

@@ -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.
* Only OAuth Bot accounts may use this method.
@@ -388,9 +345,6 @@ exports.applyToClass = (structure, full = false) => {
props.push('fetchPinnedMessages');
props.push('createCollector');
props.push('awaitMessages');
props.push('fetchWebhook');
props.push('fetchWebhooks');
props.push('createWebhook');
}
for (const prop of props) applyProp(structure, prop);
};