add webhooks v8 (#759)

* add webhook structure and getChannelWebhooks as well as getServerWebhooks

* add sendMessage

* add the ability to edit create and delete hooks

* remove server wide cache and add getter.
This commit is contained in:
Jacob
2016-10-01 06:53:14 -04:00
committed by Amish Shah
parent d22ca969db
commit c00d209014
15 changed files with 548 additions and 45 deletions

View File

@@ -78,6 +78,10 @@ var _StructuresInvite = require("../Structures/Invite");
var _StructuresInvite2 = _interopRequireDefault(_StructuresInvite);
var _StructuresWebhook = require("../Structures/Webhook");
var _StructuresWebhook2 = _interopRequireDefault(_StructuresWebhook);
var _VoiceVoiceConnection = require("../Voice/VoiceConnection");
var _VoiceVoiceConnection2 = _interopRequireDefault(_VoiceVoiceConnection);
@@ -1792,6 +1796,124 @@ var InternalClient = (function () {
return this.apiRequest("delete", _Constants.Endpoints.FRIENDS + "/" + user.id, true);
};
InternalClient.prototype.getServerWebhooks = function getServerWebhooks(server) {
var _this36 = this;
server = this.resolver.resolveServer(server);
if (!server) {
return Promise.reject(new Error("Failed to resolve server"));
}
return this.apiRequest("get", _Constants.Endpoints.SERVER_WEBHOOKS(server.id), true).then(function (res) {
return res.map(function (webhook) {
var channel = _this36.channels.get("id", webhook.channel_id);
return channel.webhooks.add(new _StructuresWebhook2["default"](webhook, server, channel, _this36.users.get("id", webhook.user.id)));
});
});
};
InternalClient.prototype.getChannelWebhooks = function getChannelWebhooks(channel) {
var _this37 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (!channel) {
return Promise.reject(new Error("Failed to resolve channel"));
}
return _this37.apiRequest("get", _Constants.Endpoints.CHANNEL_WEBHOOKS(channel.id), true).then(function (res) {
return res.map(function (webhook) {
return channel.webhooks.add(new _StructuresWebhook2["default"](webhook, _this37.servers.get("id", webhook.guild_id), channel, _this37.users.get("id", webhook.user.id)));
});
});
});
};
InternalClient.prototype.editWebhook = function editWebhook(webhook) {
var _this38 = this;
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
return this.resolver.resolveWebhook(webhook).then(function (webhook) {
if (!webhook) {
return Promise.reject(new Error(" Failed to resolve webhook"));
}
if (options.hasOwnProperty("avatar")) {
options.avatar = _this38.resolver.resolveToBase64(options.avatar);
}
return _this38.apiRequest("patch", _Constants.Endpoints.WEBHOOK(webhook.id), true, options).then(function (res) {
webhook.name = res.name;
webhook.avatar = res.hasOwnProperty('avatar') ? res.avatar : webhook.avatar;
});
});
};
InternalClient.prototype.createWebhook = function createWebhook(channel) {
var _this39 = this;
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
return this.resolver.resolveChannel(channel).then(function (destination) {
if (!channel) {
return Promise.reject(new Error(" Failed to resolve channel"));
}
if (options.hasOwnProperty("avatar")) {
options.avatar = _this39.resolver.resolveToBase64(options.avatar);
}
return _this39.apiRequest("post", _Constants.Endpoints.CHANNEL_WEBHOOKS(destination.id), true, options).then(function (webhook) {
return channel.webhooks.add(new _StructuresWebhook2["default"](webhook, _this39.servers.get("id", webhook.guild_id), channel, _this39.users.get("id", webhook.user.id)));
});
});
};
InternalClient.prototype.deleteWebhook = function deleteWebhook(webhook) {
var _this40 = this;
return this.resolver.resolveWebhook(webhook).then(function (webhook) {
if (!webhook) {
return Promise.reject(new Error(" Failed to resolve webhook"));
}
return _this40.apiRequest("delete", _Constants.Endpoints.WEBHOOK(webhook.id), true).then(function () {
webhook.channel.webhooks.remove(webhook);
});
});
};
InternalClient.prototype.sendWebhookMessage = function sendWebhookMessage(webhook, _content) {
var _this41 = this;
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return this.resolver.resolveWebhook(webhook).then(function (destination) {
var content = _this41.resolver.resolveString(_content);
if (_this41.client.options.disableEveryone || options.disableEveryone) {
content = content.replace(/(@)(everyone|here)/g, "$1$2");
}
if (!options.hasOwnProperty("username")) {
options.username = _this41.user.username;
}
var slack = undefined;
if (options.hasOwnProperty("slack")) {
slack = options.slack;
delete options["slack"];
}
options.content = _content;
return _this41.apiRequest("post", "" + _Constants.Endpoints.WEBHOOK_MESSAGE(destination.id, destination.token) + (slack ? "/slack" : "") + "?wait=true", true, options)["catch"](console.error).then(function (res) {
return destination.channel.messages.add(new _StructuresMessage2["default"](res, destination.channel, _this41.client));
});
});
};
//def getOAuthApplication
InternalClient.prototype.getOAuthApplication = function getOAuthApplication(appID) {
@@ -1818,7 +1940,7 @@ var InternalClient = (function () {
};
InternalClient.prototype.createWS = function createWS(url) {
var _this36 = this;
var _this42 = this;
if (this.websocket) {
return false;
@@ -1833,10 +1955,10 @@ var InternalClient = (function () {
this.websocket.onopen = function () {};
this.websocket.onclose = function (event) {
_this36.websocket = null;
_this36.state = _ConnectionState2["default"].DISCONNECTED;
_this42.websocket = null;
_this42.state = _ConnectionState2["default"].DISCONNECTED;
if (event && event.code) {
_this36.client.emit("warn", "WS close: " + event.code);
_this42.client.emit("warn", "WS close: " + event.code);
var err;
if (event.code === 4001) {
err = new Error("Gateway received invalid OP code");
@@ -1851,7 +1973,7 @@ var InternalClient = (function () {
}if (event.code === 4006 || event.code === 4009) {
err = new Error("Invalid session");
} else if (event.code === 4007) {
_this36.sequence = 0;
_this42.sequence = 0;
err = new Error("Invalid sequence number");
} else if (event.code === 4008) {
err = new Error("Gateway connection was ratelimited");
@@ -1859,17 +1981,17 @@ var InternalClient = (function () {
err = new Error("Invalid shard key");
}
if (err) {
_this36.client.emit("error", err);
_this42.client.emit("error", err);
}
}
_this36.disconnected(_this36.client.options.autoReconnect);
_this42.disconnected(_this42.client.options.autoReconnect);
};
this.websocket.onerror = function (e) {
_this36.client.emit("error", e);
_this36.websocket = null;
_this36.state = _ConnectionState2["default"].DISCONNECTED;
_this36.disconnected(_this36.client.options.autoReconnect);
_this42.client.emit("error", e);
_this42.websocket = null;
_this42.state = _ConnectionState2["default"].DISCONNECTED;
_this42.disconnected(_this42.client.options.autoReconnect);
};
this.websocket.onmessage = function (e) {
@@ -1882,54 +2004,54 @@ var InternalClient = (function () {
try {
packet = JSON.parse(e.data);
} catch (e) {
_this36.client.emit("error", e);
_this42.client.emit("error", e);
return;
}
_this36.client.emit("raw", packet);
_this42.client.emit("raw", packet);
if (packet.s) {
_this36.sequence = packet.s;
_this42.sequence = packet.s;
}
switch (packet.op) {
case 0:
_this36.processPacket(packet);
_this42.processPacket(packet);
break;
case 1:
_this36.heartbeat();
_this42.heartbeat();
break;
case 7:
_this36.disconnected(true);
_this42.disconnected(true);
break;
case 9:
_this36.sessionID = null;
_this36.sequence = 0;
_this36.identify();
_this42.sessionID = null;
_this42.sequence = 0;
_this42.identify();
break;
case 10:
if (_this36.sessionID) {
_this36.resume();
if (_this42.sessionID) {
_this42.resume();
} else {
_this36.identify();
_this42.identify();
}
_this36.heartbeat();
_this36.intervals.kai = setInterval(function () {
return _this36.heartbeat();
_this42.heartbeat();
_this42.intervals.kai = setInterval(function () {
return _this42.heartbeat();
}, packet.d.heartbeat_interval);
break;
case 11:
_this36.heartbeatAcked = true;
_this42.heartbeatAcked = true;
break;
default:
_this36.client.emit("unknown", packet);
_this42.client.emit("unknown", packet);
break;
}
};
};
InternalClient.prototype.processPacket = function processPacket(packet) {
var _this37 = this;
var _this43 = this;
var client = this.client;
var data = packet.d;
@@ -1954,37 +2076,37 @@ var InternalClient = (function () {
data.guilds.forEach(function (server) {
if (!server.unavailable) {
server = _this37.servers.add(new _StructuresServer2["default"](server, client));
server = _this43.servers.add(new _StructuresServer2["default"](server, client));
if (client.options.bot === false) {
_this37.unsyncedGuilds++;
_this37.syncGuild(server.id);
_this43.unsyncedGuilds++;
_this43.syncGuild(server.id);
}
if (_this37.client.options.forceFetchUsers && server.members && server.members.length < server.memberCount) {
_this37.getGuildMembers(server.id, Math.ceil(server.memberCount / 1000));
if (_this43.client.options.forceFetchUsers && server.members && server.members.length < server.memberCount) {
_this43.getGuildMembers(server.id, Math.ceil(server.memberCount / 1000));
}
} else {
client.emit("debug", "server " + server.id + " was unavailable, could not create (ready)");
_this37.unavailableServers.add(server);
_this43.unavailableServers.add(server);
}
});
data.private_channels.forEach(function (pm) {
_this37.private_channels.add(new _StructuresPMChannel2["default"](pm, client));
_this43.private_channels.add(new _StructuresPMChannel2["default"](pm, client));
});
if (!data.user.bot) {
// bots dont have friends
data.relationships.forEach(function (friend) {
if (friend.type === 1) {
// is a friend
_this37.friends.add(new _StructuresUser2["default"](friend.user, client));
_this43.friends.add(new _StructuresUser2["default"](friend.user, client));
} else if (friend.type === 2) {
// incoming friend requests
_this37.blocked_users.add(new _StructuresUser2["default"](friend.user, client));
_this43.blocked_users.add(new _StructuresUser2["default"](friend.user, client));
} else if (friend.type === 3) {
// incoming friend requests
_this37.incoming_friend_requests.add(new _StructuresUser2["default"](friend.user, client));
_this43.incoming_friend_requests.add(new _StructuresUser2["default"](friend.user, client));
} else if (friend.type === 4) {
// outgoing friend requests
_this37.outgoing_friend_requests.add(new _StructuresUser2["default"](friend.user, client));
_this43.outgoing_friend_requests.add(new _StructuresUser2["default"](friend.user, client));
} else {
client.emit("warn", "unknown friend type " + friend.type);
}