Skip to content

Commit

Permalink
feat: loader updated, as subpack and core-18n
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Nov 9, 2023
1 parent d052c83 commit 1e5a6ec
Show file tree
Hide file tree
Showing 31 changed files with 402 additions and 258 deletions.
14 changes: 10 additions & 4 deletions kotori.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
global:
command-prefix: '/'
lang: 'zh_CN'
command-prefix: "/"
lang: "zh_CN"

adapter:
kotori:
extend: "qq"
master: 3324656453
# 连接模式 可选: http ws ws-reverse 推荐首选ws-reverse
mode: ws-reverse
address: 'ws://127.0.0.1'
address: "ws://127.0.0.1"
# WebSocket反向(相对于Gocqhttp)
port: 8080 # WS反向端口
# WebSocket正向
Expand All @@ -23,4 +23,10 @@ adapter:

cmd-test:
extend: "cmd"
master: 0720
master: 720
nickname: "Kotarou"
age: 18
sex: "male"
self-id: 2333
self-nickname: "Kotori"
self-avatar: "https://kotori.js.org/kotori.png"
2 changes: 1 addition & 1 deletion modules/adapter-cmd/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "kotori-plugin-adapter-cmd",
"name": "@kotori-bot/kotori-plugin-adapter-cmd",
"version": "1.0.0",
"description": "Adapter For Cmd",
"main": "src/index.ts",
Expand Down
48 changes: 31 additions & 17 deletions modules/adapter-cmd/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,56 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:09
* @LastEditors: Hotaru biyuehuya@gmail.com
* @LastEditTime: 2023-10-28 21:14:31
* @LastEditTime: 2023-11-09 21:56:50
*/
import Kotori, { Adapter, AdapterConfig, Msg, eventDataMsgSender, isObj } from 'kotori-bot';
import { Adapter, AdapterConfig, MessageRaw, EventDataMsgSender, isObj, Content } from 'kotori-bot';
import CmdApi from './api';

interface Iconfig extends AdapterConfig {
interface CmdConfig extends AdapterConfig {
nickname: string;
age: number;
sex: eventDataMsgSender['sex'];
sex: EventDataMsgSender['sex'];
'self-id': number;
'self-nickname': string;
'self-avatar': string;
}

function checkConfig(config: unknown): config is Iconfig {
function checkConfig(config: unknown): config is CmdConfig {
if (!isObj(config)) return false;
if (typeof config.nickname !== 'string') return false;
if (typeof config.age !== 'number') return false;
if (config.sex !== 'male' && config.sex !== 'female' && config.sex !== 'unknown') return false;
if (typeof config['self-id'] !== 'number') return false;
if (typeof config['self-nickname'] !== 'string') return false;
if (typeof config['self-avatar'] !== 'string') return false;
return true;
}

export default class CmdAdapter extends Adapter<CmdApi> {
private messageId = 1;

public readonly platform: string = 'cmd';
public config: CmdConfig;

public declare config: Iconfig;

public constructor(config: AdapterConfig, identity: string) {
const defaultConfig = {
public constructor(
config: Partial<keyof Omit<CmdConfig, keyof AdapterConfig>> & AdapterConfig,
identity: string,
ctx: Content,
) {
const defaultConfig: Omit<CmdConfig, keyof AdapterConfig> = {
nickname: 'Kotarou',
age: 18,
sex: 'male',
'self-id': 2333,
'self-nickname': 'Kotori',
'self-avatar': 'https://kotori.js.org/kotori.png',
};
const newConfig = Object.assign(defaultConfig, config);
super(newConfig, identity, CmdApi);
super(newConfig, identity, ctx, CmdApi);
if (!checkConfig(newConfig)) throw new Error(`Bot '${identity}' config format error`);
this.config = newConfig;
this.selfId = this.config['self-id'];
this.nickname = this.config['self-nickname'];
this.avatar = this.config['self-avatar'];
process.stdin.on('data', data => this.handle(data));
}

Expand All @@ -48,7 +62,7 @@ export default class CmdAdapter extends Adapter<CmdApi> {
if (message === '\n' || message === '\r\n') return;
message = message.replace('\r\n', '').replace('\n', '');

Kotori.emit({
this.ctx.emit({
type: 'private_msg',
messageId: this.messageId,
message,
Expand All @@ -58,7 +72,7 @@ export default class CmdAdapter extends Adapter<CmdApi> {
sex: this.config.sex,
age: this.config.age,
},
send: (message: Msg) => {
send: (message: MessageRaw) => {
this.api.send_private_msg(message, this.config.master);
},
api: this.api,
Expand All @@ -72,15 +86,15 @@ export default class CmdAdapter extends Adapter<CmdApi> {
if (this.status.value !== 'online' || action !== 'send_private_msg' || !params) return;
if (typeof (params as { message: string }).message !== 'string') return;
if ((params as { user_id: unknown }).user_id !== this.config.master) return;
process.stdout.write(`> ${(params as { message: string }).message} \r\n`);
process.stdout.write(`${this.nickname} > ${(params as { message: string }).message} \r\n`);
this.messageId += 1;
Kotori.emit({
this.ctx.emit({
type: 'send',
api: this.api,
messageId: this.messageId,
});
};
Kotori.emit({
this.ctx.emit({
type: 'connect',
adapter: this,
normal: true,
Expand All @@ -90,7 +104,7 @@ export default class CmdAdapter extends Adapter<CmdApi> {
};

public stop = () => {
Kotori.emit({
this.ctx.emit({
type: 'disconnect',
adapter: this,
normal: true,
Expand Down
6 changes: 3 additions & 3 deletions modules/adapter-cmd/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:13
* @LastEditors: Hotaru biyuehuya@gmail.com
* @LastEditTime: 2023-10-28 17:51:02
* @LastEditTime: 2023-11-08 12:08:40
*/
import { Api, Msg } from 'kotori-bot';
import { Api, MessageRaw } from 'kotori-bot';

export default class CmdApi extends Api {
public send_private_msg = (message: Msg, userId: number) => {
public send_private_msg = (message: MessageRaw, userId: number) => {
/* handled msg... */
this.adapter.send('send_private_msg', { user_id: userId, message });
};
Expand Down
2 changes: 1 addition & 1 deletion modules/adapter-qq/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "kotori-plugin-adapter-qq",
"name": "@kotori-bot/kotori-plugin-adapter-qq",
"version": "1.0.0",
"description": "Adapter For QQ",
"main": "src/index.ts",
Expand Down

0 comments on commit 1e5a6ec

Please sign in to comment.