Skip to content

Commit

Permalink
feat: qqadapter send image
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Jan 14, 2024
1 parent 8b4c508 commit be9e966
Show file tree
Hide file tree
Showing 122 changed files with 3,810 additions and 24 deletions.
21 changes: 21 additions & 0 deletions modules/adapter-onebot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Config

```yaml
kotori:
extends: onebot
master: 10001
# 连接模式 可选: http ws ws-reverse 推荐首选ws-reverse
mode: ws-reverse
# address: "ws://127.0.0.1"
# WebSocket反向(相对于Gocqhttp)
# port: 8080 # WS反向端口
# WebSocket正向
address: ws://127.0.0.1 # WS地址
port: 8088 # WS端口
retry: 10 # 同上
# Http正反向(暂未支持)
# address: "http://localhost" # 正向Http地址
# port: 8888 # 正向Http端口
# reverse-port: 8080 # 反向Http端口
# retry: 10 # 连接断开或失败时尝试重连间隔时间 单位:秒
```
4 changes: 2 additions & 2 deletions modules/adapter-onebot/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@kotori-bot/kotori-plugin-adapter-onebot",
"version": "1.1.1",
"description": "Adapter For OneBot (go-cqhttp)",
"description": "Adapter For Onebot (go-cqhttp)",
"main": "lib/index.js",
"scripts": {
"build": "tsc --build"
Expand All @@ -19,4 +19,4 @@
"@types/ws": "^8.5.8",
"ws": "^8.14.2"
}
}
}
14 changes: 7 additions & 7 deletions modules/adapter-onebot/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/
import { Adapter, AdapterConfig, Context, EventDataApiBase, EventDataTargetId, Tsu } from 'kotori-bot';
import WebSocket from 'ws';
import OneBotApi from './api';
import OnebotApi from './api';
import WsServer from './services/wsserver';
import { EventDataType } from './types';
import OneBotElements from './elements';
import OnebotElements from './elements';

interface EventDataPoke extends EventDataApiBase<'poke'> {
targetId: EventDataTargetId;
Expand Down Expand Up @@ -40,15 +40,15 @@ export const config = Tsu.Intersection([
]),
]);

type OneBotConfig = Tsu.infer<typeof config> & AdapterConfig;
type OnebotConfig = Tsu.infer<typeof config> & AdapterConfig;

