UNPKG

guilded.ts

Version:

A powerful NPM module that allows you to easily interact with the Guilded API.

75 lines 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageManager = void 0; const BaseManager_1 = require("../BaseManager"); const Message_1 = require("../../structures/message/Message"); const collection_1 = require("@discordjs/collection"); /** * The manager of messages that belong to a chat based channel. * @example new MessageManager(channel); */ class MessageManager extends BaseManager_1.BaseManager { channel; /** @param channel The chat based channel the messages belong to. */ constructor(channel) { super(channel.client, channel.client.options.maxMessageCache); this.channel = channel; } fetch(arg1, arg2) { if (typeof arg1 === 'string' || arg1 instanceof Message_1.Message) return this.fetchSingle(arg1, arg2); return this.fetchMany(arg1); } /** @ignore */ async fetchSingle(message, options) { message = message instanceof Message_1.Message ? message.id : message; const cached = this.cache.get(message); if (cached && !options?.force) return cached; const raw = await this.client.api.messages.fetch(this.channel.id, message); return new Message_1.Message(this.channel, raw, options?.cache); } /** @ignore */ async fetchMany(options) { const raw = await this.client.api.messages.fetch(this.channel.id, options); const messages = new collection_1.Collection(); for (const data of raw) { const message = new Message_1.Message(this.channel, data, options?.cache); messages.set(message.id, message); } return messages; } /** * Create a message in the chat based channel. * @param payload The payload of the message. * @returns The created message. * @example messages.create('Hello world!'); */ async create(payload) { const raw = await this.client.api.messages.create(this.channel.id, payload); return new Message_1.Message(this.channel, raw); } /** * Edit a message in the chat based channel. * @param message The message to edit. * @param payload The payload of the message. * @returns The edited message. * @example messages.edit(message, 'Hello world!'); */ async edit(message, payload) { message = message instanceof Message_1.Message ? message.id : message; const raw = await this.client.api.messages.edit(this.channel.id, message, payload); return new Message_1.Message(this.channel, raw); } /** * Delete a message in the chat based channel. * @param message The message to delete. * @example messages.delete(message); */ delete(message) { message = message instanceof Message_1.Message ? message.id : message; return this.client.api.messages.delete(this.channel.id, message); } } exports.MessageManager = MessageManager; //# sourceMappingURL=MessageManager.js.map