Skip to content

Commit

Permalink
feat: usage record and ctx.task()
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Jun 5, 2024
1 parent 4b893b8 commit 37a8483
Show file tree
Hide file tree
Showing 72 changed files with 829 additions and 678 deletions.
12 changes: 5 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

## Features

~~- [ ] ctx.filter()~~
~~- [ ] message.schedule()~~
~~- [ ] Module System: load by filename (by import())~~
~~- [ ] @kotori-bot/i18n Node.js env: getLanguage(): LocaleType~~
~~- [ ] Kotori cli gui and modules search,install,uninstall,update~~
<!-- - [x] ctx.filter() -->
<!-- - [ ] Kotori cli gui and modules search,install,uninstall,update -->

- [x] message.schedule()
- [x] decorator plugin form
- [x] @kotori-bot/tools Events: protected list
- [x] Context: parent ctx
Expand All @@ -33,6 +31,6 @@

## Fixes

- [ ] Module Version with core tips
- [ ] Kotori cli start
- [x] Module Version with core tips
- [x] Kotori cli start
- [x] kotori-plugin-request: onGroupMsg and onPrivateMsg
33 changes: 31 additions & 2 deletions modules/core/locales/zh_CN.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
{
"corei18n.template.args_error": "参数 “{0}” 的类型错误,期望类型:{1} 实际类型:{2}",
"corei18n.template.args_few": "输入参数过少请增加参数,期望数量:{0} 实际数量:{1}",
"corei18n.template.args_many": "输入参数过多请减少参数,期望数量:{0} 实际数量:{1}",
"corei18n.template.option_error": "选项 “{0}” 的类型错误,期望类型:{1} 实际类型:{2}",
"corei18n.template.syntax": "指令语法错误,意外地在 {0} 处字符 “{1}”",
"corei18n.template.unknown": "未知的指令 “{0}” ,请检查指令是否输入正确",
"corei18n.template.error": "意外的错误!\n捕获信息:{0}",
"corei18n.template.res_error": "响应数据格式错误!\n错误信息:{0}",
"corei18n.template.num_error": "序号错误,请重新发送",
"corei18n.template.exists": "目标参数“{0}”已存在,请勿重复执行",
"corei18n.template.no_exists": "目标参数“{0}”不存在,请确认后,重新发送",
"corei18n.template.data_error.args": "参数 “{0}” 传入数据错误",
"corei18n.template.data_error.options": "选项 “{0}” 传入数据错误",
"corei18n.template.scope": "该指令仅群聊或私聊下可使用!",
"corei18n.template.no_access_manger": "该指令仅群管理员与群BOT管理员可使用!",
"corei18n.template.no_access_admin": "该指令仅BOT最高管理员可使用!",
"corei18n.template.empty": "",
"corei18n.template.unsupported": "当前平台不支持该消息元素",
"corei18n.template.": "当前平台不支持该消息元素",
"corei18n.template.prompt": "请输入一个值:",
"corei18n.template.confirm": "是否确认?(是/否)",
"corei18n.template.confirm.sure": "",
"core.descr.core": "查看实例统计信息",
"core.descr.bot": "查看当前 bot 信息与运行状态",
"core.descr.bots": "查看所有 bot 信息与运行状态",
"core.descr.about": "查看关于信息",
"core.descr.locale": "设置显示语言",
"core.descr.module": "查看模块",
"core.msg.bot": "ID:%identity%\n语言:%lang%\n平台:%platform%\nid:%self_id%\n连接时间:%create_time%\n接收消息数量:%received_msg%\n发送消息数量:%sent_msg%\n实例下线次数:%offline_times%\n最后消息时间:%last_msg_time%",
"core.msg.core": "全局语言:%lang%\n实例目录:%root%\n运行模式:%mode%\n模块数量:%modules%\n服务数量:%services%\nbot 实例数量:%bots%\n中间件数量:%midwares%\n指令数量:%commands%\n正则数量:%regexps%",
"core.msg.bots": "实例列表:%list%",
"core.msg.bots.list": "\n----------\nID:%identity%\n语言:%lang%\n平台:%platform%\n状态:%status%",
"core.msg.about": "Kotori 版本:%version%\n协议:%license%\nNodeJS 版本:%node_version%"
}
"core.msg.about": "Kotori 版本:%version%\n协议:%license%\nNodeJS 版本:%node_version%",
"core.msg.locale": "成功将当前实例显示语言设置为:%lang%",
"core.msg.locale.global": "成功将全局显示语言设置为:%lang%",
"core.msg.locale.invalid": "参数无效,必须是以下中的一个值:en_US、ja_JP、zh_CN、zh_TW",
"core.msg.module": "模块列表:%list%",
"core.msg.module.list": "\n----------\n名称:%name%\n描述:%description%\n版本:%version%"
}
33 changes: 31 additions & 2 deletions modules/core/locales/zh_TW.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
{
"corei18n.template.args_error": "參數 “{0}” 的類型錯誤,期望類型:{1} 實際類型:{2}",
"corei18n.template.args_few": "輸入參數過少請增加參數,期望數量:{0} 實際數量:{1}",
"corei18n.template.args_many": "輸入參數過多請減少參數,期望數量:{0} 實際數量:{1}",
"corei18n.template.option_error": "選項 “{0}” 的類型錯誤,期望類型:{1} 實際類型:{2}",
"corei18n.template.syntax": "指令語法錯誤,意外地在 {0} 處字符 “{1}”",
"corei18n.template.unknown": "未知的指令 “{0}” ,請檢查指令是否輸入正確",
"corei18n.template.error": "意外的錯誤!\n捕獲信息:{0}",
"corei18n.template.res_error": "響應數據格式錯誤!\n錯誤信息:{0}",
"corei18n.template.num_error": "序號錯誤,請重新發送",
"corei18n.template.exists": "目標參數“{0}”已存在,請勿重複執行",
"corei18n.template.no_exists": "目標參數“{0}”不存在,請確認後,重新發送",
"corei18n.template.data_error.args": "參數 “{0}” 傳入數據錯誤",
"corei18n.template.data_error.options": "選項 “{0}” 傳入數據錯誤",
"corei18n.template.scope": "該指令僅群聊或私聊下可使用!",
"corei18n.template.no_access_manger": "該指令僅群管理員與群BOT管理員可使用!",
"corei18n.template.no_access_admin": "該指令僅BOT最高管理員可使用!",
"corei18n.template.empty": "",
"corei18n.template.unsupported": "當前平台不支持該消息元素",
"corei18n.template.": "當前平台不支持該消息元素",
"corei18n.template.prompt": "請輸入壹個值:",
"corei18n.template.confirm": "是否確認?(是/否)",
"corei18n.template.confirm.sure": "",
"core.descr.core": "查看實例統計信息",
"core.descr.bot": "查看當前 bot 信息與運行狀態",
"core.descr.bots": "查看所有 bot 信息與運行狀態",
"core.descr.about": "查看關于信息",
"core.descr.locale": "設置顯示語言",
"core.descr.module": "查看模塊",
"core.msg.bot": "ID:%identity%\n語言:%lang%\n平台:%platform%\nid:%self_id%\n連接時間:%create_time%\n接收消息數量:%received_msg%\n發送消息數量:%sent_msg%\n實例下線次數:%offline_times%\n最後消息時間:%last_msg_time%",
"core.msg.core": "全局語言:%lang%\n實例目錄:%root%\n運行模式:%mode%\n模塊數量:%modules%\n服務數量:%services%\nbot 實例數量:%bots%\n中間件數量:%midwares%\n指令數量:%commands%\n正則數量:%regexps%",
"core.msg.bots": "實例列表:%list%",
"core.msg.bots.list": "\n----------\nID:%identity%\n語言:%lang%\n平台:%platform%\n狀態:%status%",
"core.msg.about": "Kotori 版本:%version%\n協議:%license%\nNodeJS 版本:%node_version%"
}
"core.msg.about": "Kotori 版本:%version%\n協議:%license%\nNodeJS 版本:%node_version%",
"core.msg.locale": "成功將當前實例顯示語言設置爲:%lang%",
"core.msg.locale.global": "成功將全局顯示語言設置爲:%lang%",
"core.msg.locale.invalid": "參數無效,必須是以下中的壹個值:en_US、ja_JP、zh_CN、zh_TW",
"core.msg.module": "模塊列表:%list%",
"core.msg.module.list": "\n----------\n名稱:%name%\n描述:%description%\n版本:%version%"
}
2 changes: 1 addition & 1 deletion modules/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kotori-bot/kotori-plugin-core",
"version": "1.2.1",
"version": "1.3.0",
"description": "core plugin",
"main": "lib/index.js",
"scripts": {
Expand Down
134 changes: 132 additions & 2 deletions modules/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,112 @@
* @Blog: https://hotaru.icu
* @Date: 2023-07-11 14:18:27
* @LastEditors: Hotaru biyuehuya@gmail.com
* @LastEditTime: 2024-05-03 13:24:43
* @LastEditTime: 2024-06-05 17:25:10
*/

import { Context, Symbols } from 'kotori-bot';
import { CommandAccess, CommandError, Context, MessageScope, TsuError, LocaleType, Symbols } from 'kotori-bot';

export const lang = [__dirname, '../locales'];

export function main(ctx: Context) {
ctx.on('parse', (data) => {
const { quick } = data.session;
if (!(data.result instanceof CommandError)) {
const { scope, access } = data.command.meta;
if (scope && scope !== 'all' && data.session.type !== scope) {
quick('corei18n.template.scope');
data.cancel();
} else if (data.session.userId !== data.session.api.adapter.config.master) {
if (access === CommandAccess.ADMIN) {
quick('corei18n.template.no_access_admin');
data.cancel();
} else if (
access === CommandAccess.MANGER &&
(data.session.type === MessageScope.PRIVATE || data.session.sender.role === 'member')
) {
quick('corei18n.template.no_access_manger');
data.cancel();
}
}
return;
}
data.cancel();
const { value } = data.result;
switch (value.type) {
case 'arg_error':
quick(['corei18n.template.args_error', [value.index, value.expected, value.reality]]);
break;
case 'arg_few':
quick(['corei18n.template.args_few', [value.expected, value.reality]]);
break;
case 'arg_many':
quick(['corei18n.template.args_many', [value.expected, value.reality]]);
break;
case 'option_error':
quick(['corei18n.template.option_error', [value.target, value.expected, value.reality]]);
break;
case 'syntax':
quick(['corei18n.template.syntax', [value.index, value.char]]);
break;
case 'unknown':
quick(['corei18n.template.unknown', [value.input]]);
break;
default:
}
});

ctx.on('command', (data) => {
if (!(data.result instanceof CommandError)) return;
const { value } = data.result;
const { quick } = data.session;
switch (value.type) {
case 'res_error':
quick(['corei18n.template.res_error', [value.error.message]]);
break;
case 'num_error':
quick('corei18n.template.num_error');
break;
case 'no_access_manger':
quick('corei18n.template.no_access_manger');
break;
case 'no_access_admin':
quick('corei18n.template.no_access_admin');
break;
case 'disable':
quick('corei18n.template.disable');
break;
case 'exists':
quick(['corei18n.template.exists', [value.target]]);
break;
case 'no_exists':
quick(['corei18n.template.no_exists', [value.target]]);
break;
case 'error':
ctx.logger.error(value.error);
if (value.error instanceof TsuError) {
quick(['corei18n.template.res_error', [value.error.message]]);
return;
}
if (value.error instanceof Error) {
quick(['corei18n.template.error', [`${value.error.name} ${value.error.message}`]]);
return;
}
if (typeof value.error === 'object') {
quick(['corei18n.template.error', [JSON.stringify(value.error)]]);
return;
}
quick(['corei18n.template.error', [String(value.error)]]);
break;
case 'data_error':
quick([
`corei18n.template.data_error.${typeof value.target === 'string' ? 'options' : 'args'}`,
[value.target]
]);
break;
default:
}
});

ctx.command('core - core.descr.core').action((_, session) => {
const { config, baseDir, options } = session.api.adapter.ctx;
let botsLength = 0;
Expand Down Expand Up @@ -73,4 +171,36 @@ export function main(ctx: Context) {
const { version, license } = session.api.adapter.ctx.pkg;
return ['core.msg.about', { version, license, node_version: process.version }];
});

ctx
.command('locale <lang> - core.descr.locale')
.option('G', 'global:boolean - core.option.locale.global')
.action(({ args: [lang], options: { global } }, session) => {
const { adapter } = session.api;
if (!['en_US', 'ja_JP', 'zh_CN', 'zh_TW'].includes(lang)) return 'core.msg.locale.invalid';
if (global) {
adapter.ctx.root.config.global.lang = lang as LocaleType;
adapter.ctx[Symbols.bot].forEach((apis) =>
apis.forEach((api) => {
const { adapter } = api;
adapter.config.lang = lang as LocaleType;
})
);
} else {
adapter.config.lang = lang as LocaleType;
}
return session.quick([`core.msg.locale${global ? '.global' : ''}`, { lang }]);
});

ctx.command('module - core.descr.module').action((_, session) => {
let list = '';
ctx[Symbols.modules].forEach(([{ pkg }]) => {
list += session.format('core.msg.module.list', {
name: pkg.name,
version: pkg.version,
description: pkg.description
});
});
return ['core.msg.module', { list }];
});
}
4 changes: 3 additions & 1 deletion modules/filter/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# kotori-plugin-filter
# @kotori-bot/kotori-plugin-filter

filter plugin for kotori-bot loader

## Reference

Expand Down
31 changes: 13 additions & 18 deletions modules/filter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
import { Tsu, Context, plugins, Symbols } from 'kotori-bot';
import { Tsu, Context, plugins, Symbols, CORE_MODULES, KotoriPlugin } from 'kotori-bot';
import pm from 'picomatch';

type ModuleMap = [{ pkg: string }, object];

const plugin = plugins([__dirname, '../']);

@plugin.import
export default class Plugin {
private ctx: Context;

private config: Tsu.infer<typeof Plugin.schema>;

/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
class FilterPlugin extends KotoriPlugin<Tsu.infer<typeof FilterPlugin.schema>> {
@plugin.schema
public static schema = Tsu.Object({
mode: Tsu.Union([Tsu.Literal('include'), Tsu.Literal('exclude')]).default('exclude'),
list: Tsu.Array(Tsu.String()).default([])
});

public constructor(ctx: Context, config: Tsu.infer<typeof Plugin.schema>) {
this.ctx = ctx;
this.config = config;
public constructor(ctx: Context, config: Tsu.infer<typeof FilterPlugin.schema>) {
super(ctx, config);
this.loaderFilter();
}

private loaderFilter() {
const { mode, list } = this.config;
const runner = this.ctx.get<{ [Symbols.modules]: Map<string, ModuleMap> }>('runner');
const runner = this.ctx.get<{ [Symbols.modules]: Map<string, [{ main?: string }, object]> }>('runner');
if (!runner) {
this.ctx.logger.error('it is not loader environment nowadays');
return;
}
this.ctx.logger.record(`get loader successfully, filter mode: ${mode}`);
runner[Symbols.modules].forEach((val, key) => {
const value = [runner[Symbols.modules].get(key)];
if (CORE_MODULES.includes(key)) return;
const [ModuleMeta] = val;
const result = list.filter((pattern) => pm(pattern)(key).valueOf()).length > 0;
if (result && (mode === 'exclude' || mode === undefined)) {
delete value[0];
console.log(value[0], value[0] === val);
} else if (!result && mode === 'include') {
delete value[0];
if ((result && mode === 'exclude') || (!result && mode === 'include')) {
runner[Symbols.modules].delete(key);
delete ModuleMeta.main;
}
});
}
}

// plugin.import(Plugin);
2 changes: 1 addition & 1 deletion modules/grouper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kotori-plugin-grouper",
"version": "1.2.1",
"version": "1.2.2",
"description": "groups activity ranking and simple games!",
"main": "lib/index.js",
"keywords": [
Expand Down
Loading

0 comments on commit 37a8483

Please sign in to comment.