UNPKG

heybox-bot

Version:

A heybox chat bot frame

204 lines 10.9 kB
"use strict"; var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.unshift(_); } else if (_ = accept(result)) { if (kind === "field") initializers.unshift(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // 导入所需的工具类和模块 const type_1 = require("./type"); const utils_1 = require("./utils"); const index_1 = require("./index"); const dayjs_1 = __importDefault(require("dayjs")); // 初始化HeyBoxBot实例 const bot = new index_1.HeyBoxBot({ token: 'your token', logLevel: 'info' }); // 定义一个新类MyBot,用于处理各种命令 new ((() => { var _a; let _instanceExtraInitializers = []; let _calc_decorators; let _roll_decorators; let _jrrp_decorators; let _testUser_decorators; let _testImage_decorators; let _testFile_decorators; return _a = class MyBot { /** * 处理/calc命令,执行简单的数学计算 * @param source 命令源 * @param arg0 第一个操作数 * @param arg1 运算符,支持+、-、*、/、^ * @param arg2 可选的第二个操作数,默认为arg0 * @returns 总是返回true,表示命令处理成功 */ calc(source, arg0, arg1, arg2) { // 根据运算符选择对应的计算函数 let calc; switch (arg1) { case '-': calc = (a, b) => a - b; break; case '*': calc = (a, b) => a * b; break; case '/': calc = (a, b) => { if (b === 0) { throw new Error('除数不能为零'); } return a / b; }; break; case '^': calc = (a, b) => Math.pow(a, b); break; default: calc = (a, b) => a + b; break; } // 执行计算并返回结果 try { source.success(`${arg0} ${arg1} ${arg2 || arg0} = ${calc(arg0, arg2 === undefined ? arg0 : arg2)}`); } catch (e) { source.fail(`计算出错,${(e === null || e === void 0 ? void 0 : e.message) || '未知错误'}`); } return true; } /** * 处理/roll命令,模拟掷骰子 * @param source 命令源 * @param arg0 骰子的最大点数 * @returns 总是返回true,表示命令处理成功 */ roll(source, arg0) { // 生成随机骰子点数并返回 source.success(`${arg0}点骰子: ${Math.floor(Math.random() * arg0) + 1}`); return true; } /** * 处理/jrrp命令,返回用户今天的“人品值” * @param source 命令源 * @returns 总是返回true,表示命令处理成功 */ jrrp(source) { // 根据当前日期和用户名称生成种子,以确保每天每个用户的“人品值”是固定的 const rp = new utils_1.SeededRandom(utils_1.Util.hashDJB2((0, dayjs_1.default)().format('YYYY-MM-DD') + source.getName())).nextInt(0, 100); if (source instanceof type_1.WSMsgImpl) { const wsMsgImpl = source; source.successBy(builder => { builder .card() .header('今日人品值') .divider() .textWithImage(`${rp}`, wsMsgImpl.user_info.avatar, 'left') .buttons({ text: `${rp > 70 ? '好耶!' : rp > 40 ? '一般' : '6'}` }); }); } else { source.success(`您今日的人品值为:${rp}`); } return true; } /** * 测试用户命令处理器 * * 当用户发送 "/testuser {用户}" 命令时,此方法会被调用它将用户信息以JSON格式返回给命令发送者 * 主要用于演示如何处理和响应包含用户的命令 * * @param source 命令的来源,用于发送反馈信息 * @param user 命令中指定的用户对象,包含用户的相关信息 * @returns 总是返回true,表示命令处理成功 */ testUser(source, user) { source.success(JSON.stringify(user)); return true; } /** * 测试图片命令处理器 * * 当用户发送 "/testimg {图片}" 命令时,此方法会被调用它将图片信息以JSON格式返回给命令发送者 * 主要用于演示如何处理和响应包含图片的命令 * * @param source 命令的来源,用于发送反馈信息 * @param img 命令中指定的图片对象,包含图片的相关信息 * @returns 总是返回true,表示命令处理成功 */ testImage(source, img) { source.success(JSON.stringify(img)); return true; } /** * 测试文件命令处理器 * * 当用户发送 "/testfile {文件}" 命令时,此方法会被调用它将文件信息以JSON格式返回给命令发送者 * 主要用于演示如何处理和响应包含文件的命令 * * @param source 命令的来源,用于发送反馈信息 * @param file 命令中指定的文件对象,包含文件的相关信息 * @returns 总是返回true,表示命令处理成功 */ testFile(source, file) { source.success(JSON.stringify(file)); return true; } constructor() { __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; _calc_decorators = [bot.command('/calc {arg0: NUMBER} {arg1: +|-|*|/|^} {arg2?: NUMBER}')]; _roll_decorators = [bot.command('/roll {arg0: NUMBER}')]; _jrrp_decorators = [bot.command('/jrrp')]; _testUser_decorators = [bot.command('/testuser {user: USER}')]; _testImage_decorators = [bot.command('/testimg {img: IMAGE}')]; _testFile_decorators = [bot.command('/testfile {file: FILE}')]; __esDecorate(_a, null, _calc_decorators, { kind: "method", name: "calc", static: false, private: false, access: { has: obj => "calc" in obj, get: obj => obj.calc }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _roll_decorators, { kind: "method", name: "roll", static: false, private: false, access: { has: obj => "roll" in obj, get: obj => obj.roll }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _jrrp_decorators, { kind: "method", name: "jrrp", static: false, private: false, access: { has: obj => "jrrp" in obj, get: obj => obj.jrrp }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _testUser_decorators, { kind: "method", name: "testUser", static: false, private: false, access: { has: obj => "testUser" in obj, get: obj => obj.testUser }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _testImage_decorators, { kind: "method", name: "testImage", static: false, private: false, access: { has: obj => "testImage" in obj, get: obj => obj.testImage }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _testFile_decorators, { kind: "method", name: "testFile", static: false, private: false, access: { has: obj => "testFile" in obj, get: obj => obj.testFile }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })())(); // 启动Bot实例 bot.start().then(); //# sourceMappingURL=example.js.map