UNPKG

darkcord

Version:

A NodeJS Package to interact with Discord API

263 lines (262 loc) 7.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = exports.MessageFlags = void 0; const Resolvable_1 = require("../utils/Resolvable"); const v10_1 = require("discord-api-types/v10"); const DataManager_1 = require("../manager/DataManager"); const Base_1 = require("./Base"); const BitField_1 = require("./BitField"); const Emoji_1 = require("./Emoji"); class MessageFlags extends BitField_1.BitField { constructor(flags) { super(flags, v10_1.MessageFlags); } static Flags = v10_1.MessageFlags; } exports.MessageFlags = MessageFlags; class Message extends Base_1.Base { /** * ID of the channel the message was sent in */ channelId; /** * The channel the message was sent in */ channel; /** * When this message was sent */ timestamp; /** * Contents of the message */ content; /** * Whether this message mentions everyone */ mentionEveryone; /** * Any embedded content */ embeds; /** * Any attached files */ attachments; /** * The message associated with the message_reference */ referencedMessage; /** * If the message is generated by a webhook, this is the webhook's id */ webhookId; /** * A nonce that can be used for optimistic message sending (up to 25 characters) */ nonce; /** * The author of this message (only a valid user in the case where the message is generated by a user or bot user) */ user; /** * Type of message */ type; /** * A generally increasing integer (there may be gaps or duplicates) that represents the approximate position of the message in a thread * * it can be used to estimate the relative position of the message in a thread in company with total_message_sent on parent thread */ position; /** * Message flags combined as a bitfield */ flags; /** * When this message was edited (or null if never) */ editedTimestamp; /** * Sent if the message contains stickers */ stickerItems; /** * Guild was message has sent */ guild; /** * This message has resolved */ isResolved; /** * Reactions in this message */ reactions; /** * Id of guild was message has sent */ guildId; /** * The member of this message (only received in guild) */ member; constructor(data, guild) { super(data, data.client); this.isResolved = false; this.channelId = data.channel_id; this.timestamp = Date.parse(data.timestamp); this.editedTimestamp = null; this.referencedMessage = data.referenced_message ? Resolvable_1.Resolvable.resolveMessage(new Message({ ...data.referenced_message, client: this._client, }, this.guild), this._client) : null; this.webhookId = data.webhook_id; this.nonce = data.nonce; this.user = data.client.users.add(data.author); this.member = null; this.type = data.type; this.guild = guild ?? null; this.guildId = guild?.id; this.reactions = new DataManager_1.DataCache(); if (this.guild) { this.member = this.guild.members.cache.get(this.user?.id) ?? null; } this._update(data); } /** * Reply this message * @param content The content to reply * @returns */ async reply(content) { if (this.type !== v10_1.MessageType.Reply && this.type !== v10_1.MessageType.Default) { return; } if (typeof content === "string") { content = { content, }; } if (!content.message_reference) { content.message_reference = { message_id: this.id, }; } content.message_reference.channel_id = this.channelId; content.message_reference.message_id = this.id; if (!this.isResolved) { throw new Error("Message not resolved"); } return this.channel?.createMessage(content); } /** * Create's a reaction in message * @param emoji Emoji to create te reactions * @returns */ async createReaction(emoji) { if (emoji instanceof Emoji_1.Emoji) { emoji = emoji.uriComponent; } else { emoji = Emoji_1.Emoji.getEncodedURI(emoji); } let rawReaction = (await this._client.rest.createReaction(this.channelId, this.id, emoji)); const reaction = new Emoji_1.Reaction({ ...rawReaction, client: this._client, message_id: this.id, channel_id: this.channel?.id, }); return this.reactions._add(reaction, true, reaction.emoji?.id ?? reaction.emoji?.name); } /** * Delete the message * @param reason Reason to delete message * @returns */ delete(reason) { if (!this.isResolved) { throw new Error("Message not resolved"); } return this.channel?.deleteMessage(this.id, reason); } /** * Edit the message * @param content The new content of message * @returns */ async edit(content) { const data = (await this._client.rest.editMessage(this.channelId, this.id, content)); return this._update(data); } /** * Fetch's the webhook of message * @returns */ fetchWebhook() { if (this.channel?.isGuildText() && this.webhookId) { return this.channel.fetchWebhook(this.webhookId); } return null; } _update(data) { if ("content" in data) this.content = data.content; if ("mention_everyone" in data) this.mentionEveryone = data.mention_everyone; if ("embeds" in data) this.embeds = data.embeds; if ("attachments" in data) this.attachments = data.attachments; if ("position" in data) this.position = data.position ?? 0; if ("flags" in data && data.flags) this.flags = new MessageFlags(data.flags); if ("edited_timestamp" in data && data.edited_timestamp) this.editedTimestamp = Date.parse(data.edited_timestamp); if ("sticker_items" in data) this.stickerItems = data.sticker_items; this.channel ??= this._client.channels.cache.get(data.channel_id); if (Array.isArray(data.reactions)) { for (const reaction of data.reactions) { const resolved = new Emoji_1.Reaction({ ...reaction, client: this._client }); this.reactions._add(resolved, true, resolved.emoji.id ?? resolved.emoji.name); } } this.rawData = Object.assign({}, data, this.rawData); return this; } toJSON() { return Base_1.Base.toJSON(this, [ "attachments", "channel", "channelId", "content", "createdAt", "editedTimestamp", "embeds", "flags", "guild", "guildId", "id", "isResolved", "mentionEveryone", "nonce", "position", "rawData", "reactions", "referencedMessage", "stickerItems", "timestamp", "editedTimestamp", "type", "user", "webhookId", ]); } } exports.Message = Message;