guilded.ts
Version:
A powerful NPM module that allows you to easily interact with the Guilded API.
126 lines • 3.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Webhook = void 0;
const Base_1 = require("./Base");
/**
* Represents a webhook on Guilded.
* @example new Webhook(channel, rawWebhook);
*/
class Webhook extends Base_1.Base {
channel;
raw;
/** The name of the webhook. */
name;
/** The ID of the server the webhook belongs to. */
serverId;
/** The ID of the channel the webhook belongs to. */
channelId;
/** The date the webhook was created. */
createdAt;
/** The ID of the user that created the webhook. */
createdBy;
/** The date the webhook was deleted. */
deletedAt;
/** The token of the webhook. */
token;
/**
* @param channel The channel the webhook belongs to.
* @param raw The raw data of the webhook.
* @param cache Whether to cache the webhook.
*/
constructor(channel, raw, cache = channel.client.options.cacheWebhooks ?? true) {
super(channel.client, raw.id);
this.channel = channel;
this.raw = raw;
this.name = raw.name;
this.serverId = raw.serverId;
this.channelId = raw.channelId;
this.createdAt = new Date(raw.createdAt);
this.createdBy = raw.createdBy;
this.deletedAt = raw.deletedAt ? new Date(raw.deletedAt) : undefined;
this.token = raw.token;
if (cache)
channel.webhooks.cache.set(this.id, this);
}
/** Whether the webhook is cached. */
get isCached() {
return this.channel.webhooks.cache.has(this.id);
}
/** The server the webhook belongs to. */
get server() {
return this.channel.server;
}
/** The timestamp the webhook was created. */
get createdTimestamp() {
return this.createdAt.getTime();
}
/** The server member that created the webhook. */
get author() {
return this.server?.members.cache.get(this.createdBy);
}
/** The ID of the user that created the webhook. */
get authorId() {
return this.createdBy;
}
/** The timestamp the webhook was deleted. */
get deletedTimestamp() {
return this.deletedAt?.getTime();
}
/**
* Fetch the webhook.
* @param options The options to fetch the webhook with.
* @returns The fetched webhook.
* @example webhook.fetch();
*/
fetch(options) {
return this.channel.webhooks.fetch(this, options);
}
/**
* Fetch the server the webhook belongs to.
* @param options The options to fetch the server with.
* @returns The fetched server.
* @example webhook.fetchServer();
*/
fetchServer(options) {
return this.channel.fetchServer(options);
}
/**
* Fetch the server member that created the webhook.
* @param options The options to fetch the server member with.
* @returns The fetched server member.
* @example webhook.fetchAuthor();
*/
async fetchAuthor(options) {
const server = await this.fetchServer();
return server.members.fetch(this.createdBy, options);
}
/**
* Send a message with the webhook.
* @param payload The payload of the message.
* @example webhook.send('Hello world!');
*/
send(payload) {
return this.client.api.webhooks.send(this.id, this.token, payload);
}
/**
* Edit the webhook.
* @param name The name of the webhook.
* @param channelId The ID of the channel the webhook belongs to.
* @returns The edited webhook.
* @example webhook.edit('New name');
*/
edit(name, channelId) {
return this.channel.webhooks.edit(this, name, channelId);
}
/**
* Delete the webhook.
* @returns The deleted webhook.
* @example webhook.delete();
*/
async delete() {
await this.channel.webhooks.delete(this);
return this;
}
}
exports.Webhook = Webhook;
//# sourceMappingURL=Webhook.js.map