@alemonjs/discord 0.0.5 → 0.1.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/lib/index.d.ts +5 -1
- package/lib/index.js +50 -10
- package/lib/sdk/core/config.js +46 -0
- package/lib/sdk/core/utils/from.js +42 -0
- package/lib/sdk/platform/discord/sdk/api.d.ts +896 -0
- package/lib/sdk/platform/discord/sdk/api.js +1554 -0
- package/lib/sdk/platform/discord/sdk/config.js +29 -0
- package/lib/sdk/platform/discord/sdk/intents.js +36 -0
- package/lib/sdk/platform/discord/sdk/message/CHANNEL_TOPIC_UPDATE.d.ts +11 -0
- package/lib/sdk/platform/discord/sdk/message/CHANNEL_UPDATE.d.ts +48 -0
- package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_ADD.d.ts +28 -0
- package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_REMOVE.d.ts +18 -0
- package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_UPDATE.d.ts +33 -0
- package/lib/sdk/platform/discord/sdk/message/MESSAGE_CREATE.d.ts +59 -0
- package/lib/sdk/platform/discord/sdk/message/MESSAGE_DELETE.d.ts +11 -0
- package/lib/sdk/platform/discord/sdk/message/MESSAGE_REACTION_ADD.d.ts +42 -0
- package/lib/sdk/platform/discord/sdk/message/MESSAGE_UPDATE.d.ts +56 -0
- package/lib/sdk/platform/discord/sdk/message/PRESENCE_UPDATE.d.ts +63 -0
- package/lib/sdk/platform/discord/sdk/message/READY.d.ts +9 -0
- package/lib/sdk/platform/discord/sdk/message/TYPING_START.d.ts +35 -0
- package/lib/sdk/platform/discord/sdk/message/VOICE_CHANNEL_STATUS_UPDATE.d.ts +10 -0
- package/lib/sdk/platform/discord/sdk/message/VOICE_STATE_UPDATE.d.ts +41 -0
- package/lib/sdk/platform/discord/sdk/message.d.ts +37 -0
- package/lib/sdk/platform/discord/sdk/types.d.ts +4 -0
- package/lib/sdk/platform/discord/sdk/wss.d.ts +27 -0
- package/lib/sdk/platform/discord/sdk/wss.js +201 -0
- package/lib/sdk/platform/discord/sdk/wss.types.d.ts +26 -0
- package/package.json +3 -6
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { DCClient } from './sdk/platform/discord/sdk/wss.js';
|
|
2
|
+
|
|
3
|
+
type Client = typeof DCClient.prototype;
|
|
4
|
+
declare const client: Client;
|
|
1
5
|
declare const _default: () => typeof global.alemonjs;
|
|
2
6
|
|
|
3
|
-
export { _default as default };
|
|
7
|
+
export { type Client, client, _default as default };
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
import { defineBot, getConfig, Text, At, OnProcessor, useParse } from 'alemonjs';
|
|
2
|
-
import
|
|
1
|
+
import { defineBot, getConfig, createHash, Text, At, OnProcessor, useParse } from 'alemonjs';
|
|
2
|
+
import 'fs';
|
|
3
|
+
import 'axios';
|
|
4
|
+
import 'path';
|
|
5
|
+
import 'qrcode';
|
|
6
|
+
import 'public-ip';
|
|
7
|
+
import 'stream';
|
|
8
|
+
import 'file-type';
|
|
9
|
+
import 'form-data';
|
|
10
|
+
import { DCClient } from './sdk/platform/discord/sdk/wss.js';
|
|
3
11
|
|
|
12
|
+
const client = global.client;
|
|
4
13
|
var index = defineBot(() => {
|
|
5
14
|
const cfg = getConfig();
|
|
6
15
|
const config = cfg.value?.discord;
|
|
@@ -35,6 +44,30 @@ var index = defineBot(() => {
|
|
|
35
44
|
for await (const item of at_users) {
|
|
36
45
|
msg = msg.replace(`<@${item.id}>`, '').trim();
|
|
37
46
|
}
|
|
47
|
+
const UserKey = createHash(`gui:${event.author.id}`);
|
|
48
|
+
let url = null;
|
|
49
|
+
const UserAvatar = {
|
|
50
|
+
toBuffer: async () => {
|
|
51
|
+
if (!url) {
|
|
52
|
+
url = client.userAvatar(event.author.id, event.author.avatar);
|
|
53
|
+
}
|
|
54
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
55
|
+
return Buffer.from(arrayBuffer);
|
|
56
|
+
},
|
|
57
|
+
toBase64: async () => {
|
|
58
|
+
if (!url) {
|
|
59
|
+
url = client.userAvatar(event.author.id, event.author.avatar);
|
|
60
|
+
}
|
|
61
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
62
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
63
|
+
},
|
|
64
|
+
toURL: async () => {
|
|
65
|
+
if (!url) {
|
|
66
|
+
url = client.userAvatar(event.author.id, event.author.avatar);
|
|
67
|
+
}
|
|
68
|
+
return url;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
38
71
|
// 定义消
|
|
39
72
|
const e = {
|
|
40
73
|
// 事件类型
|
|
@@ -45,16 +78,17 @@ var index = defineBot(() => {
|
|
|
45
78
|
ChannelId: event.channel_id,
|
|
46
79
|
// 是否是主人
|
|
47
80
|
IsMaster: isMaster,
|
|
48
|
-
// 用户
|
|
81
|
+
// 用户Id
|
|
49
82
|
UserId: event.author.id,
|
|
50
83
|
// 用户名
|
|
51
84
|
UserName: event.author.username,
|
|
85
|
+
UserKey,
|
|
52
86
|
// 用户头像
|
|
53
|
-
UserAvatar:
|
|
87
|
+
UserAvatar: UserAvatar,
|
|
54
88
|
// 格式化数据
|
|
55
|
-
|
|
89
|
+
MessageId: event.id,
|
|
56
90
|
// 用户消息
|
|
57
|
-
|
|
91
|
+
MessageBody: [
|
|
58
92
|
Text(msg),
|
|
59
93
|
...at_users.map(item => At(item.id, 'user', {
|
|
60
94
|
name: item.name,
|
|
@@ -62,8 +96,9 @@ var index = defineBot(() => {
|
|
|
62
96
|
bot: item.bot
|
|
63
97
|
}))
|
|
64
98
|
],
|
|
99
|
+
MessageText: msg,
|
|
65
100
|
// 用户openId
|
|
66
|
-
|
|
101
|
+
OpenId: '',
|
|
67
102
|
// 创建时间
|
|
68
103
|
CreateAt: Date.now(),
|
|
69
104
|
tag: 'MESSAGE_CREATE',
|
|
@@ -83,19 +118,24 @@ var index = defineBot(() => {
|
|
|
83
118
|
client.on('ERROR', msg => {
|
|
84
119
|
console.error(msg);
|
|
85
120
|
});
|
|
121
|
+
global.client = client;
|
|
86
122
|
return {
|
|
87
123
|
api: {
|
|
88
124
|
use: {
|
|
89
125
|
send: (event, val) => {
|
|
90
126
|
if (val.length < 0)
|
|
91
127
|
return Promise.all([]);
|
|
92
|
-
const content = useParse(
|
|
128
|
+
const content = useParse({
|
|
129
|
+
MessageBody: val
|
|
130
|
+
}, 'Text');
|
|
93
131
|
if (content) {
|
|
94
132
|
return Promise.all([content].map(item => client.channelsMessages(event.ChannelId, {
|
|
95
133
|
content: item
|
|
96
134
|
})));
|
|
97
135
|
}
|
|
98
|
-
const images = useParse(
|
|
136
|
+
const images = useParse({
|
|
137
|
+
MessageBody: val
|
|
138
|
+
}, 'Image');
|
|
99
139
|
if (images) {
|
|
100
140
|
return Promise.all(images.map(item => client.channelsMessagesImage(event.ChannelId, item)));
|
|
101
141
|
}
|
|
@@ -106,4 +146,4 @@ var index = defineBot(() => {
|
|
|
106
146
|
};
|
|
107
147
|
});
|
|
108
148
|
|
|
109
|
-
export { index as default };
|
|
149
|
+
export { client, index as default };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 基础配置结构
|
|
3
|
+
*/
|
|
4
|
+
class BaseConfig {
|
|
5
|
+
#data = null;
|
|
6
|
+
constructor(val) {
|
|
7
|
+
this.#data = val;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 设置配置
|
|
11
|
+
* @param key
|
|
12
|
+
* @param val
|
|
13
|
+
*/
|
|
14
|
+
set(key, val) {
|
|
15
|
+
if (val !== undefined)
|
|
16
|
+
this.#data[key] = val;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param key
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
has(key) {
|
|
25
|
+
if (Object.prototype.hasOwnProperty.call(this.#data, key)) ;
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 读取配置
|
|
30
|
+
* @param key
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
all() {
|
|
34
|
+
return this.#data;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 读取配置
|
|
38
|
+
* @param key
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
get(key) {
|
|
42
|
+
return this.#data[key];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { BaseConfig };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { existsSync, createReadStream } from 'fs';
|
|
2
|
+
import { Readable, isReadable } from 'stream';
|
|
3
|
+
import { basename } from 'path';
|
|
4
|
+
import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 创建form
|
|
8
|
+
* @param image
|
|
9
|
+
* @param name
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
async function createPicFrom(image, name = 'image.jpg') {
|
|
13
|
+
let picData;
|
|
14
|
+
// 是 string
|
|
15
|
+
if (typeof image === 'string') {
|
|
16
|
+
if (!existsSync(image))
|
|
17
|
+
return false;
|
|
18
|
+
if (!name)
|
|
19
|
+
name = basename(image);
|
|
20
|
+
picData = createReadStream(image);
|
|
21
|
+
// 是 buffer
|
|
22
|
+
}
|
|
23
|
+
else if (Buffer.isBuffer(image)) {
|
|
24
|
+
if (!name)
|
|
25
|
+
name = 'file.' + (await fileTypeFromBuffer(image)).ext;
|
|
26
|
+
picData = new Readable();
|
|
27
|
+
picData.push(image);
|
|
28
|
+
picData.push(null);
|
|
29
|
+
// 是 文件流
|
|
30
|
+
}
|
|
31
|
+
else if (isReadable(image)) {
|
|
32
|
+
if (!name)
|
|
33
|
+
name = 'file.' + (await fileTypeFromStream(image)).ext;
|
|
34
|
+
picData = image;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return { picData, image, name };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { createPicFrom };
|