mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
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:
@@ -1191,6 +1191,63 @@ var Client = (function (_EventEmitter) {
|
||||
return this.internal.removeFriend(user).then(dataCallback(callback), errorCallback(callback));
|
||||
};
|
||||
|
||||
Client.prototype.getServerWebhooks = function getServerWebhooks(guild) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function () /*err, {}*/{} : arguments[1];
|
||||
|
||||
return this.internal.getServerWebhooks(guild).then(dataCallback(callback), errorCallback(callback));
|
||||
};
|
||||
|
||||
Client.prototype.getChannelWebhooks = function getChannelWebhooks(channel) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function () /*err, {}*/{} : arguments[1];
|
||||
|
||||
return this.internal.getChannelWebhooks(channel).then(dataCallback(callback), errorCallback(callback));
|
||||
};
|
||||
|
||||
Client.prototype.sendWebhookMessage = function sendWebhookMessage(webhook, content) {
|
||||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
||||
var callback = arguments.length <= 3 || arguments[3] === undefined ? function () /*err, {}*/{} : arguments[3];
|
||||
|
||||
if (typeof options === "function") {
|
||||
// options is the callback
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
return this.internal.sendWebhookMessage(webhook, content, options).then(dataCallback(callback), errorCallback(callback));
|
||||
};
|
||||
|
||||
Client.prototype.editWebhook = function editWebhook(webhook) {
|
||||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
||||
var callback = arguments.length <= 2 || arguments[2] === undefined ? function () /*err, {}*/{} : arguments[2];
|
||||
|
||||
if (typeof options === "function") {
|
||||
// options is the callback
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
return this.internal.editWebhook(webhook, options).then(dataCallback(callback), errorCallback(callback));
|
||||
};
|
||||
|
||||
Client.prototype.createWebhook = function createWebhook(webhook) {
|
||||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
||||
var callback = arguments.length <= 2 || arguments[2] === undefined ? function () /*err, {}*/{} : arguments[2];
|
||||
|
||||
if (typeof options === "function") {
|
||||
// options is the callback
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
return this.internal.createWebhook(webhook, options).then(dataCallback(callback), errorCallback(callback));
|
||||
};
|
||||
|
||||
Client.prototype.deleteWebhook = function deleteWebhook(webhook) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function () /*err, {}*/{} : arguments[1];
|
||||
|
||||
return this.internal.createWebhook(webhook).then(dataCallback(callback), errorCallback(callback));
|
||||
};
|
||||
|
||||
// def getOAuthApplication
|
||||
|
||||
Client.prototype.getOAuthApplication = function getOAuthApplication(appID) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -84,6 +84,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);
|
||||
@@ -308,6 +312,27 @@ var Resolver = (function () {
|
||||
return null;
|
||||
};
|
||||
|
||||
Resolver.prototype.resolveWebhook = function resolveWebhook(resource) {
|
||||
/*
|
||||
accepts a Webhook
|
||||
*/
|
||||
if (resource instanceof _StructuresWebhook2["default"]) {
|
||||
return Promise.resolve(resource);
|
||||
}
|
||||
if (resource instanceof String || typeof resource === "string") {
|
||||
var server = this.internal.servers.find(function (s) {
|
||||
return s.webhooks.has("id", resource);
|
||||
});
|
||||
if (server) {
|
||||
return Promise.resolve(server.webhooks.get("id", resource));
|
||||
}
|
||||
}
|
||||
|
||||
var error = new Error("Could not resolve webhook");
|
||||
error.resource = resource;
|
||||
return Promise.reject(error);
|
||||
};
|
||||
|
||||
Resolver.prototype.resolveMessage = function resolveMessage(resource) {
|
||||
// accepts a Message, PMChannel & TextChannel
|
||||
|
||||
|
||||
@@ -64,6 +64,9 @@ var Endpoints = Constants.Endpoints = {
|
||||
SERVER_CHANNELS: function SERVER_CHANNELS(serverID) {
|
||||
return Endpoints.SERVER(serverID) + "/channels";
|
||||
},
|
||||
SERVER_WEBHOOKS: function SERVER_WEBHOOKS(serverID) {
|
||||
return Endpoints.SERVER(serverID) + "/webhooks";
|
||||
},
|
||||
|
||||
// channels
|
||||
CHANNELS: API + "/channels",
|
||||
@@ -91,6 +94,24 @@ var Endpoints = Constants.Endpoints = {
|
||||
CHANNEL_PIN: function CHANNEL_PIN(channelID, messageID) {
|
||||
return Endpoints.CHANNEL_PINS(channelID) + "/" + messageID;
|
||||
},
|
||||
CHANNEL_WEBHOOKS: function CHANNEL_WEBHOOKS(channelID) {
|
||||
return Endpoints.CHANNEL(channelID) + "/webhooks";
|
||||
},
|
||||
|
||||
// webhooks
|
||||
WEBHOOKS: API + "/webhooks",
|
||||
WEBHOOK: function WEBHOOK(webhookID) {
|
||||
return Endpoints.WEBHOOKS + "/" + webhookID;
|
||||
},
|
||||
WEBHOOK_WITH_TOKEN: function WEBHOOK_WITH_TOKEN(webhookID, webhookToken) {
|
||||
return Endpoints.WEBHOOKS + "/" + webhookToken;
|
||||
},
|
||||
WEBHOOK_MESSAGE: function WEBHOOK_MESSAGE(webhookID, webhookToken) {
|
||||
return Endpoints.WEBHOOK(webhookID) + "/" + webhookToken;
|
||||
},
|
||||
WEBHOOK_MESSAGE_SLACK: function WEBHOOK_MESSAGE_SLACK(webhookID, webhookToken) {
|
||||
return Endpoints.WEBHOOK_MESSAGE(webhookID, webhookToken) + "/slack";
|
||||
},
|
||||
|
||||
// friends
|
||||
FRIENDS: API + "/users/@me/relationships"
|
||||
|
||||
@@ -450,6 +450,20 @@ var Server = (function (_Equality) {
|
||||
};
|
||||
|
||||
_createClass(Server, [{
|
||||
key: "webhooks",
|
||||
get: function get() {
|
||||
return this.channels.map(function (c) {
|
||||
return c.webhooks;
|
||||
}).reduce(function (previousChannel, currentChannel) {
|
||||
if (currentChannel) {
|
||||
currentChannel.forEach(function (webhook) {
|
||||
previousChannel.add(webhook);
|
||||
});
|
||||
}
|
||||
return previousChannel;
|
||||
}, new _UtilCache2["default"]("id"));
|
||||
}
|
||||
}, {
|
||||
key: "createdAt",
|
||||
get: function get() {
|
||||
return new Date(+this.id / 4194304 + 1420070400000);
|
||||
|
||||
@@ -30,6 +30,7 @@ var TextChannel = (function (_ServerChannel) {
|
||||
|
||||
this.topic = data.topic;
|
||||
this.lastMessageID = data.last_message_id || data.lastMessageID;
|
||||
this.webhooks = new _UtilCache2["default"]("id");
|
||||
this.messages = new _UtilCache2["default"]("id", client.options.maxCachedMessages);
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +86,10 @@ var _StructuresVoiceChannel = require("./Structures/VoiceChannel");
|
||||
|
||||
var _StructuresVoiceChannel2 = _interopRequireDefault(_StructuresVoiceChannel);
|
||||
|
||||
var _StructuresWebhook = require("./Structures/Webhook");
|
||||
|
||||
var _StructuresWebhook2 = _interopRequireDefault(_StructuresWebhook);
|
||||
|
||||
var _Constants = require("./Constants");
|
||||
|
||||
var _Constants2 = _interopRequireDefault(_Constants);
|
||||
@@ -108,6 +112,7 @@ exports["default"] = {
|
||||
TextChannel: _StructuresTextChannel2["default"],
|
||||
User: _StructuresUser2["default"],
|
||||
VoiceChannel: _StructuresVoiceChannel2["default"],
|
||||
Webhook: _StructuresWebhook2["default"],
|
||||
Constants: _Constants2["default"],
|
||||
Cache: _UtilCacheJs2["default"]
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user