Skip to content

Commit

Permalink
refactory: modules use and services,database
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Jan 1, 2024
1 parent 8a347ec commit 6df9f60
Show file tree
Hide file tree
Showing 38 changed files with 1,344 additions and 905 deletions.
8 changes: 4 additions & 4 deletions modules/adapter-cmd/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:09
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2023-12-02 22:54:34
* @LastEditTime: 2024-01-01 16:44:32
*/
import { Adapter, AdapterConfig, Context, Tsu, eventDataTargetIdSchema } from 'kotori-bot';
import CmdApi from './api';
Expand All @@ -25,7 +25,7 @@ export class CmdAdapter extends Adapter<CmdApi> {
public config: CmdConfig;

public constructor(ctx: Context, config: CmdConfig, identity: string) {
super(ctx, config, identity, CmdApi, CmdElements);
super(ctx, config, identity, CmdApi, new CmdElements());
this.config = config;
this.selfId = config['self-id'];
process.stdin.on('data', data => this.handle(data));
Expand All @@ -52,7 +52,7 @@ export class CmdAdapter extends Adapter<CmdApi> {

public start() {
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: true,
info: `start cmd-line listen`,
});
Expand All @@ -61,7 +61,7 @@ export class CmdAdapter extends Adapter<CmdApi> {

public stop() {
this.ctx.emit('disconnect', {
adapter: this,
service: this,
normal: true,
info: `stop cmd-line listen`,
});
Expand Down
42 changes: 26 additions & 16 deletions modules/adapter-cmd/src/elements.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import { ElementsParam, none } from 'kotori-bot';
import { Elements, EventDataTargetId, none } from 'kotori-bot';

export const CmdElements: ElementsParam = {
at(target) {
export class CmdElements extends Elements {
public at(target: EventDataTargetId) {
none(this);
return `@${target} `;
},
image(url) {
}

public image(url: string) {
none(this);
return `[image,${url}]`;
},
voice(url) {
}

public voice(url: string) {
none(this);
return `[voice,${url}]`;
},
video(url) {
}

public video(url: string) {
none(this);
return `[video,${url}]`;
},
face(id) {
}

public face(id: string) {
none(this);
return `[face,${id}]`;
},
file(data) {
none(data);
}

public file(data: string) {
none(data, this);
return `[file]`;
},
};
}
}

export default CmdElements;
20 changes: 9 additions & 11 deletions modules/adapter-onebot/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:09
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2023-12-02 22:54:59
* @LastEditTime: 2024-01-01 17:06:21
*/
import { Adapter, AdapterConfig, Context, EventDataApiBase, EventDataTargetId, Tsu } from 'kotori-bot';
import WebSocket from 'ws';
Expand Down Expand Up @@ -48,7 +48,7 @@ export class OneBotAdapter extends Adapter {
public readonly config: OneBotConfig;

public constructor(ctx: Context, config: OneBotConfig, identity: string) {
super(ctx, config, identity, OneBotApi, OneBotElements);
super(ctx, config, identity, OneBotApi, new OneBotElements());
this.config = config;
this.info = `${this.config.address}:${this.config.port}`;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ export class OneBotAdapter extends Adapter {
public start() {
if (this.config.mode === 'ws-reverse') {
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: true,
info: `start wsserver at ${this.info}`,
onlyStart: true,
Expand All @@ -172,7 +172,7 @@ export class OneBotAdapter extends Adapter {

public stop() {
this.ctx.emit('disconnect', {
adapter: this,
service: this,
normal: true,
info: this.config.mode === 'ws' ? `disconnect from ${this.info}` : `stop wsserver at ${this.info}`,
});
Expand All @@ -192,13 +192,13 @@ export class OneBotAdapter extends Adapter {
const { 0: socket } = wss;
this.socket = socket;
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: true,
info: `client connect to ${this.info}`,
});
this.socket?.on('close', () => {
this.ctx.emit('disconnect', {
adapter: this,
service: this,
normal: false,
info: `unexpected client disconnect from ${this.info}`,
});
Expand All @@ -207,22 +207,22 @@ export class OneBotAdapter extends Adapter {
});
} else {
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: true,
info: `connect server to ${this.info}`,
});
this.socket = new WebSocket(`${this.info}`);
this.socket.on('close', () => {
this.ctx.emit('disconnect', {
adapter: this,
service: this,
normal: false,
info: `unexpected disconnect server from ${this.info}, will reconnect in ${this.config.retry} seconds`,
});
setTimeout(() => {
if (!this.socket) return;
this.socket.close();
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: false,
info: `reconnect server to ${this.info}`,
});
Expand All @@ -236,5 +236,3 @@ export class OneBotAdapter extends Adapter {
/* global NodeJS */
private onlineTimerId: NodeJS.Timeout | null = null;
}

export default OneBotAdapter;
75 changes: 28 additions & 47 deletions modules/adapter-onebot/src/elements.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
import { ElementsParam } from 'kotori-bot';

/*
function cq(type: MessageCqType, data: obj<string|number>) {
return `[CQ:${type},data]`
}
const image = ;
const at = (qq: EventDataTargetId) => `[CQ:at,qq=${qq}]`;
const poke = (qq: EventDataTargetId) => `[CQ:poke,qq=${qq}]`;
interface OneBotApiExtra {
type: 'onebot';
image: typeof image;
at: typeof at;
poke: typeof poke;
}
declare module 'kotori-bot' {
interface ApiExtra {
onebot: OneBotApiExtra;
}
import { Elements, EventDataTargetId, none } from 'kotori-bot';
import { MessageCqType } from './types';

export class OneBotElements extends Elements {
public cq(type: MessageCqType, data: string) {
none(this);
return `[CQ:${type},${data}]`;
}

public at(target: EventDataTargetId) {
return this.cq('at', `qq=${target}`);
}

public image(url: string) {
return this.cq('image', `file=${url},cache=0`);
}

public voice(url: string) {
return this.cq('record', `file=${url}`);
}

public video(url: string) {
return this.cq('video', `file=${url}`);
}

public face(id: number) {
return this.cq('face', `id=${id}`);
}
}
*/

export const OneBotElements: ElementsParam = {
at(target) {
return `[CQ:at,qq=${target}]`;
},
image(url) {
return `[CQ:image,file=${url},cache=0]`;
},
voice(url) {
return `[CQ:voice,file=${url}]`;
},
video(url) {
return `[CQ:video,file=${url}]`;
},
face(id) {
return `[CQ:face,id=${id}]`;
},
file(data) {
return `[CQ:file,file=${data}]`;
},
};

export default OneBotElements;
14 changes: 7 additions & 7 deletions modules/adapter-qq/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:09
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2023-12-22 22:37:29
* @LastEditTime: 2024-01-01 16:43:54
*/
import { Adapter, AdapterConfig, Context, Tsu, obj } from 'kotori-bot';
import WebSocket from 'ws';
Expand Down Expand Up @@ -46,14 +46,14 @@ export class QQAdapter extends Adapter<QQApi> {
},
});
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: true,
onlyStart: true,
info: `logging in qqbot...`,
});
} else if (data.t === 'READY') {
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: true,
info: `logged in qqbot successfully`,
});
Expand Down Expand Up @@ -86,7 +86,7 @@ export class QQAdapter extends Adapter<QQApi> {

public stop() {
this.ctx.emit('disconnect', {
adapter: this,
service: this,
normal: true,
info: `disconnect from ${WS_ADDRESS}`,
});
Expand Down Expand Up @@ -127,15 +127,15 @@ export class QQAdapter extends Adapter<QQApi> {
this.socket = new WebSocket(WS_ADDRESS);
this.socket.on('close', () => {
this.ctx.emit('disconnect', {
adapter: this,
service: this,
normal: false,
info: `unexpected disconnect server from ${WS_ADDRESS}, will reconnect in ${this.config.retry} seconds`,
});
setTimeout(() => {
if (!this.socket) return;
this.socket.close();
this.ctx.emit('connect', {
adapter: this,
service: this,
normal: false,
info: `reconnect server to ${WS_ADDRESS}`,
});
Expand All @@ -154,7 +154,7 @@ export class QQAdapter extends Adapter<QQApi> {
this.offline();

this.ctx.emit('disconnect', {
adapter: this,
service: this,
normal: false,
info: `got token error!`,
});
Expand Down
18 changes: 18 additions & 0 deletions modules/core/locales/zh_CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"core.descr.core": "查看实例统计信息",
"core.descr.bot": "查看当前 bot 信息与运行状态",
"core.descr.bots": "查看所有 bot 信息与运行状态",
"core.descr.version": "查看版本信息",
"core.descr.about": "帮助信息",
"core.descr.update": "检查更新",
"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.version": "Kotori 版本:%version%\n协议:%license%\nNodeJS 版本:%node_version%",
"core.msg.about": "kotori 是一个跨平台、解耦合、现代化于一体的 ChatBot 聊天机器人 框架,运行于 NodeJS 环境并使用 TypeScript 语言开发。\n官网:kotori.js.org",
"core.msg.update": "当前版本:%version%\n%content%",
"core.msg.update.yes": "当前为最新版本!",
"core.msg.update.no": "检测到有更新!\n最新版本: %version%\n请前往Github仓库获取最新版本:%repo%",
"core.msg.update.fail": "获取更新失败!"
}
18 changes: 18 additions & 0 deletions modules/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@kotori-bot/kotori-plugin-core",
"version": "1.1.1",
"description": "core plugin",
"main": "lib/index.js",
"scripts": {
"build": "tsc --build"
},
"license": "GPL-3.0",
"files": [
"lib",
"README.md"
],
"author": "Hotaru <[email protected]>",
"peerDependencies": {
"kotori-bot": "workspace:^"
}
}
Loading

0 comments on commit 6df9f60

Please sign in to comment.