UNPKG

@kotori-bot/core

Version:
217 lines (216 loc) 6.04 kB
/** * @Package @kotori-bot/core * @Version 1.7.2 * @Author Arimura Sena <me@hotaru.icu> * @Copyright 2024-2025 Hotaru. All rights reserved. * @License GPL-3.0 * @Link https://github.com/kotorijs/kotori * @Date 2026/2/14 15:50:13 */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var messages_exports = {}; __export(messages_exports, { MessageList: () => MessageList, MessageSingle: () => MessageSingle, Messages: () => Messages, default: () => messages_default }); module.exports = __toCommonJS(messages_exports); class MessageSingle extends String { /** * Message data. * * @readonly */ data; /** * Create a single message * * @param type - Message type * @param data - Message data */ constructor(type, data) { super(Messages.stringify(type, data, false)); this.data = { type, ...data }; } /** * Convert message to string. * * @param isStrict - Whether to strictly convert message, strict convert will ignore the elements else text * @returns Message string */ toString(isStrict = true) { return Messages.stringify(this.data.type, this.data, isStrict); } /** * Check whether the message is text. * * @returns Whether the message is text */ isText() { return this.data.type === "text"; } } class MessageListOrigin extends Array { /** * Create a message list. * * @param messages - Message list */ constructor(...messages) { const handleMessage = []; for (const value of messages) { if (typeof value === "string") handleMessage.push(Messages.text(value)); else handleMessage.push(...value instanceof MessageListOrigin ? [...value] : [value]); } super(...handleMessage); Object.setPrototypeOf(this, MessageList.prototype); } /** * Convert message list to string. * * @param isStrict - Whether to strictly convert message, strict convert will ignore the elements else text * @returns Message string */ toString(isStrict = true) { return Array.from(this).map((value) => value.toString(isStrict)).join(""); } /** * Check whether the message list is pure. * * @param keys - Message type list * * @returns Whether the message list is pure */ isPure(...keys) { return this.every((value) => keys.includes(value.data.type)); } /** * Check whether the message list is text. * * @returns Whether the message list is text */ isText() { return this.isPure("text"); } /** * Pick message list. * * @param keys - Message type list * @returns Message list */ pick(...keys) { return Messages( ...this.filter((value) => keys.includes(value.data.type)) ); } /** * Omit message list. * * @param keys - Message type list * @returns Message list */ omit(...keys) { return Messages( ...this.filter((value) => !keys.includes(value.data.type)) ); } } const MessageList = new Proxy(MessageListOrigin, { construct: (target, argArray, newTarget) => new Proxy(Reflect.construct(target, argArray, newTarget), { get: (target2, prop, receiver) => { if (prop in target2) return Reflect.get(target2, prop, receiver); const func = String.prototype[target2]; if (func instanceof Function) func.bind(void 0); return void 0; } }) }); function Messages(...messages) { return new MessageList(...messages); } ((Messages2) => { function text(text2) { return new MessageSingle("text", { text: text2 }); } Messages2.text = text; function mention(userId) { return new MessageSingle("mention", { userId }); } Messages2.mention = mention; function mentionAll() { return new MessageSingle("mentionAll", {}); } Messages2.mentionAll = mentionAll; function image(content) { return new MessageSingle("image", { content }); } Messages2.image = image; function voice(content) { return new MessageSingle("voice", { content }); } Messages2.voice = voice; function audio(content) { return new MessageSingle("audio", { content }); } Messages2.audio = audio; function video(content) { return new MessageSingle("video", { content }); } Messages2.video = video; function file(content) { return new MessageSingle("file", { content }); } Messages2.file = file; function location(latitude, longitude, title, content) { return new MessageSingle("location", { latitude, longitude, title, content }); } Messages2.location = location; function reply(messageId) { return new MessageSingle("reply", { messageId }); } Messages2.reply = reply; function stringify(type, data, isStrict = true) { if (isStrict) { if (type === "text") return data.text; return ""; } switch (type) { case "text": return data.text; case "mention": return `@${data.userId}`; case "mentionAll": return "@all"; case "location": return "[Location]"; default: return `[${type.charAt(0).toUpperCase()}${type.slice(1)}]`; } } Messages2.stringify = stringify; })(Messages || (Messages = {})); var messages_default = Messages; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MessageList, MessageSingle, Messages });