alemonjs 1.0.0

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.
Files changed (169) hide show
  1. package/LICENSE +340 -0
  2. package/README.md +54 -0
  3. package/lib/alemon/app.js +38 -0
  4. package/lib/alemon/apps.js +121 -0
  5. package/lib/alemon/buffer.js +38 -0
  6. package/lib/alemon/dealmsg.js +423 -0
  7. package/lib/alemon/dialogue.js +61 -0
  8. package/lib/alemon/index.js +40 -0
  9. package/lib/alemon/message.js +27 -0
  10. package/lib/alemon/plugin.js +44 -0
  11. package/lib/alemon/puppeteer.js +226 -0
  12. package/lib/alemon/qrcode.js +66 -0
  13. package/lib/alemon/screenshot.js +126 -0
  14. package/lib/alemon/typings.js +96 -0
  15. package/lib/config.js +25 -0
  16. package/lib/discord/alemon/api.js +44 -0
  17. package/lib/discord/alemon/bot.js +11 -0
  18. package/lib/discord/alemon/conversation.js +7 -0
  19. package/lib/discord/alemon/message/PUBLIC_GUILD_MESSAGES.js +185 -0
  20. package/lib/discord/alemon/segment.js +62 -0
  21. package/lib/discord/index.js +50 -0
  22. package/lib/discord/login.js +99 -0
  23. package/lib/index.js +12 -0
  24. package/lib/kook/alemon/bot.js +11 -0
  25. package/lib/kook/alemon/conversation.js +56 -0
  26. package/lib/kook/alemon/message/PUBLIC_GUILD_MESSAGES.js +195 -0
  27. package/lib/kook/alemon/segment.js +90 -0
  28. package/lib/kook/index.js +39 -0
  29. package/lib/kook/login.js +53 -0
  30. package/lib/login.js +112 -0
  31. package/lib/main.js +106 -0
  32. package/lib/ntqq/alemon/bot.js +11 -0
  33. package/lib/ntqq/alemon/conversation.js +18 -0
  34. package/lib/ntqq/alemon/message/C2C_MESSAGE_CREATE.js +142 -0
  35. package/lib/ntqq/alemon/message/GROUP_AT_MESSAGE_CREATE.js +218 -0
  36. package/lib/ntqq/alemon/segment.js +29 -0
  37. package/lib/ntqq/alemon/types.js +1 -0
  38. package/lib/ntqq/index.js +77 -0
  39. package/lib/ntqq/login.js +74 -0
  40. package/lib/ntqq/sdk/api.js +158 -0
  41. package/lib/ntqq/sdk/config.js +46 -0
  42. package/lib/ntqq/sdk/index.js +5 -0
  43. package/lib/ntqq/sdk/intents.js +69 -0
  44. package/lib/ntqq/sdk/types.js +1 -0
  45. package/lib/ntqq/sdk/typings.js +9 -0
  46. package/lib/ntqq/sdk/web/client.js +89 -0
  47. package/lib/ntqq/sdk/web/img.js +76 -0
  48. package/lib/ntqq/sdk/web/index.js +4 -0
  49. package/lib/ntqq/sdk/web/ip.js +24 -0
  50. package/lib/ntqq/sdk/wss.js +178 -0
  51. package/lib/qq/alemon/apidoc.js +216 -0
  52. package/lib/qq/alemon/bot.js +11 -0
  53. package/lib/qq/alemon/console.js +13 -0
  54. package/lib/qq/alemon/conversation.js +139 -0
  55. package/lib/qq/alemon/doc.js +93 -0
  56. package/lib/qq/alemon/e.js +41 -0
  57. package/lib/qq/alemon/message/AUDIO_ACTION.js +57 -0
  58. package/lib/qq/alemon/message/DIRECT_MESSAGE.js +202 -0
  59. package/lib/qq/alemon/message/FORUMS_EVENT.js +66 -0
  60. package/lib/qq/alemon/message/GUILDS.js +65 -0
  61. package/lib/qq/alemon/message/GUILD_MEMBERS.js +153 -0
  62. package/lib/qq/alemon/message/GUILD_MESSAGES.js +50 -0
  63. package/lib/qq/alemon/message/GUILD_MESSAGE_REACTIONS.js +37 -0
  64. package/lib/qq/alemon/message/INTERACTION.js +36 -0
  65. package/lib/qq/alemon/message/MESSAGE.js +303 -0
  66. package/lib/qq/alemon/message/MESSAGE_AUDIT.js +41 -0
  67. package/lib/qq/alemon/message/OPEN_FORUMS_EVENT.js +74 -0
  68. package/lib/qq/alemon/message/PUBLIC_GUILD_MESSAGES.js +52 -0
  69. package/lib/qq/alemon/permissions.js +43 -0
  70. package/lib/qq/alemon/privatechat.js +78 -0
  71. package/lib/qq/alemon/segment.js +25 -0
  72. package/lib/qq/alemon/types.js +25 -0
  73. package/lib/qq/index.js +44 -0
  74. package/lib/qq/login.js +151 -0
  75. package/lib/types.js +5 -0
  76. package/lib/villa/alemon/conversation.js +48 -0
  77. package/lib/villa/alemon/message/GUILDS.js +124 -0
  78. package/lib/villa/alemon/message/GUILD_MEMBERS.js +128 -0
  79. package/lib/villa/alemon/message/GUILD_MESSAGE_REACTIONS.js +127 -0
  80. package/lib/villa/alemon/message/MESSAGES.js +334 -0
  81. package/lib/villa/alemon/message/MESSAGE_AUDIT.js +128 -0
  82. package/lib/villa/alemon/message/e.js +41 -0
  83. package/lib/villa/alemon/segment.js +34 -0
  84. package/lib/villa/index.js +46 -0
  85. package/lib/villa/login.js +62 -0
  86. package/package.json +63 -0
  87. package/types/alemon/app.d.ts +22 -0
  88. package/types/alemon/apps.d.ts +41 -0
  89. package/types/alemon/buffer.d.ts +13 -0
  90. package/types/alemon/dealmsg.d.ts +41 -0
  91. package/types/alemon/dialogue.d.ts +55 -0
  92. package/types/alemon/index.d.ts +40 -0
  93. package/types/alemon/message.d.ts +17 -0
  94. package/types/alemon/plugin.d.ts +69 -0
  95. package/types/alemon/puppeteer.d.ts +53 -0
  96. package/types/alemon/qrcode.d.ts +20 -0
  97. package/types/alemon/screenshot.d.ts +23 -0
  98. package/types/alemon/typings.d.ts +525 -0
  99. package/types/config.d.ts +12 -0
  100. package/types/discord/alemon/api.d.ts +11 -0
  101. package/types/discord/alemon/bot.d.ts +6 -0
  102. package/types/discord/alemon/conversation.d.ts +6 -0
  103. package/types/discord/alemon/message/PUBLIC_GUILD_MESSAGES.d.ts +7 -0
  104. package/types/discord/alemon/segment.d.ts +46 -0
  105. package/types/discord/index.d.ts +1 -0
  106. package/types/discord/login.d.ts +7 -0
  107. package/types/index.d.ts +6 -0
  108. package/types/kook/alemon/bot.d.ts +8 -0
  109. package/types/kook/alemon/conversation.d.ts +7 -0
  110. package/types/kook/alemon/message/PUBLIC_GUILD_MESSAGES.d.ts +6 -0
  111. package/types/kook/alemon/segment.d.ts +66 -0
  112. package/types/kook/index.d.ts +5 -0
  113. package/types/kook/login.d.ts +7 -0
  114. package/types/login.d.ts +18 -0
  115. package/types/main.d.ts +15 -0
  116. package/types/ntqq/alemon/bot.d.ts +8 -0
  117. package/types/ntqq/alemon/conversation.d.ts +8 -0
  118. package/types/ntqq/alemon/message/C2C_MESSAGE_CREATE.d.ts +2 -0
  119. package/types/ntqq/alemon/message/GROUP_AT_MESSAGE_CREATE.d.ts +8 -0
  120. package/types/ntqq/alemon/segment.d.ts +20 -0
  121. package/types/ntqq/alemon/types.d.ts +11 -0
  122. package/types/ntqq/index.d.ts +1 -0
  123. package/types/ntqq/login.d.ts +6 -0
  124. package/types/ntqq/sdk/api.d.ts +44 -0
  125. package/types/ntqq/sdk/config.d.ts +23 -0
  126. package/types/ntqq/sdk/index.d.ts +5 -0
  127. package/types/ntqq/sdk/intents.d.ts +2 -0
  128. package/types/ntqq/sdk/types.d.ts +11 -0
  129. package/types/ntqq/sdk/typings.d.ts +17 -0
  130. package/types/ntqq/sdk/web/client.d.ts +8 -0
  131. package/types/ntqq/sdk/web/img.d.ts +20 -0
  132. package/types/ntqq/sdk/web/index.d.ts +4 -0
  133. package/types/ntqq/sdk/web/ip.d.ts +5 -0
  134. package/types/ntqq/sdk/wss.d.ts +35 -0
  135. package/types/qq/alemon/apidoc.d.ts +154 -0
  136. package/types/qq/alemon/bot.d.ts +8 -0
  137. package/types/qq/alemon/console.d.ts +1 -0
  138. package/types/qq/alemon/conversation.d.ts +5 -0
  139. package/types/qq/alemon/doc.d.ts +93 -0
  140. package/types/qq/alemon/e.d.ts +38 -0
  141. package/types/qq/alemon/message/AUDIO_ACTION.d.ts +15 -0
  142. package/types/qq/alemon/message/DIRECT_MESSAGE.d.ts +16 -0
  143. package/types/qq/alemon/message/FORUMS_EVENT.d.ts +25 -0
  144. package/types/qq/alemon/message/GUILDS.d.ts +19 -0
  145. package/types/qq/alemon/message/GUILD_MEMBERS.d.ts +11 -0
  146. package/types/qq/alemon/message/GUILD_MESSAGES.d.ts +12 -0
  147. package/types/qq/alemon/message/GUILD_MESSAGE_REACTIONS.d.ts +9 -0
  148. package/types/qq/alemon/message/INTERACTION.d.ts +8 -0
  149. package/types/qq/alemon/message/MESSAGE.d.ts +16 -0
  150. package/types/qq/alemon/message/MESSAGE_AUDIT.d.ts +9 -0
  151. package/types/qq/alemon/message/OPEN_FORUMS_EVENT.d.ts +28 -0
  152. package/types/qq/alemon/message/PUBLIC_GUILD_MESSAGES.d.ts +12 -0
  153. package/types/qq/alemon/permissions.d.ts +29 -0
  154. package/types/qq/alemon/privatechat.d.ts +14 -0
  155. package/types/qq/alemon/segment.d.ts +19 -0
  156. package/types/qq/alemon/types.d.ts +82 -0
  157. package/types/qq/index.d.ts +6 -0
  158. package/types/qq/login.d.ts +6 -0
  159. package/types/types.d.ts +85 -0
  160. package/types/villa/alemon/conversation.d.ts +7 -0
  161. package/types/villa/alemon/message/GUILDS.d.ts +10 -0
  162. package/types/villa/alemon/message/GUILD_MEMBERS.d.ts +14 -0
  163. package/types/villa/alemon/message/GUILD_MESSAGE_REACTIONS.d.ts +7 -0
  164. package/types/villa/alemon/message/MESSAGES.d.ts +7 -0
  165. package/types/villa/alemon/message/MESSAGE_AUDIT.d.ts +10 -0
  166. package/types/villa/alemon/message/e.d.ts +37 -0
  167. package/types/villa/alemon/segment.d.ts +26 -0
  168. package/types/villa/index.d.ts +1 -0
  169. package/types/villa/login.d.ts +7 -0
