Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some bug. #149

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions client/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
</el-descriptions-item>
{{ systemInfo }}
</el-descriptions>
<pre @click="input?.focus?.()">{{ logs }}</pre>
<pre>{{ logs }}</pre>
<div class="input-wrapper">
<span class="text">{{ inputData }}</span>
<input ref="input" v-model="inputData" @keyup.enter="submitInput" />
Expand All @@ -109,6 +109,7 @@
import { nextTick, onMounted, onUnmounted, ref } from "vue";
import { AdapterInfo, SystemInfo } from "./types";
import { formatSize, formatTime } from "./utils";

const input = ref();
const ws = ref<WebSocket>();
const config = ref<string>("");
Expand Down Expand Up @@ -226,7 +227,8 @@ const init = () => {
const port =
localStorage.getItem("OneBots:serverPort") ||
prompt("请输入服务端监听的端口号", location.port);
const wsUrl = `${location.protocol.replace("http", "ws")}//${location.hostname}:${port}`;
const wsProtocol = location.protocol === "https:" ? "wss:" : "ws:";
const wsUrl = `${wsProtocol}//${location.hostname}:${port}`;
ws.value = new WebSocket(wsUrl);
ws.value.onerror = e => {
console.error("连接出错", e);
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/icqq/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class IcqqAdapter extends Adapter<"icqq", Sendable> {
const oneBot = this.getOneBot<Client>(uin);
if (!oneBot) throw new Error("No one");
if (!oneBot.internal[name]) throw new Error(`internal no api ${name}`);
return oneBot[version][name](...args);
return oneBot.internal[name](...args);
}

createOneBot(uin: string, protocol: IcqqConfig, versions: OneBot.Config[]): OneBot {
Expand Down Expand Up @@ -204,7 +204,7 @@ export default class IcqqAdapter extends Adapter<"icqq", Sendable> {
const oneBot = this.getOneBot<Client>(uin);
if (!oneBot) throw new Error("No one");
let { message, ...result } = await oneBot.internal.getMsg.call(oneBot.internal, message_id);
const segments = this.toSegment(version, message);
const segments = version === "V11" ? message : this.toSegment(version, message);
return {
...result,
message: segments,
Expand Down
9 changes: 5 additions & 4 deletions src/adapters/icqq/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ export async function processMessages(
const { type, data, ...other } = item;
switch (type) {
case "node": {
const node = data || { ...other };
result.push({
type,
...(data || other),
user_id: data.user_id,
...node,
user_id: node.user_id,
message: await processMessages.call(
this,
uin,
data.user_id,
node.user_id,
"private",
data.content || [],
node.content || []
),
});
break;
Expand Down
27 changes: 21 additions & 6 deletions src/server/app.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import Koa from "koa";
import * as os from "os";
import "reflect-metadata";
import { copyFileSync, existsSync, mkdirSync, writeFileSync } from "fs";
import { Logger, getLogger, configure } from "log4js";
import * as fs from "fs";
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { configure, getLogger, Logger } from "log4js";
import { createServer, Server } from "http";
import koaStatic from "koa-static";
import yaml from "js-yaml";
import KoaBodyParser from "koa-bodyparser";
import basicAuth from "koa-basic-auth";

import { deepMerge, deepClone, Class, readLine } from "@/utils";
import { Class, deepClone, deepMerge, readLine } from "@/utils";
import { Router, WsServer } from "./router";
import { readFileSync } from "fs";
import { V11 } from "@/service/V11";
import { V12 } from "@/service/V12";
import { LogLevel } from "@/types";
import * as path from "path";
import { Adapter } from "@/adapter";
import { ChildProcess } from "child_process";
import process from "process";
import * as fs from "fs";
import { Dict } from "@zhinjs/shared";

export interface KoaOptions {
env?: string;
keys?: string[];
Expand Down Expand Up @@ -216,7 +216,22 @@ export class App extends Koa {
}
switch (payload.action) {
case "system.input":
return process.stdin.write(`${payload.data}\n`);
// 将流的模式切换到“流动模式”
process.stdin.resume();

// 使用以下函数来模拟输入数据
function simulateInput(data: Buffer) {
process.nextTick(() => {
process.stdin.emit("data", data);
});
}

simulateInput(Buffer.from(payload.data + "\n", "utf8"));
// 模拟结束
process.nextTick(() => {
process.stdin.emit("end");
});
return true;
case "system.saveConfig":
return fs.writeFileSync(App.configPath, payload.data, "utf8");
case "system.reload":
Expand Down
21 changes: 13 additions & 8 deletions src/service/V11/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Config } from "./config";
import { Action } from "./action";
import { OneBot, OneBotStatus } from "@/onebot";
import { BOOLS, NotFoundError, OneBot, OneBotStatus } from "@/onebot";
import { Logger } from "log4js";
import crypto from "crypto";
import { WebSocket, WebSocketServer } from "ws";
import { Dispose } from "@/types";
import { Context } from "koa";
import { URL } from "url";
import { toBool, toHump, toLine, randomInt } from "@/utils";
import { BOOLS, NotFoundError } from "@/onebot";
import { randomInt, toBool, toHump, toLine } from "@/utils";
import http from "http";
import https from "https";
import { join } from "path";
Expand Down Expand Up @@ -621,7 +620,7 @@ export class V11 extends Service<"V11"> implements OneBot.Base {
ret = await this.action[method].apply(this, args);
} catch (e) {
this.logger.error(`run ${action} with args:${args.length} failed:`, e);
return JSON.stringify(V11.error(e.message));
return JSON.stringify(V11.error(e.message, echo));
}
if (ret instanceof Promise) {
if (is_async) {
Expand All @@ -640,7 +639,7 @@ export class V11 extends Service<"V11"> implements OneBot.Base {
result.echo = echo;
}
return JSON.stringify(result, (_, v) => (typeof v === "bigint" ? v.toString() : v));
}
} else throw new NotFoundError();
}

/**
Expand All @@ -667,24 +666,30 @@ export namespace V11 {
status: "ok" | "async" | "error";
data: T;
error: string;
msg?: string;
wording?: string;
echo?: string;
}

export function ok<T extends any>(data: T, retcode = 0, pending?: boolean): Result<T> {
export function ok<T extends any>(data: T, retcode = 0, pending?: boolean, echo?: string): Result<T> {
return {
retcode,
retcode: pending ? 1 : retcode,
status: pending ? "async" : "ok",
data,
error: null,
echo
};
}

export function error(error: string, retcode = 1): Result<any> {
export function error(error: string, echo?: string, retcode = 1500, wording?: string): Result<any> {
return {
retcode,
status: "error",
data: null,
error,
msg: error,
wording,
echo
};
}

Expand Down