@alemonjs/onebot 0.0.1

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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # [https://alemonjs.com/](https://alemonjs.com/)
2
+
3
+ 跨平台开发的事件驱动机器人
4
+
5
+ - alemomn.config.yaml
6
+
7
+ ```yaml
8
+ onebot:
9
+ url: ''
10
+ token: ''
11
+ master_id: null
12
+ version: 'v11'
13
+ ```
@@ -0,0 +1,143 @@
1
+ import { defineBot, getConfig, Text, At, OnProcessor, useParse } from 'alemonjs';
2
+ import { OneBotClient } from './sdk-v11/wss.js';
3
+
4
+ var INDEXV11 = defineBot(() => {
5
+ const cfg = getConfig();
6
+ const config = cfg.value?.onebot;
7
+ if (!config)
8
+ return;
9
+ //
10
+ const client = new OneBotClient({
11
+ // url
12
+ url: config?.url ?? '',
13
+ // token
14
+ access_token: config?.token ?? ''
15
+ });
16
+ //
17
+ client.connect();
18
+ //
19
+ client.on('META', event => {
20
+ if (event?.self_id) {
21
+ String(event.self_id);
22
+ }
23
+ });
24
+ //
25
+ client.on('MESSAGES', event => {
26
+ const uis = config.master_uids ?? [];
27
+ let msg = '';
28
+ const arr = [];
29
+ let at_users = [];
30
+ for (const item of event.message) {
31
+ if (item.type == 'text') {
32
+ msg = item.data.text;
33
+ }
34
+ }
35
+ for (const item of arr) {
36
+ msg = msg.replace(item.text, '').trim();
37
+ }
38
+ // 定义消
39
+ const e = {
40
+ // 平台类型
41
+ Platform: 'onebot',
42
+ // 频道
43
+ GuildId: String(event.group_id),
44
+ // guild_name: event.group_name,
45
+ // 子频道
46
+ ChannelId: String(event.group_id),
47
+ // 是否是主人
48
+ IsMaster: uis.includes(String(event.user_id)),
49
+ // 用户ID
50
+ UserId: String(event.user_id),
51
+ // 用户名
52
+ UserName: event.sender.nickname,
53
+ // 用户头像
54
+ UserAvatar: `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}`,
55
+ // 格式化数据
56
+ MsgId: String(event.message_id),
57
+ // 用户消息
58
+ Megs: [
59
+ Text(msg),
60
+ ...at_users.map(item => At(item.id, 'user', {
61
+ name: item.name,
62
+ avatar: item.avatar,
63
+ bot: item.bot
64
+ }))
65
+ ],
66
+ // 用户openId
67
+ OpenID: String(event.user_id),
68
+ // 创建时间
69
+ CreateAt: Date.now(),
70
+ // 标签
71
+ tag: 'MESSAGES',
72
+ //
73
+ value: null
74
+ };
75
+ // 当访问的时候获取
76
+ Object.defineProperty(e, 'value', {
77
+ get() {
78
+ return event;
79
+ }
80
+ });
81
+ // 处理消息
82
+ OnProcessor(e, 'message.create');
83
+ });
84
+ client.on('DIRECT_MESSAGE', () => {
85
+ // const e = {
86
+ // isMaster: event.user_id == masterID,
87
+ // msg_txt: event.raw_message,
88
+ // msg: event.raw_message.trim(),
89
+ // msg_id: event.message_id,
90
+ // open_id: event.user_id,
91
+ // user_id: event.user_id,
92
+ // user_avatar:
93
+ // event.platform == 'qq'
94
+ // ? `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}`
95
+ // : '',
96
+ // user_name: event.sender.nickname,
97
+ });
98
+ // 错误处理
99
+ client.on('ERROR', event => {
100
+ console.error('ERROR', event);
101
+ });
102
+ return {
103
+ api: {
104
+ use: {
105
+ send: (event, val) => {
106
+ if (val.length < 0)
107
+ return Promise.all([]);
108
+ const content = useParse(val, 'Text');
109
+ if (content) {
110
+ return Promise.all([content].map(item => client.sendGroupMsg({
111
+ group_id: event.ChannelId,
112
+ message: [
113
+ {
114
+ type: 'text',
115
+ data: {
116
+ text: item
117
+ }
118
+ }
119
+ ]
120
+ })));
121
+ }
122
+ const images = useParse(val, 'Image');
123
+ if (images) {
124
+ return Promise.all(images.map(item => client.sendGroupMsg({
125
+ group_id: event.ChannelId,
126
+ message: [
127
+ {
128
+ type: 'image',
129
+ data: {
130
+ file_id: `base64://${item.toString('base64')}`
131
+ }
132
+ }
133
+ ]
134
+ })));
135
+ }
136
+ return Promise.all([]);
137
+ }
138
+ }
139
+ }
140
+ };
141
+ });
142
+
143
+ export { INDEXV11 as default };
@@ -0,0 +1,155 @@
1
+ import { defineBot, getConfig, Text, At, OnProcessor, useParse } from 'alemonjs';
2
+ import { OneBotClient } from './sdk-v12/wss.js';
3
+
4
+ var INDEXV12 = defineBot(() => {
5
+ const cfg = getConfig();
6
+ const config = cfg.value?.onebot;
7
+ if (!config)
8
+ return;
9
+ //
10
+ const client = new OneBotClient({
11
+ // url
12
+ url: config?.url ?? '',
13
+ // token
14
+ access_token: config?.token ?? ''
15
+ });
16
+ //
17
+ client.connect();
18
+ //
19
+ client.on('META', event => {
20
+ const bot = event.status.bots[0];
21
+ if (!bot)
22
+ return;
23
+ if (bot.self) {
24
+ bot.self.user_id;
25
+ }
26
+ if (bot.nickname) {
27
+ bot.nickname;
28
+ }
29
+ if (bot.avatar) {
30
+ bot?.avatar ?? '';
31
+ }
32
+ });
33
+ //
34
+ client.on('MESSAGES', event => {
35
+ const uis = config.master_uids ?? [];
36
+ let msg = '';
37
+ const arr = [];
38
+ let at_users = [];
39
+ for (const item of event.message) {
40
+ if (item.type == 'mention') {
41
+ arr.push(item.data);
42
+ at_users.push({
43
+ avatar: '',
44
+ bot: false,
45
+ id: item.data.user_id,
46
+ name: item.data.text.replace(/^@/, '')
47
+ });
48
+ }
49
+ else if (item.type == 'text') {
50
+ msg = item.data.text;
51
+ }
52
+ }
53
+ for (const item of arr) {
54
+ msg = msg.replace(item.text, '').trim();
55
+ }
56
+ // 定义消
57
+ const e = {
58
+ // 平台类型
59
+ Platform: 'onebot',
60
+ // 频道
61
+ GuildId: event.group_id,
62
+ // guild_name: event.group_name,
63
+ // 子频道
64
+ ChannelId: event.group_id,
65
+ // 是否是主人
66
+ IsMaster: uis.includes(String(event.user_id)),
67
+ // 用户ID
68
+ UserId: event.user_id,
69
+ // 用户名
70
+ UserName: event.sender.nickname,
71
+ // 用户头像
72
+ UserAvatar: event.platform == 'qq' ? `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}` : '',
73
+ // 格式化数据
74
+ MsgId: event.message_id,
75
+ // 用户消息
76
+ Megs: [
77
+ Text(msg),
78
+ ...at_users.map(item => At(item.id, 'user', {
79
+ name: item.name,
80
+ avatar: item.avatar,
81
+ bot: item.bot
82
+ }))
83
+ ],
84
+ // 用户openId
85
+ OpenID: event.user_id,
86
+ //
87
+ tag: 'MESSAGES',
88
+ // 创建时间
89
+ CreateAt: Date.now(),
90
+ //
91
+ value: null
92
+ };
93
+ // 当访问的时候获取
94
+ Object.defineProperty(e, 'value', {
95
+ get() {
96
+ return event;
97
+ }
98
+ });
99
+ // 处理消息
100
+ OnProcessor(e, 'message.create');
101
+ });
102
+ client.on('DIRECT_MESSAGE', () => {
103
+ // const e = {
104
+ // isMaster: event.user_id == masterID,
105
+ // msg_txt: event.raw_message,
106
+ // msg: event.raw_message.trim(),
107
+ // msg_id: event.message_id,
108
+ // open_id: event.user_id,
109
+ // user_id: event.user_id,
110
+ // user_avatar:
111
+ // event.platform == 'qq'
112
+ // ? `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}`
113
+ // : '',
114
+ // user_name: event.sender.nickname,
115
+ });
116
+ // 错误处理
117
+ client.on('ERROR', event => {
118
+ console.error(event);
119
+ });
120
+ return {
121
+ api: {
122
+ use: {
123
+ send: (event, val) => {
124
+ if (val.length < 0)
125
+ return Promise.all([]);
126
+ const content = useParse(val, 'Text');
127
+ if (content) {
128
+ return Promise.all([content].map(item => client.postMessage(event.ChannelId, [
129
+ {
130
+ type: 'text',
131
+ data: {
132
+ text: item
133
+ }
134
+ }
135
+ ])));
136
+ }
137
+ const images = useParse(val, 'Image');
138
+ if (images) {
139
+ return Promise.all(images.map(item => client.postMessage(event.ChannelId, [
140
+ {
141
+ type: 'image',
142
+ data: {
143
+ file_id: `base64://${item.toString('base64')}`
144
+ }
145
+ }
146
+ ])));
147
+ }
148
+ return Promise.all([]);
149
+ }
150
+ }
151
+ }
152
+ };
153
+ });
154
+
155
+ export { INDEXV12 as default };
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare const _default: () => typeof global.alemonjs;
2
+
3
+ export { _default as default };
package/lib/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import { defineBot, getConfig } from 'alemonjs';
2
+ import INDEXV12 from './index-12.js';
3
+ import INDEXV11 from './index-11.js';
4
+
5
+ var index = defineBot(() => {
6
+ const cfg = getConfig();
7
+ const config = cfg.value?.onebot;
8
+ if (!config)
9
+ return;
10
+ if (config.version === 'v12') {
11
+ return INDEXV12();
12
+ }
13
+ return INDEXV11();
14
+ });
15
+
16
+ export { index as default };
@@ -0,0 +1,134 @@
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
+ sendGroupMsg(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
+ sendPrivateMsg(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 };
@@ -0,0 +1,115 @@
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 guild_id
100
+ * @param message
101
+ * @returns
102
+ */
103
+ postMessage(guild_id, message) {
104
+ return this.#ws.send(JSON.stringify({
105
+ action: 'send_group_msg',
106
+ params: {
107
+ group_id: guild_id,
108
+ message: message
109
+ },
110
+ echo: '1234'
111
+ }));
112
+ }
113
+ }
114
+
115
+ export { OneBotClient };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@alemonjs/onebot",
3
+ "version": "0.0.1",
4
+ "description": "onebot",
5
+ "author": "lemonade",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "lib/index.js",
9
+ "dependencies": {
10
+ "chat-space": "^0.0.6"
11
+ },
12
+ "types": "lib",
13
+ "exports": {
14
+ ".": {
15
+ "import": "./lib/index.js",
16
+ "types": "./lib/index.d.ts"
17
+ }
18
+ },
19
+ "keywords": [
20
+ "alemonjs",
21
+ "onebot",
22
+ "bot",
23
+ "chat-bot"
24
+ ],
25
+ "publishConfig": {
26
+ "registry": "https://registry.npmjs.org",
27
+ "access": "public"
28
+ },
29
+ "bugs": "https://github.com/ningmengchongshui/alemonjs/issues",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/ningmengchongshui/alemonjs.git"
33
+ }
34
+ }