@@ -0,0 +1,185 @@
1
+ import { InstructionMatching } from '../../../alemon/index.js';
2
+ import { segmentDiscord } from '../segment.js';
3
+ import { getBotMsgByDiscord } from '../bot.js';
4
+ import { postImage } from '../api.js';
5
+ import { getBotConfigByKey } from '../../../login.js';
6
+ /**
7
+ * 公共
8
+ * @param event
9
+ * @returns
10
+ */
11
+ export const PUBLIC_GUILD_MESSAGES_DISCORD = async (event) => {
12
+ /**
13
+ * 忽视机器人
14
+ */
15
+ if (event.author.bot) {
16
+ return;
17
+ }
18
+ /**
19
+ * 艾特消息处理
20
+ */
21
+ const at_users = [];
22
+ const obj = Object.fromEntries(event.mentions.users);
23
+ /**
24
+ *
25
+ */
26
+ for (const item in obj) {
27
+ at_users.push({
28
+ id: obj[item].id,
29
+ name: obj[item].username,
30
+ avatar: obj[item].avatarURL({
31
+ extension: 'png',
32
+ forceStatic: true,
33
+ size: 1024
34
+ }),
35
+ bot: obj[item].bot
36
+ });
37
+ }
38
+ /**
39
+ * 清除 @ 相关
40
+ */
41
+ let msg = event.content;
42
+ for await (const item of at_users) {
43
+ msg = msg.replace(`<@${item.id}>`, '').trim();
44
+ }
45
+ /**
46
+ * 艾特处理
47
+ */
48
+ let at = false;
49
+ let at_user = undefined;
50
+ if (at_users.some(item => item.bot != true)) {
51
+ at = true;
52
+ }
53
+ if (at) {
54
+ at_user = at_users.find(item => item.bot != true);
55
+ }
56
+ /**
57
+ * 主人处理
58
+ */
59
+ let isMaster = false;
60
+ /**
61
+ * 得到登录配置
62
+ */
63
+ const cfg = getBotConfigByKey('discord');
64
+ /**
65
+ * 检查身份
66
+ */
67
+ if (event.author.id == cfg.masterID) {
68
+ /**
69
+ * 是主人
70
+ */
71
+ isMaster = true;
72
+ }
73
+ /**
74
+ * 构造e对象
75
+ */
76
+ const e = {
77
+ platform: 'discord',
78
+ /**
79
+ * 机器人信息 tudo
80
+ */
81
+ bot: getBotMsgByDiscord(),
82
+ /**
83
+ * 频道
84
+ */
85
+ guild_id: event.guildId,
86
+ channel_id: event.channelId,
87
+ isPrivate: true,
88
+ isRecall: false,
89
+ isMaster: isMaster,
90
+ isGroup: true,
91
+ /**
92
+ * 消息事件
93
+ */
94
+ event: 'MESSAGES',
95
+ eventType: 'CREATE',
96
+ msg_txt: event.content,
97
+ msg_id: event.id,
98
+ msg: msg,
99
+ /**
100
+ * 艾特消息
101
+ */
102
+ at,
103
+ at_user,
104
+ at_users,
105
+ msg_create_time: new Date().getTime(),
106
+ /**
107
+ * 用户
108
+ */
109
+ user_id: event.author.id,
110
+ user_name: event.author.username,
111
+ user_avatar: event.author.avatarURL({
112
+ extension: 'png',
113
+ forceStatic: true,
114
+ size: 1024
115
+ }),
116
+ /**
117
+ * 模板函数
118
+ */
119
+ segment: segmentDiscord,
120
+ /**
121
+ * 消息发送机制
122
+ * @param content 消息内容
123
+ * @param img 额外消息 可选
124
+ */
125
+ reply: async (msg, img, name) => {
126
+ if (Buffer.isBuffer(msg)) {
127
+ try {
128
+ const attach = await postImage(msg, typeof img == 'string' ? img : undefined);
129
+ if (attach) {
130
+ await event.channel.send({ files: [attach] });
131
+ return true;
132
+ }
133
+ else {
134
+ return false;
135
+ }
136
+ }
137
+ catch (err) {
138
+ console.error(err);
139
+ return false;
140
+ }
141
+ }
142
+ const content = Array.isArray(msg)
143
+ ? msg.join('')
144
+ : typeof msg === 'string'
145
+ ? msg
146
+ : undefined;
147
+ if (Buffer.isBuffer(img)) {
148
+ try {
149
+ const attach = await postImage(img, name);
150
+ if (attach) {
151
+ await event.channel.send({ content, files: [attach] });
152
+ return true;
153
+ }
154
+ else {
155
+ return false;
156
+ }
157
+ }
158
+ catch (err) {
159
+ console.error(err);
160
+ return false;
161
+ }
162
+ }
163
+ try {
164
+ await event.channel.send(content);
165
+ return true;
166
+ }
167
+ catch {
168
+ return false;
169
+ }
170
+ }
171
+ };
172
+ /**
173
+ * 消息处理
174
+ */
175
+ await InstructionMatching(e)
176
+ .then(() => {
177
+ console.info(`\n[${e.channel_id}] [${e.user_name}] [${true}] ${e.msg_txt}`);
178
+ return true;
179
+ })
180
+ .catch((err) => {
181
+ console.error(err);
182
+ console.info(`\n[${e.channel_id}] [${e.user_name}] [${false}] ${e.msg_txt}`);
183
+ return false;
184
+ });
185
+ };
@@ -0,0 +1,62 @@
1
+ export const segmentDiscord = {
2
+ /**
3
+ * 艾特某人
4
+ * @param uid
5
+ * @returns
6
+ */
7
+ at: function (uid) {
8
+ return `<@${uid}>`;
9
+ },
10
+ /**
11
+ * 艾特全体
12
+ * @returns
13
+ */
14
+ atAll: function atAll() {
15
+ return `@everyone`;
16
+ },
17
+ /**
18
+ * 子频道引用
19
+ * @param channel_id
20
+ * @returns
21
+ */
22
+ atChannel: function (channel_id) {
23
+ return `<#${channel_id}>`;
24
+ },
25
+ /**
26
+ * 蓝链接
27
+ * @param name
28
+ * @param url
29
+ * @returns
30
+ */
31
+ link: function (name, url) {
32
+ return `[${name}](${url})`;
33
+ },
34
+ /**
35
+ * 代码块
36
+ * @param txt
37
+ */
38
+ block: function (txt) {
39
+ return `\`${txt}\``;
40
+ },
41
+ /**
42
+ * 加粗
43
+ * @param txt
44
+ */
45
+ Bold: function (txt) {
46
+ return `**${txt}**`;
47
+ },
48
+ /**
49
+ * 斜体
50
+ * @param txt
51
+ */
52
+ italic: function (txt) {
53
+ return `*${txt}*`;
54
+ },
55
+ /**
56
+ * 删除线
57
+ * @param txt
58
+ */
59
+ strikethrough: function (txt) {
60
+ return `~~${txt}~~`;
61
+ }
62
+ };
@@ -0,0 +1,50 @@
1
+ import { Client } from 'discord.js';
2
+ import { checkRobotByDiscord } from './login.js';
3
+ import { callBackByDisdord } from './alemon/conversation.js';
4
+ import { setBotMsgByDiscord } from './alemon/bot.js';
5
+ import { getBotConfigByKey } from '../login.js';
6
+ export async function createAlemonByDiscord() {
7
+ /**
8
+ * 登录
9
+ */
10
+ if (await checkRobotByDiscord().catch(err => {
11
+ console.error(err);
12
+ process.exit();
13
+ })) {
14
+ /**
15
+ * 读取配置
16
+ */
17
+ const cfg = getBotConfigByKey('discord');
18
+ /**
19
+ * 创建程序
20
+ */
21
+ const client = new Client({
22
+ intents: cfg.intents
23
+ });
24
+ /**
25
+ * 启动准备
26
+ */
27
+ client.on('ready', () => {
28
+ setBotMsgByDiscord({
29
+ id: client.user?.id ?? '',
30
+ name: client.user?.username ?? '',
31
+ avatar: client.user?.avatarURL({
32
+ extension: 'png',
33
+ forceStatic: true,
34
+ size: 1024
35
+ }) ?? ''
36
+ });
37
+ console.info(`欢迎回来 ${client.user?.username}`);
38
+ });
39
+ /**
40
+ * 监听消息创建
41
+ */
42
+ client.on('messageCreate', callBackByDisdord);
43
+ /**
44
+ * 登录
45
+ */
46
+ client.login(cfg.token);
47
+ return true;
48
+ }
49
+ return false;
50
+ }
@@ -0,0 +1,99 @@
1
+ import prompts from 'prompts';
2
+ import { GatewayIntentBits } from 'discord.js';
3
+ import { setBotConfigByKey, getBotConfigByKey } from '../login.js';
4
+ import { getToml, writeToml } from '../config.js';
5
+ /**
6
+ * 登录配置
7
+ * @param Bcf
8
+ * @param val
9
+ * @returns
10
+ */
11
+ export async function checkRobotByDiscord() {
12
+ /**
13
+ * 读取配置
14
+ */
15
+ if (process.argv.indexOf('login') == -1) {
16
+ const config = getBotConfigByKey('discord');
17
+ if ((config ?? '') !== '' && (config.token ?? '') !== '') {
18
+ if (!config.intents) {
19
+ config.intents = [
20
+ GatewayIntentBits.DirectMessageReactions,
21
+ GatewayIntentBits.DirectMessageTyping,
22
+ GatewayIntentBits.DirectMessages,
23
+ GatewayIntentBits.GuildMessageReactions,
24
+ GatewayIntentBits.GuildMessageTyping,
25
+ GatewayIntentBits.GuildMessages,
26
+ GatewayIntentBits.Guilds,
27
+ GatewayIntentBits.MessageContent // 消息内容
28
+ ];
29
+ }
30
+ setBotConfigByKey('discord', config);
31
+ return true;
32
+ }
33
+ }
34
+ console.info('[LOGIN]', '-----------------------');
35
+ const timeoutId = setTimeout(() => {
36
+ throw '超过1分钟未完成登录';
37
+ }, 60000);
38
+ const { token } = await prompts([
39
+ {
40
+ type: 'password',
41
+ name: 'token',
42
+ message: 'BotToken: ',
43
+ validate: value => value !== '' && typeof value === 'string' ? true : 'BotToken: '
44
+ }
45
+ ]);
46
+ if (!token) {
47
+ return false;
48
+ }
49
+ clearTimeout(timeoutId);
50
+ /**
51
+ * 默认公域机器人
52
+ */
53
+ const intents = [
54
+ GatewayIntentBits.DirectMessageReactions,
55
+ GatewayIntentBits.DirectMessageTyping,
56
+ GatewayIntentBits.DirectMessages,
57
+ GatewayIntentBits.GuildMessageReactions,
58
+ GatewayIntentBits.GuildMessageTyping,
59
+ GatewayIntentBits.GuildMessages,
60
+ GatewayIntentBits.Guilds,
61
+ GatewayIntentBits.MessageContent // 消息内容
62
+ ];
63
+ // 得到已变更的配置
64
+ const db = getBotConfigByKey('discord');
65
+ // 得到配置
66
+ const data = getToml();
67
+ data.discord = {
68
+ ...db,
69
+ // 覆盖新配置
70
+ token,
71
+ intents
72
+ };
73
+ // 写入配置
74
+ writeToml(data);
75
+ // 设置配置
76
+ setBotConfigByKey('discord', data.discord);
77
+ return true;
78
+ }
79
+ // GatewayIntentBits.AutoModerationConfiguration, // 自动调节配置
80
+ // GatewayIntentBits.AutoModerationExecution, // 自动调节执行
81
+ // 消息
82
+ // GatewayIntentBits.DirectMessageReactions, // 直接消息反应
83
+ // GatewayIntentBits.DirectMessageTyping, // 直接消息键入
84
+ // GatewayIntentBits.DirectMessages, // 直接消息
85
+ // 公会
86
+ // GatewayIntentBits.GuildEmojisAndStickers, // 帮会表情符号和贴纸,
87
+ // GatewayIntentBits.GuildIntegrations, // 帮会整合,
88
+ // GatewayIntentBits.GuildInvites, // 帮会邀请,
89
+ // GatewayIntentBits.GuildEmojisAndStickers, // 帮会表情符号和贴纸,
90
+ // GatewayIntentBits.GuildMessageReactions, // 帮会消息反应,
91
+ // GatewayIntentBits.GuildMessageTyping, // 帮会消息打字,
92
+ // GatewayIntentBits.GuildMessages, // 帮会消息
93
+ // GatewayIntentBits.GuildModeration, // 帮会审核,
94
+ // GatewayIntentBits.GuildPresences, // 帮会礼物,
95
+ // GatewayIntentBits.GuildScheduledEvents, // 帮会预定活动,
96
+ // GatewayIntentBits.GuildVoiceStates, // 帮会音频,
97
+ // GatewayIntentBits.GuildWebhooks, //帮会网络挂钩,
98
+ // GatewayIntentBits.Guilds, // 帮会,
99
+ // GatewayIntentBits.MessageContent // 消息内容
package/lib/index.js ADDED
@@ -0,0 +1,12 @@
1
+ if (process.argv.slice(2).includes('discord') &&
2
+ !process.argv.slice(2).includes('not')) {
3
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
4
+ }
5
+ // 导出机器人
6
+ export * from './main.js';
7
+ // 导出核心方法
8
+ export * from './alemon/index.js';
9
+ /**
10
+ * 编译
11
+ */
12
+ export { compilationTools } from 'alemon-rollup';
@@ -0,0 +1,11 @@
1
+ let bot = {
2
+ id: '',
3
+ name: '',
4
+ avatar: 'string'
5
+ };
6
+ export function setBotMsgByKOOK(val) {
7
+ bot = val;
8
+ }
9
+ export function getBotMsgByKOOK() {
10
+ return bot;
11
+ }
@@ -0,0 +1,56 @@
1
+ import { PUBLIC_GUILD_MESSAGES_KOOK } from './message/PUBLIC_GUILD_MESSAGES.js';
2
+ /**
3
+ * 事件处理集
4
+ */
5
+ const ConversationMap = {
6
+ /**
7
+ * 文字消息
8
+ */
9
+ [1]: PUBLIC_GUILD_MESSAGES_KOOK,
10
+ /**
11
+ * 图片消息,
12
+ */
13
+ [2]: PUBLIC_GUILD_MESSAGES_KOOK,
14
+ /**
15
+ * 视频消息,
16
+ */
17
+ [3]: PUBLIC_GUILD_MESSAGES_KOOK,
18
+ /**
19
+ * 文件消息,
20
+ */
21
+ [4]: PUBLIC_GUILD_MESSAGES_KOOK,
22
+ /**
23
+ * 音频消息,
24
+ */
25
+ [8]: PUBLIC_GUILD_MESSAGES_KOOK,
26
+ /**
27
+ * mk消息
28
+ */
29
+ [9]: PUBLIC_GUILD_MESSAGES_KOOK,
30
+ /**
31
+ * card消息,
32
+ */
33
+ [10]: PUBLIC_GUILD_MESSAGES_KOOK,
34
+ /**
35
+ * 系统消息
36
+ * @param event
37
+ */
38
+ [255]: (event) => {
39
+ console.info('系统消息', new Date(event.msg_timestamp));
40
+ // console.info('event', event)
41
+ }
42
+ };
43
+ /**
44
+ * 消息接收入口
45
+ * @param req
46
+ * @param res
47
+ */
48
+ export async function callBackByKOOK(event) {
49
+ if (Object.prototype.hasOwnProperty.call(ConversationMap, event.type)) {
50
+ return await ConversationMap[event.type](event);
51
+ }
52
+ else {
53
+ // console.info('[KOOK] 新事件', event.type)
54
+ return false;
55
+ }
56
+ }
@@ -0,0 +1,195 @@
1
+ import { InstructionMatching } from '../../../alemon/index.js';
2
+ import { KOOKApiClient } from 'kook-ws';
3
+ import { segmentKOOK } from '../segment.js';
4
+ import { getBotMsgByKOOK } from '../bot.js';
5
+ import { getBotConfigByKey } from '../../../login.js';
6
+ /**
7
+ *
8
+ * @param event
9
+ * @returns
10
+ */
11
+ export const PUBLIC_GUILD_MESSAGES_KOOK = async (event) => {
12
+ if (event.extra.author.bot) {
13
+ return false;
14
+ }
15
+ let at = false;
16
+ const at_users = [];
17
+ let msg = event.content;
18
+ /**
19
+ * 艾特类型所得到的
20
+ * 包括机器人在内
21
+ */
22
+ const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
23
+ for await (const item of mention_role_part) {
24
+ at = true;
25
+ at_users.push({
26
+ id: item.role_id,
27
+ name: item.name,
28
+ avatar: 'string',
29
+ bot: true
30
+ });
31
+ msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim();
32
+ }
33
+ /**
34
+ * 艾特用户所得到的
35
+ */
36
+ const mention_part = event.extra.kmarkdown?.mention_part ?? [];
37
+ for await (const item of mention_part) {
38
+ at = true;
39
+ at_users.push({
40
+ id: item.id,
41
+ name: item.username,
42
+ avatar: item.avatar,
43
+ bot: false
44
+ });
45
+ msg = msg.replace(`(met)${item.id}(met)`, '').trim();
46
+ }
47
+ let isMaster = false;
48
+ const cfg = getBotConfigByKey('kook');
49
+ const masterID = cfg.masterID;
50
+ if (event.msg_id == masterID) {
51
+ isMaster = true;
52
+ }
53
+ let at_user;
54
+ if (at) {
55
+ if (at_users[0] && at_users[0].bot != true) {
56
+ at_user = at_users[0];
57
+ }
58
+ }
59
+ const avatar = event.extra.author.avatar;
60
+ const e = {
61
+ platform: 'kook',
62
+ bot: getBotMsgByKOOK(),
63
+ event: 'MESSAGES',
64
+ eventType: 'CREATE',
65
+ isPrivate: true,
66
+ isRecall: false,
67
+ isMaster: isMaster,
68
+ isGroup: event.channel_type == 'GROUP' ? true : false,
69
+ at,
70
+ at_users,
71
+ at_user,
72
+ msg,
73
+ msg_id: event.msg_id,
74
+ msg_txt: event.content,
75
+ msg_create_time: event.msg_timestamp,
76
+ guild_id: event.target_id,
77
+ channel_id: event.extra.guild_id,
78
+ user_id: event.extra.author.id,
79
+ user_name: event.extra.author.username,
80
+ user_avatar: avatar.substring(0, avatar.indexOf('?')),
81
+ segment: segmentKOOK,
82
+ /**
83
+ * 消息发送机制
84
+ * @param content 消息内容
85
+ * @param obj 额外消息 可选
86
+ */
87
+ reply: async (msg, img, name) => {
88
+ if (Buffer.isBuffer(msg)) {
89
+ try {
90
+ const url = await KOOKApiClient.postImage(msg, typeof img == 'string' ? img : undefined);
91
+ if (url) {
92
+ if (event.channel_type == 'GROUP') {
93
+ await KOOKApiClient.createMessage({
94
+ type: 2,
95
+ target_id: event.target_id,
96
+ content: url
97
+ });
98
+ return true;
99
+ }
100
+ await KOOKApiClient.createDirectMessage({
101
+ type: 2,
102
+ target_id: event.target_id,
103
+ chat_code: event.extra.code,
104
+ content: url
105
+ });
106
+ return true;
107
+ }
108
+ return false;
109
+ }
110
+ catch (err) {
111
+ console.error(err);
112
+ return false;
113
+ }
114
+ }
115
+ const content = Array.isArray(msg)
116
+ ? msg.join('')
117
+ : typeof msg === 'string'
118
+ ? msg
119
+ : undefined;
120
+ if (Buffer.isBuffer(img)) {
121
+ try {
122
+ const url = await KOOKApiClient.postImage(img, name);
123
+ if (url) {
124
+ if (event.channel_type == 'GROUP') {
125
+ await KOOKApiClient.createMessage({
126
+ type: 9,
127
+ target_id: event.target_id,
128
+ content: content
129
+ });
130
+ await KOOKApiClient.createMessage({
131
+ type: 2,
132
+ target_id: event.target_id,
133
+ content: url
134
+ });
135
+ return true;
136
+ }
137
+ await KOOKApiClient.createDirectMessage({
138
+ type: 9,
139
+ target_id: event.target_id,
140
+ chat_code: event.extra.code,
141
+ content: content
142
+ });
143
+ await KOOKApiClient.createDirectMessage({
144
+ type: 2,
145
+ target_id: event.target_id,
146
+ chat_code: event.extra.code,
147
+ content: url
148
+ });
149
+ return true;
150
+ }
151
+ return false;
152
+ }
153
+ catch (err) {
154
+ console.error(err);
155
+ return false;
156
+ }
157
+ }
158
+ if (event.channel_type == 'GROUP') {
159
+ try {
160
+ await KOOKApiClient.createMessage({
161
+ type: 9,
162
+ target_id: event.target_id,
163
+ content
164
+ });
165
+ return true;
166
+ }
167
+ catch {
168
+ return false;
169
+ }
170
+ }
171
+ try {
172
+ await KOOKApiClient.createDirectMessage({
173
+ type: 9,
174
+ target_id: event.target_id,
175
+ chat_code: event.extra.code,
176
+ content
177
+ });
178
+ return true;
179
+ }
180
+ catch {
181
+ return false;
182
+ }
183
+ }
184
+ };
185
+ await InstructionMatching(e)
186
+ .then(() => {
187
+ console.info(`\n[${e.channel_id}] [${e.user_name}] [${true}] ${e.msg_txt}`);
188
+ return true;
189
+ })
190
+ .catch((err) => {
191
+ console.error(err);
192
+ console.info(`\n[${e.channel_id}] [${e.user_name}] [${false}] ${e.msg_txt}`);
193
+ return false;
194
+ });
195
+ };