@alemonjs/discord 0.0.1 → 0.0.2

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 CHANGED
@@ -1,9 +1,5 @@
1
- import { ConfigType } from 'alemonjs'
1
+ import { ConfigType } from 'alemonjs';
2
2
 
3
- /**
4
- *
5
- * @param val
6
- */
7
- declare const login: (config: ConfigType) => void
3
+ declare const _default: (_: ConfigType) => typeof global.alemonjs;
8
4
 
9
- export { login }
5
+ export { _default as default };
package/lib/index.js CHANGED
@@ -1,119 +1,104 @@
1
- import { Text, At, OnProcessor, useParse } from 'alemonjs'
2
- import { DCClient } from 'chat-space'
1
+ import { defineBot, Text, At, OnProcessor, useParse } from 'alemonjs';
2
+ import { DCClient } from 'chat-space';
3
3
 
4
- /**
5
- *
6
- * @param val
7
- */
8
- const login = config => {
9
- // 创建客户端
10
- const client = new DCClient({
11
- token: config.token
12
- })
13
- // 连接
14
- client.connect()
15
- // 监听消息
16
- client.on('MESSAGE_CREATE', async event => {
17
- // 消除bot消息
18
- if (event.author?.bot) return
19
- const master_id = config?.master_id ?? []
20
- const isMaster = master_id.includes(event.author.id)
21
- // 艾特消息处理
22
- const at_users = []
23
- // 获取艾特用户
24
- for (const item of event.mentions) {
25
- at_users.push({
26
- id: item.id,
27
- name: item.username,
28
- avatar: client.userAvatar(item.id, item.avatar),
29
- bot: item.bot
30
- })
31
- }
32
- // 清除 @ 相关的消息
33
- let msg = event.content
34
- for await (const item of at_users) {
35
- msg = msg.replace(`<@${item.id}>`, '').trim()
36
- }
37
- // 定义消
38
- const e = {
39
- // 事件类型
40
- Platform: 'kook',
41
- // 频道
42
- GuildId: event.guild_id,
43
- // 子频道
44
- ChannelId: event.channel_id,
45
- // 是否是主人
46
- IsMaster: isMaster,
47
- // 用户ID
48
- UserId: event.author.id,
49
- // 用户名
50
- UserName: event.author.username,
51
- // 用户头像
52
- UserAvatar: client.userAvatar(event.author.id, event.author.avatar),
53
- // 格式化数据
54
- MsgId: event.id,
55
- // 用户消息
56
- Megs: [
57
- Text(msg),
58
- ...at_users.map(item =>
59
- At(item.id, 'user', {
60
- name: item.name,
61
- avatar: item.avatar,
62
- bot: item.bot
63
- })
64
- )
65
- ],
66
- // 用户openId
67
- OpenID: '',
68
- // 创建时间
69
- CreateAt: Date.now(),
70
- //
71
- value: null
72
- }
73
- // 当访问的时候获取
74
- Object.defineProperty(e, 'value', {
75
- get() {
76
- return event
77
- }
78
- })
79
- // 处理消息
80
- OnProcessor(e, 'message.create')
81
- })
82
- // 发送错误时
83
- client.on('ERROR', msg => {
84
- console.error(msg)
85
- })
86
- /**
87
- * 开始实现全局接口
88
- */
89
- if (!global?.alemonjs) {
90
- global.alemonjs = {
91
- api: {
92
- use: {
93
- send: (event, val) => {
94
- if (val.length < 0) return
95
- const content = useParse(val, 'Text')
96
- if (content) {
97
- return Promise.all(
98
- [content].map(item =>
99
- client.channelsMessages(event.ChannelId, {
100
- content: item
101
- })
102
- )
103
- )
4
+ var index = defineBot((config) => {
5
+ // 创建客户端
6
+ const client = new DCClient({
7
+ token: config.token
8
+ });
9
+ // 连接
10
+ client.connect();
11
+ // 监听消息
12
+ client.on('MESSAGE_CREATE', async (event) => {
13
+ // 消除bot消息
14
+ if (event.author?.bot)
15
+ return;
16
+ const master_id = config?.master_id ?? [];
17
+ const isMaster = master_id.includes(event.author.id);
18
+ // 艾特消息处理
19
+ const at_users = [];
20
+ // 获取艾特用户
21
+ for (const item of event.mentions) {
22
+ at_users.push({
23
+ id: item.id,
24
+ name: item.username,
25
+ avatar: client.userAvatar(item.id, item.avatar),
26
+ bot: item.bot
27
+ });
28
+ }
29
+ // 清除 @ 相关的消息
30
+ let msg = event.content;
31
+ for await (const item of at_users) {
32
+ msg = msg.replace(`<@${item.id}>`, '').trim();
33
+ }
34
+ // 定义消
35
+ const e = {
36
+ // 事件类型
37
+ Platform: 'discord',
38
+ // 频道
39
+ GuildId: event.guild_id,
40
+ // 子频道
41
+ ChannelId: event.channel_id,
42
+ // 是否是主人
43
+ IsMaster: isMaster,
44
+ // 用户ID
45
+ UserId: event.author.id,
46
+ // 用户名
47
+ UserName: event.author.username,
48
+ // 用户头像
49
+ UserAvatar: client.userAvatar(event.author.id, event.author.avatar),
50
+ // 格式化数据
51
+ MsgId: event.id,
52
+ // 用户消息
53
+ Megs: [
54
+ Text(msg),
55
+ ...at_users.map(item => At(item.id, 'user', {
56
+ name: item.name,
57
+ avatar: item.avatar,
58
+ bot: item.bot
59
+ }))
60
+ ],
61
+ // 用户openId
62
+ OpenID: '',
63
+ // 创建时间
64
+ CreateAt: Date.now(),
65
+ //
66
+ value: null
67
+ };
68
+ // 当访问的时候获取
69
+ Object.defineProperty(e, 'value', {
70
+ get() {
71
+ return event;
104
72
  }
105
- const images = useParse(val, 'Image')
106
- if (images) {
107
- return Promise.all(
108
- images.map(item => client.channelsMessagesImage(event.ChannelId, item))
109
- )
73
+ });
74
+ // 处理消息
75
+ OnProcessor(e, 'message.create');
76
+ });
77
+ // 发送错误时
78
+ client.on('ERROR', msg => {
79
+ console.error(msg);
80
+ });
81
+ return {
82
+ api: {
83
+ use: {
84
+ send: (event, val) => {
85
+ if (val.length < 0)
86
+ return Promise.all([]);
87
+ const content = useParse(val, 'Text');
88
+ if (content) {
89
+ return Promise.all([content].map(item => client.channelsMessages(event.ChannelId, {
90
+ content: item
91
+ })));
92
+ }
93
+ const images = useParse(val, 'Image');
94
+ if (images) {
95
+ return Promise.all(images.map(item => client.channelsMessagesImage(event.ChannelId, item)));
96
+ }
97
+ return Promise.all([]);
98
+ }
110
99
  }
111
- return Promise.resolve()
112
- }
113
100
  }
114
- }
115
- }
116
- }
117
- }
101
+ };
102
+ });
118
103
 
119
- export { login }
104
+ export { index as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "啊柠檬脚本",
5
5
  "author": "ningmengchongshui",
6
6
  "license": "MIT",