export class OneBotAdapter extends Adapter {
export class OnebotAdapter extends Adapter {
private readonly info: string;

public readonly config: OneBotConfig;
public readonly config: OnebotConfig;

public constructor(ctx: Context, config: OneBotConfig, identity: string) {
super(ctx, config, identity, OneBotApi, new OneBotElements());
public constructor(ctx: Context, config: OnebotConfig, identity: string) {
super(ctx, config, identity, OnebotApi, new OnebotElements());
this.config = config;
this.info = `${this.config.address}:${this.config.port}`;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/adapter-onebot/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { Api, MessageRaw } from 'kotori-bot';

export class OneBotApi extends Api {
export class OnebotApi extends Api {
public send_private_msg(message: MessageRaw, userId: number) {
this.adapter.status.lastMsgTime = new Date();
this.adapter.status.sentMsg += 1;
Expand Down Expand Up @@ -125,4 +125,4 @@ export class OneBotApi extends Api {
/* public extra: ApiExtraValue = { type: 'onebot', image, at, poke }; */
}

export default OneBotApi;
export default OnebotApi;
4 changes: 2 additions & 2 deletions modules/adapter-onebot/src/elements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Elements, EventDataTargetId, none } from 'kotori-bot';
import { MessageCqType } from './types';

export class OneBotElements extends Elements {
export class OnebotElements extends Elements {
public cq(type: MessageCqType, data: string) {
none(this);
return `[CQ:${type},${data}]`;
Expand All @@ -28,4 +28,4 @@ export class OneBotElements extends Elements {
}
}

export default OneBotElements;
export default OnebotElements;
4 changes: 2 additions & 2 deletions modules/adapter-onebot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { OneBotAdapter } from './adapter';
import { OnebotAdapter } from './adapter';

export default OneBotAdapter;
export default OnebotAdapter;
44 changes: 41 additions & 3 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: 2024-01-01 16:43:54
* @LastEditTime: 2024-01-14 17:45:01
*/
import { Adapter, AdapterConfig, Context, Tsu, obj } from 'kotori-bot';
import WebSocket from 'ws';
Expand All @@ -26,7 +26,12 @@ export class QQAdapter extends Adapter<QQApi> {

private seq = 0;

private msg_seq = 0;
/* here need */
public msg_seq = 0;

private groupId = '';

public imageStack: (string | true)[] = [];

public readonly config: QQConfig;

Expand Down Expand Up @@ -71,6 +76,8 @@ export class QQAdapter extends Adapter<QQApi> {
},
groupId: data.d.group_openid,
});
this.groupId = data.d.group_openid;
/* here need imporve */
} else if (data.op === 11) {
this.online();
// this.offlineCheck();
Expand Down Expand Up @@ -100,18 +107,49 @@ export class QQAdapter extends Adapter<QQApi> {
return undefined;
}
let address = '/';
let cancel = false;
let req: obj = {};
if (action === 'send_group_msg' && 'groupId' in params && 'message' in params && 'id' in params) {
if (!params.message) return null;
this.msg_seq += 1;
address += `groups/${params.groupId}/messages`;
req = {
content: params.message,
msg_type: 0,
msg_id: params.id,
msg_seq: this.msg_seq,
};
if (this.imageStack[this.msg_seq]) {
cancel = true;
let timerId: NodeJS.Timeout
const timer = () => setTimeout(() => {
if (timerId) clearTimeout(timerId);
if (this.imageStack[this.msg_seq] === true) {
timerId = timer();
return;
}
req['file_info'] = { file_info: this.imageStack[this.msg_seq] };
req.msg_type = 7;
this.ctx.http.post(`${API_ADDRESS}${address}`, req, {
headers: {
Authorization: `QQBot ${this.token}`,
'X-Union-Appid': this.config.appid,
},
validateStatus: () => true,
});
}, 500);
};
this.msg_seq += 1;
} else if (action === 'send_group_msg_media' && 'url' in params && 'file_type' in params) {
address += `groups/${this.groupId}/files`;
req = {
file_type: params.file_type,
url: params.url,
srv_send_msg: false,
};
console.log(req);
this.msg_seq += 1;
}
if (cancel) return undefined;
return this.ctx.http.post(`${API_ADDRESS}${address}`, req, {
headers: {
Authorization: `QQBot ${this.token}`,
Expand Down
21 changes: 21 additions & 0 deletions modules/adapter-qq/src/elements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Elements, EventDataTargetId, none } from 'kotori-bot';
import QQAdapter from '.';

export class QQElements extends Elements<QQAdapter> {

public image(url: string) {
const seq = this.adapter.msg_seq;
this.adapter.imageStack[seq] = true;;
this.adapter.send
('send_group_msg_media', { url })?.then(res => {
if (!res || typeof res !== 'object' || !('file_info' in res)) {
delete this.adapter.imageStack[seq]
return;
};
this.adapter.imageStack[this.adapter.msg_seq] = res.file_info as string;
})
return '';
}
}

export default QQElements;
10 changes: 10 additions & 0 deletions modules/bangumi/locales/en_US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bangumi.descr.bgm": "Search games/anime on Bangumi",
"bangumi.descr.bgmc": "Get today's Bangumi schedule",
"bangumi.msg.bgm": "Original Name: %name%\nChinese Name: %name_cn%\nSummary: %summary%\nTags: %tags%\nDetails: %url%\n%image%",
"bangumi.msg.bgm.list": "%num%.%name%%name_cn%\n",
"bangumi.msg.bgm.lists": "%list%Resend command and input [number] to choose corresponding content\nExample: /command content 2",
"bangumi.msg.bgm.fail": "No matching items found: %input%",
"bangumi.msg.bgmc": "Date: %weekday%~%list%",
"bangumi.msg.bgmc.list": "\nOriginal Name: %name%\nChinese Name: %name_cn%\nAir Time: %air_date%\n%image%"
}
10 changes: 10 additions & 0 deletions modules/bangumi/locales/ja_JP.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bangumi.descr.bgm": "Bangumiでゲーム/アニメを検索",
"bangumi.descr.bgmc": "今日のBangumiの番組表を取得",
"bangumi.msg.bgm": "オリジナル名: %name%\n中国語名: %name_cn%\n概要: %summary%\nタグ: %tags%\n詳細: %url%\n%image%",
"bangumi.msg.bgm.list": "%num%.%name%%name_cn%\n",
"bangumi.msg.bgm.lists": "%list%コマンドを再送信し、対応するコンテンツを選択するために[番号]を入力してください。\n例: /コマンド コンテンツ 2",
"bangumi.msg.bgm.fail": "一致するアイテムが見つかりません: %input%",
"bangumi.msg.bgmc": "日付: %weekday%~%list%",
"bangumi.msg.bgmc.list": "\nオリジナル名: %name%\n中国語名: %name_cn%\n放送時間: %air_date%\n%image%"
}
10 changes: 10 additions & 0 deletions modules/bangumi/locales/zh_CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bangumi.descr.bgm": "番组计划搜索游戏/动漫",
"bangumi.descr.bgmc": "获取番组计划今日放送",
"bangumi.msg.bgm": "原名: %name%\n中文名: %name_cn%\n介绍: %summary%\n标签: %tags%\n详情: %url%\n%image%",
"bangumi.msg.bgm.list": "%num%。%name%%name_cn%\n",
"bangumi.msg.bgm.lists": "%list%再次发送指令并传入参数[序号]以选择对应内容\n示例:/指令 内容 2",
"bangumi.msg.bgm.fail": "未找到相关条目: %input%",
"bangumi.msg.bgmc": "日期: %weekday%~%list%",
"bangumi.msg.bgmc.list": "\n原名: %name%\n中文名: %name_cn%\n开播时间: %air_date%\n%image%"
}
10 changes: 10 additions & 0 deletions modules/bangumi/locales/zh_TW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bangumi.descr.bgm": "番组計劃搜索遊戲/動漫",
"bangumi.descr.bgmc": "獲取番組計劃今日放送",
"bangumi.msg.bgm": "原名: %name%\n中文名: %name_cn%\n介紹: %summary%\n標簽: %tags%\n詳情: %url%\n%image%",
"bangumi.msg.bgm.list": "%num%。%name%%name_cn%\n",
"bangumi.msg.bgm.lists": "%list%再次發送指令並傳入參數[序號]以選擇對應內容\n示例:/指令 內容 2",
"bangumi.msg.bgm.fail": "未找到相關條目: %input%",
"bangumi.msg.bgmc": "日期: %weekday%~%list%",
"bangumi.msg.bgmc.list": "\n原名: %name%\n中文名: %name_cn%\n開播時間: %air_date%\n%image%"
}
28 changes: 28 additions & 0 deletions modules/bangumi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "kotori-plugin-bangumi",
"version": "1.1.1",
"description": "bangumi plugin",
"main": "lib/index.js",
"scripts": {
"build": "tsc --build"
},
"license": "GPL-3.0",
"files": [
"lib",
"README.md"
],
"author": "Himeno <[email protected]>",
"peerDependencies": {
"kotori-bot": "workspace:^"
},
"kotori": {
"meta": {
"language": [
"en_US",
"ja_JP",
"zh_TW",
"zh_CN"
]
}
}
}
10 changes: 10 additions & 0 deletions modules/bangumi/src/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Kotori from 'kotori-bot';

const headers = {
'user-agent': 'czy0729/Bangumi/6.4.0 (Android) (http://github.com/czy0729/Bangumi)',
};

export const http: typeof Kotori.http.get = async (url, params) =>
Kotori.http.get(`https://api.bgm.tv/${url}`, params, { headers });

export default http;
Loading

0 comments on commit be9e966

Please sign in to comment.