UNPKG

seyfert

Version:

The most advanced framework for discord bots

206 lines (205 loc) 6.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InMessageEmbed = exports.WebhookMessage = exports.Message = exports.BaseMessage = void 0; const __1 = require(".."); const transformers_1 = require("../client/transformers"); const common_1 = require("../common"); const DiscordBase_1 = require("./extra/DiscordBase"); class BaseMessage extends DiscordBase_1.DiscordBase { embeds; constructor(client, data) { super(client, data); this.mentions = { roles: data.mention_roles ?? [], channels: data.mention_channels ?? [], users: [], }; this.components = data.components?.map(__1.componentFactory) ?? []; this.embeds = data.embeds.map(embed => new InMessageEmbed(embed)); this.patch(data); } get user() { return this.author; } createComponentCollector(options) { return this.client.components.createComponentCollector(this.id, this.channelId, this.guildId, options); } get url() { return common_1.Formatter.messageLink(this.guildId ?? '@me', this.channelId, this.id); } guild(mode = 'flow') { if (!this.guildId) return (mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve()); switch (mode) { case 'cache': return (this.client.cache.guilds?.get(this.guildId) || (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined)); default: return this.client.guilds.fetch(this.guildId, mode === 'rest'); } } channel(mode = 'flow') { switch (mode) { case 'cache': return (this.client.cache.channels?.get(this.channelId) || (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined)); default: return this.client.channels.fetch(this.channelId, mode === 'rest'); } } react(emoji) { return this.client.reactions.add(this.id, this.channelId, emoji); } patch(data) { if ('timestamp' in data && data.timestamp) { this.timestamp = Date.parse(data.timestamp); } if ('author' in data && data.author) { this.author = transformers_1.Transformers.User(this.client, data.author); } if ('member' in data && data.member) { this.member = transformers_1.Transformers.GuildMember(this.client, data.member, data.author, this.guildId); } if (data.mentions?.length) { this.mentions.users = this.guildId ? data.mentions.map(m => transformers_1.Transformers.GuildMember(this.client, { ...m.member, user: m, }, m, this.guildId)) : data.mentions.map(u => transformers_1.Transformers.User(this.client, u)); } if (data.poll) { this.poll = transformers_1.Transformers.Poll(this.client, data.poll, this.channelId, this.id); } } } exports.BaseMessage = BaseMessage; class Message extends BaseMessage { constructor(client, data) { super(client, data); } fetch(force = false) { return this.client.messages.fetch(this.id, this.channelId, force); } reply(body, fail = true) { return this.write({ ...body, message_reference: { message_id: this.id, channel_id: this.channelId, guild_id: this.guildId, fail_if_not_exists: fail, }, }); } edit(body) { return this.client.messages.edit(this.id, this.channelId, body); } write(body) { return this.client.messages.write(this.channelId, body); } delete(reason) { return this.client.messages.delete(this.id, this.channelId, reason); } crosspost(reason) { return this.client.messages.crosspost(this.id, this.channelId, reason); } } exports.Message = Message; class WebhookMessage extends BaseMessage { webhookId; webhookToken; constructor(client, data, webhookId, webhookToken) { super(client, data); this.webhookId = webhookId; this.webhookToken = webhookToken; } fetchWebhook() { return this.client.webhooks.fetch(this.webhookId, this.webhookToken); } fetch() { return this.client.webhooks.fetchMessage({ messageId: this.id, webhookId: this.webhookId, token: this.webhookToken, query: this.thread?.id ? { thread_id: this.thread.id } : undefined, }); } edit(body) { const { query, ...rest } = body; return this.client.webhooks.editMessage(this.webhookId, this.webhookToken, { body: rest, query, messageId: this.id, }); } write(body) { const { query, ...rest } = body; return this.client.webhooks.writeMessage(this.webhookId, this.webhookToken, { body: rest, query, }); } delete(reason) { return this.client.webhooks.deleteMessage({ webhookId: this.webhookId, token: this.webhookToken, messageId: this.id, reason, query: this.thread?.id ? { thread_id: this.thread.id } : undefined, }); } } exports.WebhookMessage = WebhookMessage; class InMessageEmbed { data; constructor(data) { this.data = data; } get title() { return this.data.title; } get type() { return this.data.type; } get description() { return this.data.description; } get url() { return this.data.url; } get timestamp() { return this.data.timestamp; } get color() { return this.data.color; } get footer() { return this.data.footer ? (0, common_1.toCamelCase)(this.data.footer) : undefined; } get image() { return this.data.image ? (0, common_1.toCamelCase)(this.data.image) : undefined; } get thumbnail() { return this.data.thumbnail ? (0, common_1.toCamelCase)(this.data.thumbnail) : undefined; } get video() { return this.data.video ? (0, common_1.toCamelCase)(this.data.video) : undefined; } get provider() { return this.data.provider; } get author() { return this.data.author ? (0, common_1.toCamelCase)(this.data.author) : undefined; } get fields() { return this.data.fields; } toBuilder() { return new __1.Embed(this.data); } toJSON() { return { ...this.data }; } } exports.InMessageEmbed = InMessageEmbed;