UNPKG

koishi-plugin-eula

Version:

EULA(End-user licence agreement) for your koishi bot.

107 lines (103 loc) 5.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("@satorijs/element/jsx-runtime"); const koishi_1 = require("koishi"); class Eula extends koishi_1.Service { constructor(ctx, configs) { super(ctx, 'eula', true); this.configs = configs; this.filter = false; this.usage = Eula.usage; ctx.i18n.define('zh', require('./locales/zh')); ctx.i18n.define('en', require('./locales/en')); this.log = ctx.logger('eula'); ctx.model.extend('user', { eula: { type: 'boolean', initial: false } }); ctx.before('attach-user', (session, fields) => { fields.add('eula'); fields.add('authority'); }); ctx.before('command/execute', async (_) => { const session = _.session; const authority = session.resolve(session.argv.command.config.authority); ctx.emit('eula/before', _); if (!session.user.eula && authority > 0 && session.user.authority <= configs.replyAuthority && _.command.name !== 'eula') if (configs.enable || (!configs.model && !configs.commands.includes(_.command.name)) || (configs.model && configs.commands.includes(_.command.name))) return await this.eula(_); }); ctx.command('eula', '最终用户许可协议', { authority: 0, }) .action((_) => this.eula(_)); } async eula(argv) { const session = argv.session; const userLocale = session.user.locale || session.user.locales[0] || 'zh'; const accept = koishi_1.Random.pick(this.configs.accept) ?? session.text('eula.defaultAccept'); await session.send((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("message", { id: "{0}", children: (0, jsx_runtime_1.jsx)("i18n", { path: "eula.eulaMessage.title", children: [this.configs.alias] }) }), (0, jsx_runtime_1.jsxs)("message", { forward: this.configs.forwardMessgae, children: [(0, jsx_runtime_1.jsx)("message", { id: "{0}", children: this.configs.content[userLocale] ?? this.configs.content['zh'] }), (0, jsx_runtime_1.jsx)("message", { id: "{0}", children: (0, jsx_runtime_1.jsx)("i18n", { path: "eula.eulaMessage.confirm", children: [accept] }) })] })] })); const prompt = await session.prompt(this.configs.waitTime * 1000); this.log.info(`[platfrom: ${session.platform}]user ${session.userId} reply to (${prompt}) eula`); if (prompt) { const promptEle = koishi_1.h.parse(prompt); if (promptEle[0].type === 'at' && promptEle[0].attrs.id === session.bot.selfId) promptEle.shift(); // remove `at` element const accredit = promptEle[0].attrs.content.replace(/^\//g, '').trim() === accept; session.user.eula = accredit; this.ctx.emit('eula/update', session, accredit); return session.text(`${accredit ? 'eula.acceptedMessage' : 'eula.rejectMessage'}`, [this.configs.alias]); } else return session.text('eula.timeout'); } /** * * @param userId 用户 id,即 session.user.id * @returns false: 该用户未同意 eula,true: 该用户已同意 eula */ async vertify(userId) { return (await this.ctx.database.get('user', { id: userId }, ['eula']))[0].eula; } } (function (Eula) { Eula.usage = ` ## 注意事项 本插件只用于体现 Koishi 部署者意志,即:“部署者仅对同意了《最终用户协议》的最终用户提供服务”。 对于部署者行为及所产生的任何纠纷, Koishi 及 koishi-plugin-eula 概不负责。 协议内容文本可以在 <a href="/locales/eula/eulaMessage">本地化 - eula.eulaMessage</a> 中修改,因此你可以根据不同语言给予不同的协议文本。 `; Eula.Config = koishi_1.Schema.intersect([ koishi_1.Schema.object({ waitTime: koishi_1.Schema.number().min(30).max(300).step(1).default(60).description('等待回复时长(秒)'), replyAuthority: koishi_1.Schema.number().min(1).max(5).default(1).description('协议生效最高等级'), forwardMessgae: koishi_1.Schema.boolean().default(true).description('合并发送的协议以及认可段(目前仅在 QQ 生效)') }).description('基本设置'), koishi_1.Schema.object({ alias: koishi_1.Schema.string().default('EULA').description('《最终用户许可协议》别名,或其他自拟协议名称'), content: koishi_1.Schema.dict(koishi_1.Schema.string().role('textarea', { rows: [2, 4] })).default({ 'zh': '协议内容', }).description('协议内容的多语言设置'), accept: koishi_1.Schema.array(String).description('认可协议关键字,如果有多个,则随机某一个作为认可关键字') }).description('协议设置'), koishi_1.Schema.intersect([ koishi_1.Schema.object({ enable: koishi_1.Schema.boolean().default(true).description('限制所有的命令') }), koishi_1.Schema.union([ koishi_1.Schema.object({ enable: koishi_1.Schema.const(false).required(), model: koishi_1.Schema.union([ koishi_1.Schema.const(true).description('白名单'), koishi_1.Schema.const(false).description('黑名单'), ]).description('限制命令的模式'), commands: koishi_1.Schema.array(String).description('指令列表') }), koishi_1.Schema.object({}) ]) ]).description('指令限制') ]); })(Eula || (Eula = {})); exports.default = Eula;