Skip to content

Commit b342d83

Browse files
committed
update types definition according to Yunzai repo
1 parent 750a244 commit b342d83

7 files changed

Lines changed: 111 additions & 43 deletions

File tree

components/cpath.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { PLUGIN_ID } from '#gc';
12

23
const cwd = process.cwd();
34

45
const Path = {
56
Process: cwd,
6-
Config: `${cwd}/plugins/anon-plugin/config`,
7-
Resource: `${cwd}/plugins/anon-plugin/resources`,
8-
HTML: `${cwd}/plugins/anon-plugin/resources/html`,
9-
Image: `${cwd}/plugins/anon-plugin/resources/img`
7+
App: `${cwd}/plugins/${PLUGIN_ID}/apps`,
8+
Config: `${cwd}/plugins/${PLUGIN_ID}/config`,
9+
Resource: `${cwd}/plugins/${PLUGIN_ID}/resources`,
10+
HTML: `${cwd}/plugins/${PLUGIN_ID}/resources/html`,
11+
Image: `${cwd}/plugins/${PLUGIN_ID}/resources/img`
1012
};
1113

1214
export default Path;

components/import/common.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// @ts-ignore
22
import common from '../../../../lib/common/common.js';
33

4+
import type { MessageSegment, SendMessageResult } from "./types.js";
5+
46
const Common = {
5-
async replyPrivate(userId: string, msg: string) {
7+
async replyPrivate(userId: string, msg: string): Promise<SendMessageResult> {
68
return await common.relpyPrivate(userId, msg);
79
},
8-
async makeForwardMsg(e: any, msgs: string[], desc?: string, anonymous?: boolean) {
10+
async makeForwardMsg(e: any, msgs: string[], desc?: string, anonymous?: boolean): Promise<MessageSegment[]> {
911
return await common.makeForwardMsg(e, msgs, desc, anonymous);
1012
}
1113
};

components/import/plugin.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
// @ts-ignore
22
import plugin from '../../../../lib/plugins/plugin.js';
33

4-
import type { User, Group, IBot } from "./types.js";
4+
import type { User, Group, GroupUser, IBot, MessageSegment, SendMessageResult } from "./types.js";
55
interface Task {
66
name: string;
77
fnc?: any;
88
cron?: any;
99
}
10+
1011
interface Rule {
1112
reg: string;
1213
fnc: string;
14+
permission?: "master";
1315
log?: boolean;
1416
}
17+
1518
interface ReplyParam {
1619
recallMsg?: number; // seconds, 0 - 120
1720
at?: boolean;
1821
}
22+
1923
interface E {
20-
msg: string;
21-
sender: User;
22-
group: Group;
24+
message: MessageSegment[]; // Message segments array
25+
message_type?: "private" | "group"; // Message type
26+
notice_type?: "friend" | "group"; // Notice type
27+
isPrivate?: boolean;
28+
isGroup?: boolean;
29+
isMaster?: boolean; // Is master user
30+
31+
msg?: string; // Text message
32+
atBot?: boolean;
33+
img?: string[]; // Image urls array
34+
at?: number;
35+
sender?: User | GroupUser;
36+
friend?: User;
37+
group?: Group;
38+
member?: GroupUser;
39+
2340
bot: IBot;
2441

25-
reply: (msg: string | string[], quote?: boolean, data?: ReplyParam) => Promise<void>;
42+
// NOTE: quote is not working for some reason
43+
reply: (msg: string | string[], quote?: boolean, data?: ReplyParam) => Promise<SendMessageResult> | false;
44+
recall?: () => Promise<MessageSegment[]>; // Recall message
45+
46+
reply_id?: string; // Reply message ID
47+
getReply?: () => Promise<MessageSegment[]>;
2648
}
2749

2850
class PluginClass {
@@ -34,8 +56,16 @@ class PluginClass {
3456
rule: Rule[];
3557
e: E;
3658

37-
reply: (msg: string | string[], quote?: boolean, data?: ReplyParam) => Promise<void>;
3859
constructor(t: any) {};
60+
61+
reply: (msg: string | string[], quote?: boolean, data?: ReplyParam) => Promise<SendMessageResult> | false;
62+
63+
// isGroup == true => any group member message will trigger the context
64+
// otherwise only the sender
65+
// by default isGroup is false
66+
setContext: (fnc: string, isGroup?: boolean, time?: number, timeout_prompt?: string) => E;
67+
getContext: (fnc: string, isGroup?: boolean) => E;
68+
finish: (fnc: string, isGroup?: boolean) => void;
3969
};
4070

4171
const Plugin = plugin as (typeof PluginClass);

components/import/types.ts

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
type Message = string[] | string;
21
interface IDict {
32
[key: string]: any;
43
}
4+
55
export interface IYunzai {
66
uin: number[];
77
pickFriend: (user_id: number) => User;
88
pickGroup: (group_id: number) => Group;
99
pickMember: (group_id: number, user_id: number) => GroupUser;
1010
}
11+
1112
export interface IBot {
1213
uin: number;
1314
nickname: string;
@@ -21,21 +22,65 @@ export interface IBot {
2122
getGroupList: () => Group[];
2223
getGroupMap: () => Map<number, Group>;
2324
}
25+
26+
export interface ISegment {
27+
image: (file: string, type?: string, subType?: string) => string;
28+
at: (user_id: number, name: string) => string;
29+
record: (file: string) => string;
30+
video: (file: string) => string;
31+
reply: (id, text, qq, time, seq) => string;
32+
face: (id) => string;
33+
share: (url, title, content, image) => string;
34+
music: (type, id, url, audio, title) => string;
35+
poke: (qq) => string;
36+
gift: (qq, id) => string;
37+
xml: (data, resid) => string;
38+
json: (data, resid) => string;
39+
cardimage: (file, minwidth, minheight, maxwidth, maxheight, source, icon) => string;
40+
tts: (text) => string;
41+
custom: (type, data) => string;
42+
}
43+
44+
export interface MessageSegment {
45+
type: "text" | "image" | "record" | "video" | "reply" | "face" | "share" | "music" | "poke" | "gift" | "xml" | "json" | "cardimage" | "tts" | "raw" | "markdown" | "node" | "custom";
46+
data?: IDict;
47+
qq?: number;
48+
file?: string;
49+
name?: string;
50+
id?: string; // Reply message_id
51+
text?: string; // Reply text or message text
52+
}
53+
export type Message = MessageSegment | string | (MessageSegment | string)[];
54+
55+
export interface ForwardMessage {
56+
message: Message;
57+
nickname?: string;
58+
user_id?: number;
59+
time?: number; // Date.now() / 1000
60+
}
61+
export interface SendMessageResult {
62+
message_id: string | string[]; // Message ID
63+
data?: any[];
64+
}
65+
2466
export interface User {
2567
user_id: number;
26-
card?: string; // Only for GroupMember
27-
sendMsg: (msg: Message) => void;
28-
recallMsg: (message_id: number) => void;
29-
makeForwardMsg: (msg: Message) => Message;
68+
69+
sendMsg: (msg: Message) => Promise<SendMessageResult>;
70+
recallMsg: (message_id: string | string[]) => Promise<MessageSegment[]>;
71+
makeForwardMsg: (msgs: ForwardMessage[]) => Promise<MessageSegment[]>;
3072
getInfo: () => IDict; // User info
3173
getAvatarUrl: () => string;
3274
}
75+
3376
export interface Group {
3477
group_id: number;
35-
sendMsg: (msg: Message) => void;
36-
recallMsg: (message_id: number) => void;
37-
makeForwardMsg: (msg: Message) => Message;
78+
79+
sendMsg: (msg: Message) => Promise<SendMessageResult>;
80+
recallMsg: (message_id: string | string[]) => Promise<MessageSegment[]>;
81+
makeForwardMsg: (msgs: ForwardMessage[]) => Promise<MessageSegment[]>;
3882
getInfo: () => IDict; // Group info
83+
3984
getAvatarUrl: () => string;
4085
getMemberList: () => GroupUser[];
4186
getMemberMap: () => Map<number, GroupUser>;
@@ -45,24 +90,10 @@ export interface Group {
4590
setAvatar: (file: string) => void;
4691
setAdmin: (user_id: number, enable: boolean) => void;
4792
}
93+
4894
export interface GroupUser extends User {
95+
card: string; // Group member card (nickname in group)
96+
4997
pickFriend: () => User;
5098
poke: () => void;
5199
}
52-
export interface Segment {
53-
image: (file: string, type?: string, subType?: string) => string;
54-
at: (user_id: number, name: string) => string;
55-
record: (file: string) => string;
56-
video: (file: string) => string;
57-
reply: (id, text, qq, time, seq) => string;
58-
face: (id) => string;
59-
share: (url, title, content, image) => string;
60-
music: (type, id, url, audio, title) => string;
61-
poke: (qq) => string;
62-
gift: (qq, id) => string;
63-
xml: (data, resid) => string;
64-
json: (data, resid) => string;
65-
cardimage: (file, minwidth, minheight, maxwidth, maxheight, source, icon) => string;
66-
tts: (text) => string;
67-
custom: (type, data) => string;
68-
}

components/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const PLUGIN_ID = 'anon-plugin';
2+
13
import Common from './import/common.js';
24
import Logger from './import/logger.js';
35
import Plugin from './import/plugin.js';
@@ -9,6 +11,7 @@ import Config from './config.js';
911
import TodaysFortunePicker from './todays_fortune/picker.js';
1012

1113
export {
14+
PLUGIN_ID,
1215
Common,
1316
Logger,
1417
Plugin,

components/types/import.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* It will break global definition.
44
*/
55

6-
type Segment = import("../import/types.ts").Segment;
6+
type ISegment = import("../import/types.ts").ISegment;
77
type IYunzai = import("../import/types.ts").IYunzai;
88
declare namespace NodeJS {
99
export interface Global {
10-
segment: Segment;
10+
segment: ISegment;
1111
Bot: IYunzai;
1212
}
1313
}
1414

15-
declare var segment: Segment;
15+
declare var segment: ISegment;
1616
declare var Bot: IYunzai;

index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import fs from 'node:fs'
22

3-
import { Path, Logger, Config } from '#gc';
3+
import { PLUGIN_ID, Path, Logger, Config } from '#gc';
44

55
const apps = await (async () => {
66
let ret = [];
77
const files = fs
8-
.readdirSync(`${Path.Process}/plugins/anon-plugin/apps`)
8+
.readdirSync(Path.App)
99
.filter(file => file.endsWith('.js'));
1010

1111
Config.load();
@@ -21,7 +21,7 @@ const apps = await (async () => {
2121
let name = files[i].replace('.js', '');
2222

2323
if (ret[i].status != 'fulfilled') {
24-
Logger.error(`[anon-plugin] Failed to load app ${name}`);
24+
Logger.error(`[${PLUGIN_ID}] Failed to load app ${name}`);
2525
Logger.error(ret[i].reason);
2626
continue;
2727
}

0 commit comments

Comments
 (0)