UNPKG

@kotori-bot/core

Version:
286 lines (285 loc) 8.39 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 __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var session_exports = {}; __export(session_exports, { Session: () => Session }); module.exports = __toCommonJS(session_exports); var import_types = require("../types"); var import_factory = require("../utils/factory"); var import_error = require("../utils/error"); var import_global = require("../global"); var import_messages = require("./messages"); var import_fast_safe_stringify = __toESM(require("fast-safe-stringify")); class SessionOrigin { /** * Api instance of current session. * * @readonly */ api; /** * Elements instance of current session. * * @readonly */ el; /** * I18n instance of current session. * * @readonly */ i18n; /** * Session unique id, generated base on `type`. `userId`, `groupId`, `guildId`, `channelId`. * * @readonly */ id; /** * Send message to current session. * * @param message - Message to send * @returns Message id and sent time */ async send(message) { if (this.type === import_types.MessageScope.GROUP) { return this.api.sendGroupMsg(message, this.groupId, this.meta); } if (this.type === import_types.MessageScope.CHANNEL) { return this.api.sendChannelMsg(message, this.guildId, this.channelId, this.meta); } if (this.type === import_types.MessageScope.PRIVATE) { return this.api.sendPrivateMsg(message, this.userId, this.meta); } return { messageId: "", time: 0 }; } /** * Format message template with data. * * @param template - Message template * @param data - Data to format * @returns Formatted message */ format; /** * Send message to current session, it's packed base on `session.send()`, `session.i18n` and `session.format()`. * * @param message - Message to send * @returns Message id and sent time * * @async */ async quick(message) { const msg = await message; if (!msg || msg instanceof import_error.CommandError) return; if (msg instanceof import_messages.MessageList && (msg.toString() === "" || msg.pick("text").length === 0)) { this.send(msg); return; } if (Array.isArray(msg) && !(msg instanceof import_messages.MessageList) && msg.length === 2) { this.send(this.format(msg[0], msg[1])); return; } this.send(typeof msg === "string" ? this.i18n.locale(msg) : msg); } /** * Get message from current session. * * @param message - Message to get * @returns Message id and sent time */ async json(message) { if (typeof message === "string") return this.send(message); if (message && typeof message === "object") { const result = (0, import_fast_safe_stringify.default)(message, void 0, 2); return this.send(result === "{}" ? String(message) : result); } if (typeof message === "function") { return this.send( `[${message.toString().slice(0, 5) === "class" ? "class" : "Function"} ${message.name || "(anonymous)"}]` ); } return this.send(String(message)); } /** * Prompt message to current session. * * @param message - Message to prompt * @returns Message from current session * * @async */ prompt(message) { return new Promise((resolve) => { this.api.adapter.ctx[import_global.Symbols.promise].set(this.id, [ ...this.api.adapter.ctx[import_global.Symbols.promise].get(this.id) ?? [], resolve ]); this.quick(message ?? "corei18n.template.prompt").then(() => { }); }).finally(() => this.api.adapter.ctx[import_global.Symbols.promise].delete(this.id)); } /** * Confirm message to current session. * * @param options - Options to confirm * @returns Message from current session * * @async */ confirm(options) { return new Promise((resolve) => { this.api.adapter.ctx[import_global.Symbols.promise].set(this.id, [ ...this.api.adapter.ctx[import_global.Symbols.promise].get(this.id) ?? [], (message) => resolve(message === (options?.sure ?? "corei18n.template.confirm.sure")) ]); this.quick(options?.message ?? "corei18n.template.confirm").then(() => { }); }).finally(() => this.api.adapter.ctx[import_global.Symbols.promise].delete(this.id)); } /** * Create a command error. * * @param type - Error type * @param data - Error data * @returns Command error */ error(type, data) { return new import_error.CommandError(Object.assign(data ?? {}, { type })); } t; /** * Session type * * @readonly */ type; /** * Session time, milliseconds timestamp. * * @readonly */ time; /** * Session related user id if exists. * * @readonly */ userId; /** * Session related operator id if exists. * * @readonly */ operatorId; /** * Session related message id if exists. * * @readonly */ messageId; /** * Session related group id if exists. * * @readonly */ groupId; /** * Session related channel id if exists. * * @readonly */ channelId; /** * Session related guild id if exists. * * @readonly */ guildId; /** * Session related meta data if exists, it is customized by the specific adapter. * * @readonly */ meta; /** * Create a session instance. * * @param data - Session data * @param adapter - Adapter instance * * @constructor */ constructor(data, adapter) { this.api = adapter.api; this.el = adapter.elements; this.i18n = adapter.ctx.i18n.extends(adapter.config.lang); this.type = data.type; this.userId = data.userId; this.groupId = data.groupId; this.operatorId = data.operatorId; this.time = data.time; this.meta = data.meta; switch (this.type) { case import_types.MessageScope.PRIVATE: this.id = `${import_types.MessageScope.PRIVATE}|${this.userId}`; break; case import_types.MessageScope.GROUP: this.id = `${import_types.MessageScope.GROUP}|${this.groupId}|${this.userId}`; break; case import_types.MessageScope.CHANNEL: this.id = `${import_types.MessageScope.CHANNEL}|${this.channelId}|${this.guildId}|${this.userId}`; break; } this.format = (0, import_factory.formatFactory)(this.i18n); this.t = this.i18n.t.bind(this.i18n); } } const Session = new Proxy(SessionOrigin, { construct: (target, args, newTarget) => new Proxy(Reflect.construct(target, args, newTarget), { get: (target2, prop, receiver) => { const result = Reflect.get(target2, prop, receiver); return result === void 0 ? args[0][prop] : result; } }) }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Session });