@alemonjs/onebot 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index-12.js DELETED
@@ -1,176 +0,0 @@
1
- import { defineBot, getConfig, useUserHashKey, OnProcessor } from 'alemonjs';
2
- import { OneBotClient } from './sdk-v12/wss.js';
3
-
4
- const platform = 'onebot';
5
- var INDEXV12 = defineBot(() => {
6
- const cfg = getConfig();
7
- const config = cfg.value?.onebot;
8
- if (!config)
9
- return;
10
- //
11
- const client = new OneBotClient({
12
- // url
13
- url: config?.url ?? '',
14
- // token
15
- access_token: config?.token ?? ''
16
- });
17
- //
18
- client.connect();
19
- //
20
- client.on('META', event => {
21
- const bot = event.status.bots[0];
22
- if (!bot)
23
- return;
24
- if (bot.self) {
25
- bot.self.user_id;
26
- }
27
- if (bot.nickname) {
28
- bot.nickname;
29
- }
30
- if (bot.avatar) {
31
- bot?.avatar ?? '';
32
- }
33
- });
34
- //
35
- client.on('MESSAGES', event => {
36
- const uis = config.master_uids ?? [];
37
- let msg = '';
38
- const arr = [];
39
- let at_users = [];
40
- for (const item of event.message) {
41
- if (item.type == 'mention') {
42
- arr.push(item.data);
43
- at_users.push({
44
- avatar: '',
45
- bot: false,
46
- id: item.data.user_id,
47
- name: item.data.text.replace(/^@/, '')
48
- });
49
- }
50
- else if (item.type == 'text') {
51
- msg = item.data.text;
52
- }
53
- }
54
- for (const item of arr) {
55
- msg = msg.replace(item.text, '').trim();
56
- }
57
- const url = event.platform == 'qq' ? `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}` : '';
58
- const UserAvatar = {
59
- toBuffer: async () => {
60
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
61
- return Buffer.from(arrayBuffer);
62
- },
63
- toBase64: async () => {
64
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
65
- return Buffer.from(arrayBuffer).toString('base64');
66
- },
67
- toURL: async () => {
68
- return url;
69
- }
70
- };
71
- const UserId = String(event.user_id);
72
- const UserKey = useUserHashKey({
73
- Platform: platform,
74
- UserId
75
- });
76
- // 定义消
77
- const e = {
78
- // 平台类型
79
- Platform: platform,
80
- // 频道
81
- GuildId: event.group_id,
82
- // guild_name: event.group_name,
83
- // 子频道
84
- ChannelId: event.group_id,
85
- // uyser
86
- IsMaster: uis.includes(String(event.user_id)),
87
- UserId: UserId,
88
- IsBot: false,
89
- UserKey,
90
- UserName: event.sender.nickname,
91
- UserAvatar: UserAvatar,
92
- // message
93
- MessageId: event.message_id,
94
- MessageText: msg,
95
- OpenId: event.user_id,
96
- CreateAt: Date.now(),
97
- // ohther
98
- tag: 'MESSAGES',
99
- value: null
100
- };
101
- // 当访问的时候获取
102
- Object.defineProperty(e, 'value', {
103
- get() {
104
- return event;
105
- }
106
- });
107
- // 处理消息
108
- OnProcessor(e, 'message.create');
109
- });
110
- client.on('DIRECT_MESSAGE', () => {
111
- // const e = {
112
- // isMaster: event.user_id == masterId,
113
- // msg_txt: event.raw_message,
114
- // msg: event.raw_message.trim(),
115
- // msg_id: event.message_id,
116
- // open_id: event.user_id,
117
- // user_id: event.user_id,
118
- // user_avatar:
119
- // event.platform == 'qq'
120
- // ? `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}`
121
- // : '',
122
- // user_name: event.sender.nickname,
123
- });
124
- // 错误处理
125
- client.on('ERROR', event => {
126
- console.error(event);
127
- });
128
- global.client = client;
129
- return {
130
- api: {
131
- use: {
132
- send: (event, val) => {
133
- if (val.length < 0)
134
- return Promise.all([]);
135
- const content = val
136
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
137
- .map(item => item.value)
138
- .join('');
139
- if (content) {
140
- return Promise.all([content].map(item => client.sendGroupMessage({
141
- group_id: event.ChannelId,
142
- message: [
143
- {
144
- type: 'text',
145
- data: {
146
- text: item
147
- }
148
- }
149
- ]
150
- })));
151
- }
152
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
153
- if (images) {
154
- return Promise.all(images.map(item => client.sendGroupMessage({
155
- group_id: event.ChannelId,
156
- message: [
157
- {
158
- type: 'image',
159
- data: {
160
- file_id: item
161
- }
162
- }
163
- ]
164
- })));
165
- }
166
- return Promise.all([]);
167
- },
168
- mention: async () => {
169
- return [];
170
- }
171
- }
172
- }
173
- };
174
- });
175
-
176
- export { INDEXV12 as default };
@@ -1,53 +0,0 @@
1
- import { DIRECT_MESSAGE_TYPE, MESSAGES_TYPE, meta_event_lifecycle, meta_event_heartbeat } from './type.js';
2
-
3
- type OneBotEventMap = {
4
- DIRECT_MESSAGE: DIRECT_MESSAGE_TYPE;
5
- MESSAGES: MESSAGES_TYPE;
6
- META: meta_event_lifecycle | meta_event_heartbeat;
7
- ERROR: any;
8
- };
9
- /**
10
- * 连接
11
- */
12
- declare class OneBotClient {
13
- #private;
14
- /**
15
- * 设置配置
16
- * @param opstion
17
- */
18
- constructor(opstion: {
19
- url: string;
20
- access_token: string;
21
- });
22
- /**
23
- * 注册事件处理程序
24
- * @param key 事件名称
25
- * @param val 事件处理函数
26
- */
27
- on<T extends keyof OneBotEventMap>(key: T, val: (event: OneBotEventMap[T]) => any): this;
28
- /**
29
- *
30
- * @param cfg
31
- * @param conversation
32
- */
33
- connect(): Promise<void>;
34
- /**
35
- *
36
- * @param options
37
- * @returns
38
- */
39
- sendGroupMessage(options: {
40
- group_id: number;
41
- message: any[];
42
- }): void;
43
- /**
44
- * @param options
45
- * @returns
46
- */
47
- sendPrivateMessage(options: {
48
- user_id: number;
49
- message: any[];
50
- }): void;
51
- }
52
-
53
- export { OneBotClient };
@@ -1,134 +0,0 @@
1
- import ws from 'ws';
2
-
3
- /**
4
- * 连接
5
- */
6
- class OneBotClient {
7
- #options = {
8
- url: '',
9
- access_token: ''
10
- };
11
- /**
12
- * 设置配置
13
- * @param opstion
14
- */
15
- constructor(opstion) {
16
- for (const key in opstion) {
17
- if (Object.prototype.hasOwnProperty.call(opstion, key)) {
18
- this.#options[key] = opstion[key];
19
- }
20
- }
21
- }
22
- #ws;
23
- #events = {};
24
- /**
25
- * 注册事件处理程序
26
- * @param key 事件名称
27
- * @param val 事件处理函数
28
- */
29
- on(key, val) {
30
- this.#events[key] = val;
31
- return this;
32
- }
33
- /**
34
- *
35
- * @param cfg
36
- * @param conversation
37
- */
38
- async connect() {
39
- const { url, access_token: token } = this.#options;
40
- const c = token == '' || !token
41
- ? {}
42
- : {
43
- headers: {
44
- ['Authorization']: `Bearer ${token}`
45
- }
46
- };
47
- if (!this.#ws) {
48
- this.#ws = new ws(url, c);
49
- }
50
- // open
51
- this.#ws.on('open', () => {
52
- console.debug(`open:${url}`);
53
- });
54
- // message
55
- this.#ws.on('message', async (data) => {
56
- try {
57
- const event = JSON.parse(data.toString());
58
- if (!event) {
59
- if (this.#events['ERROR'])
60
- this.#events['ERROR'](event);
61
- return;
62
- }
63
- else if (event?.post_type == 'meta_event') {
64
- if (this.#events['META'])
65
- this.#events['META'](event);
66
- return;
67
- }
68
- else if (event?.post_type == 'message') {
69
- if (event?.message_type == 'group') {
70
- if (this.#events['MESSAGES'])
71
- this.#events['MESSAGES'](event);
72
- }
73
- else if (event?.message_type == 'private') {
74
- if (this.#events['DIRECT_MESSAGE'])
75
- this.#events['DIRECT_MESSAGE'](event);
76
- }
77
- else {
78
- console.info('未知消息类型', event);
79
- }
80
- return;
81
- }
82
- else if (event?.post_type == 'notice') {
83
- console.info('暂未处理事件', event);
84
- return;
85
- }
86
- else if (event?.post_type == 'request') {
87
- console.info('暂未处理事件', event);
88
- return;
89
- }
90
- else {
91
- console.info('未知事件', event);
92
- return;
93
- }
94
- }
95
- catch (err) {
96
- if (this.#events['ERROR'])
97
- this.#events['ERROR'](err);
98
- }
99
- });
100
- // close
101
- this.#ws.on('close', (code, reason) => {
102
- if (this.#events['ERROR'])
103
- this.#events['ERROR']({
104
- de: code,
105
- reason: reason.toString('utf8')
106
- });
107
- });
108
- }
109
- /**
110
- *
111
- * @param options
112
- * @returns
113
- */
114
- sendGroupMessage(options) {
115
- return this.#ws.send(JSON.stringify({
116
- action: 'send_group_msg',
117
- params: options,
118
- echo: '1234'
119
- }));
120
- }
121
- /**
122
- * @param options
123
- * @returns
124
- */
125
- sendPrivateMessage(options) {
126
- return this.#ws.send(JSON.stringify({
127
- action: 'send_private_msg',
128
- params: options,
129
- echo: '1234'
130
- }));
131
- }
132
- }
133
-
134
- export { OneBotClient };
@@ -1,122 +0,0 @@
1
- import ws from 'ws';
2
-
3
- /**
4
- * 连接
5
- */
6
- class OneBotClient {
7
- #options = {
8
- url: '',
9
- access_token: ''
10
- };
11
- /**
12
- * 设置配置
13
- * @param opstion
14
- */
15
- constructor(opstion) {
16
- for (const key in opstion) {
17
- if (Object.prototype.hasOwnProperty.call(opstion, key)) {
18
- this.#options[key] = opstion[key];
19
- }
20
- }
21
- }
22
- #ws;
23
- #events = {};
24
- /**
25
- * 注册事件处理程序
26
- * @param key 事件名称
27
- * @param val 事件处理函数
28
- */
29
- on(key, val) {
30
- this.#events[key] = val;
31
- return this;
32
- }
33
- /**
34
- *
35
- * @param cfg
36
- * @param conversation
37
- */
38
- async connect() {
39
- const { url, access_token: token } = this.#options;
40
- const c = token == '' || !token
41
- ? {}
42
- : {
43
- headers: {
44
- ['Authorization']: `Bearer ${token}`
45
- }
46
- };
47
- if (!this.#ws) {
48
- this.#ws = new ws(url, c);
49
- }
50
- // open
51
- this.#ws.on('open', () => {
52
- console.debug(`open:${url}`);
53
- });
54
- // message
55
- this.#ws.on('message', async (data) => {
56
- try {
57
- const event = JSON.parse(data.toString());
58
- if (event) {
59
- if (event?.type == 'meta' && this.#events['META']) {
60
- if (event.status && event.status.bots) {
61
- this.#events['META'](event);
62
- }
63
- }
64
- else if (event?.type == 'message') {
65
- if (event.detail_type == 'private' && this.#events['DIRECT_MESSAGE']) {
66
- await this.#events['DIRECT_MESSAGE'](event);
67
- }
68
- else if (event.detail_type != 'private' && this.#events['MESSAGES']) {
69
- await this.#events['MESSAGES'](event);
70
- }
71
- else {
72
- //
73
- }
74
- }
75
- }
76
- else {
77
- if (event?.status != 'ok') {
78
- if (this.#events['ERROR'])
79
- this.#events['ERROR'](event);
80
- }
81
- }
82
- }
83
- catch (err) {
84
- if (this.#events['ERROR'])
85
- this.#events['ERROR'](err);
86
- }
87
- });
88
- // close
89
- this.#ws.on('close', (code, reason) => {
90
- if (this.#events['ERROR'])
91
- this.#events['ERROR']({
92
- de: code,
93
- reason: reason.toString('utf8')
94
- });
95
- });
96
- }
97
- /**
98
- *
99
- * @param options
100
- * @returns
101
- */
102
- sendGroupMessage(options) {
103
- return this.#ws.send(JSON.stringify({
104
- action: 'send_group_msg',
105
- params: options,
106
- echo: '1234'
107
- }));
108
- }
109
- /**
110
- * @param options
111
- * @returns
112
- */
113
- sendPrivateMessage(options) {
114
- return this.#ws.send(JSON.stringify({
115
- action: 'send_private_msg',
116
- params: options,
117
- echo: '1234'
118
- }));
119
- }
120
- }
121
-
122
- export { OneBotClient };
File without changes