detritus-client
Version:
A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.
159 lines (158 loc) • 5.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Webhook = void 0;
const detritus_client_rest_1 = require("detritus-client-rest");
const baseset_1 = require("../collections/baseset");
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const basestructure_1 = require("./basestructure");
const channel_1 = require("./channel");
const guild_1 = require("./guild");
const user_1 = require("./user");
const keysWebhook = new baseset_1.BaseSet([
constants_1.DiscordKeys.APPLICATION_ID,
constants_1.DiscordKeys.AVATAR,
constants_1.DiscordKeys.CHANNEL_ID,
constants_1.DiscordKeys.DISCRIMINATOR,
constants_1.DiscordKeys.GUILD_ID,
constants_1.DiscordKeys.ID,
constants_1.DiscordKeys.NAME,
constants_1.DiscordKeys.SOURCE_CHANNEL,
constants_1.DiscordKeys.SOURCE_GUILD,
constants_1.DiscordKeys.TOKEN,
constants_1.DiscordKeys.USER,
]);
/**
* Webhook Structure
* @category Structure
*/
class Webhook extends basestructure_1.BaseStructure {
constructor(client, data, isClone) {
super(client, undefined, isClone);
this._keys = keysWebhook;
this.applicationId = null;
this.avatar = null;
this.channelId = '';
this.discriminator = '0000';
this.guildId = '';
this.id = '';
this.name = '';
this.type = constants_1.WebhookTypes.INCOMING;
this.merge(data);
}
get avatarUrl() {
return this.avatarUrlFormat();
}
get channel() {
return this.client.channels.get(this.channelId) || null;
}
get createdAt() {
return new Date(this.createdAtUnix);
}
get createdAtUnix() {
return utils_1.Snowflake.timestamp(this.id);
}
get defaultAvatarUrl() {
return detritus_client_rest_1.Endpoints.CDN.URL + detritus_client_rest_1.Endpoints.CDN.AVATAR_DEFAULT(parseInt(this.discriminator) % 5);
}
get guild() {
return this.client.guilds.get(this.guildId) || null;
}
get jumpLink() {
return detritus_client_rest_1.Endpoints.Routes.URL + detritus_client_rest_1.Endpoints.Routes.USER(this.id);
}
get mention() {
return `<@${this.id}>`;
}
avatarUrlFormat(format, query) {
if (!this.avatar) {
return utils_1.addQuery(this.defaultAvatarUrl, query);
}
const hash = this.avatar;
format = utils_1.getFormatFromHash(hash, format, this.client.imageFormat);
return utils_1.addQuery(detritus_client_rest_1.Endpoints.CDN.URL + detritus_client_rest_1.Endpoints.CDN.AVATAR(this.id, hash, format), query);
}
async createMessage(options, compatibleType) {
return this.execute(options, compatibleType);
}
async delete(options = {}) {
if (this.token) {
return this.client.rest.deleteWebhookToken(this.id, this.token, options);
}
return this.client.rest.deleteWebhook(this.id, options);
}
async deleteMessage(messageId) {
if (!this.token) {
throw new Error('Webhook is missing its token');
}
return this.client.rest.deleteWebhookTokenMessage(this.id, this.token, messageId);
}
async edit(options = {}) {
if (this.token) {
return this.client.rest.editWebhookToken(this.id, this.token, options);
}
return this.client.rest.editWebhook(this.id, options);
}
async editMessage(messageId, options = {}) {
if (!this.token) {
throw new Error('Webhook is missing its token');
}
return this.client.rest.editWebhookTokenMessage(this.id, this.token, messageId, options);
}
async execute(options, compatibleType) {
if (!this.token) {
throw new Error('Webhook is missing its token');
}
return this.client.rest.executeWebhook(this.id, this.token, options, compatibleType);
}
async fetchMessage(messageId) {
if (!this.token) {
throw new Error('Webhook is missing its token');
}
return this.client.rest.fetchWebhookTokenMessage(this.id, this.token, messageId);
}
mergeValue(key, value) {
if (value !== undefined) {
switch (key) {
case constants_1.DiscordKeys.SOURCE_CHANNEL:
{
value.type = constants_1.ChannelTypes.GUILD_NEWS;
value = channel_1.createChannelFromData(this.client, value);
}
;
break;
case constants_1.DiscordKeys.SOURCE_GUILD:
{
value = new guild_1.BaseGuild(this.client, value);
}
;
break;
case constants_1.DiscordKeys.USER:
{
let user;
if (this.isClone) {
user = new user_1.User(this.client, value, this.isClone);
}
else {
if (this.client.users.has(value.id)) {
user = this.client.users.get(value.id);
user.merge(value);
}
else {
user = new user_1.User(this.client, value);
this.client.users.insert(user);
}
}
value = user;
}
;
break;
}
super.mergeValue(key, value);
}
}
toString() {
return `${this.name}#${this.discriminator}`;
}
}
exports.Webhook = Webhook;