@zhin.js/adapter-discord 1.0.87 → 1.1.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/CHANGELOG.md +949 -66
- package/PERMITS.md +25 -0
- package/README.md +60 -67
- package/adapters/discord/index.js +41 -0
- package/adapters/discord/index.ts +57 -0
- package/agents/discord/agent.json +20 -0
- package/agents/discord/boundaries.md +3 -0
- package/agents/discord/conventions.md +3 -0
- package/agents/discord/skills/discord/SKILL.md +21 -0
- package/agents/discord/skills/discord/tools/add_role/index.js +24 -0
- package/agents/discord/skills/discord/tools/add_role/index.ts +26 -0
- package/agents/discord/skills/discord/tools/create_thread/index.js +29 -0
- package/agents/discord/skills/discord/tools/create_thread/index.ts +28 -0
- package/agents/discord/skills/discord/tools/forum_post/index.js +33 -0
- package/agents/discord/skills/discord/tools/forum_post/index.ts +33 -0
- package/agents/discord/skills/discord/tools/list_roles/index.js +28 -0
- package/agents/discord/skills/discord/tools/list_roles/index.ts +34 -0
- package/agents/discord/skills/discord/tools/react/index.js +24 -0
- package/agents/discord/skills/discord/tools/react/index.ts +24 -0
- package/agents/discord/skills/discord/tools/remove_role/index.js +24 -0
- package/agents/discord/skills/discord/tools/remove_role/index.ts +26 -0
- package/agents/discord/skills/discord/tools/send_embed/index.js +43 -0
- package/agents/discord/skills/discord/tools/send_embed/index.ts +38 -0
- package/agents/discord/system.md +3 -0
- package/commands/discord/endpoint/add/[id]/index.js +3 -0
- package/commands/discord/endpoint/add/[id]/index.ts +3 -0
- package/commands/discord/endpoint/definition.js +19 -0
- package/commands/discord/endpoint/definition.ts +19 -0
- package/commands/discord/endpoint/list/index.js +3 -0
- package/commands/discord/endpoint/list/index.ts +3 -0
- package/commands/discord/endpoint/remove/[id]/index.js +3 -0
- package/commands/discord/endpoint/remove/[id]/index.ts +3 -0
- package/lib/client.d.ts +16 -0
- package/lib/client.js +8 -0
- package/lib/discord-runtime-state.d.ts +1 -0
- package/lib/discord-runtime-state.js +6 -0
- package/lib/endpoint.d.ts +72 -0
- package/lib/endpoint.js +458 -0
- package/lib/gateway.d.ts +144 -0
- package/lib/gateway.js +303 -0
- package/lib/index.d.ts +6 -18
- package/lib/index.js +6 -323
- package/lib/platform-permit.d.ts +14 -0
- package/lib/platform-permit.js +42 -0
- package/lib/protocol.d.ts +145 -0
- package/lib/protocol.js +320 -0
- package/lib/side-event-dispatch.d.ts +8 -0
- package/lib/side-event-dispatch.js +24 -0
- package/lib/webhook.d.ts +14 -0
- package/lib/webhook.js +88 -0
- package/package.json +74 -30
- package/plugin.js +19 -0
- package/schema.json +205 -0
- package/src/client.ts +24 -0
- package/src/discord-runtime-state.ts +7 -0
- package/src/endpoint.ts +562 -0
- package/src/gateway.ts +426 -0
- package/src/index.ts +57 -323
- package/src/platform-permit.ts +57 -0
- package/src/protocol.ts +475 -0
- package/src/side-event-dispatch.ts +37 -0
- package/src/webhook.ts +123 -0
- package/client/Dashboard.tsx +0 -195
- package/client/index.tsx +0 -11
- package/client/tsconfig.json +0 -7
- package/client/utils/api.ts +0 -17
- package/dist/index.js +0 -29
- package/lib/adapter.d.ts +0 -19
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -90
- package/lib/adapter.js.map +0 -1
- package/lib/bot-interactions.d.ts +0 -33
- package/lib/bot-interactions.d.ts.map +0 -1
- package/lib/bot-interactions.js +0 -278
- package/lib/bot-interactions.js.map +0 -1
- package/lib/bot.d.ts +0 -117
- package/lib/bot.d.ts.map +0 -1
- package/lib/bot.js +0 -904
- package/lib/bot.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types.d.ts +0 -49
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/plugin.yml +0 -3
- package/skills/discord/SKILL.md +0 -104
- package/src/adapter.ts +0 -107
- package/src/bot-interactions.ts +0 -348
- package/src/bot.ts +0 -1037
- package/src/types.ts +0 -60
package/lib/bot.js
DELETED
|
@@ -1,904 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Discord Bot 实现 (Gateway)
|
|
3
|
-
*/
|
|
4
|
-
import { Client, GatewayIntentBits, EmbedBuilder, AttachmentBuilder, ChannelType, REST, Routes, } from "discord.js";
|
|
5
|
-
import { Message, segment, } from "zhin.js";
|
|
6
|
-
import { createReadStream } from "fs";
|
|
7
|
-
import { promises as fs } from "fs";
|
|
8
|
-
import path from "path";
|
|
9
|
-
export class DiscordBot extends Client {
|
|
10
|
-
adapter;
|
|
11
|
-
$config;
|
|
12
|
-
slashCommandHandlers = new Map();
|
|
13
|
-
$connected = false;
|
|
14
|
-
get pluginLogger() {
|
|
15
|
-
return this.adapter.plugin.logger;
|
|
16
|
-
}
|
|
17
|
-
get $id() {
|
|
18
|
-
return this.$config.name;
|
|
19
|
-
}
|
|
20
|
-
constructor(adapter, $config) {
|
|
21
|
-
const intents = $config.intents || [
|
|
22
|
-
GatewayIntentBits.Guilds,
|
|
23
|
-
GatewayIntentBits.GuildMessages,
|
|
24
|
-
GatewayIntentBits.MessageContent,
|
|
25
|
-
GatewayIntentBits.DirectMessages,
|
|
26
|
-
GatewayIntentBits.GuildMembers,
|
|
27
|
-
GatewayIntentBits.GuildMessageReactions,
|
|
28
|
-
];
|
|
29
|
-
super({ intents });
|
|
30
|
-
this.adapter = adapter;
|
|
31
|
-
this.$config = $config;
|
|
32
|
-
this.$connected = false;
|
|
33
|
-
}
|
|
34
|
-
async handleDiscordMessage(msg) {
|
|
35
|
-
// 忽略机器人消息
|
|
36
|
-
if (msg.author.bot)
|
|
37
|
-
return;
|
|
38
|
-
const message = this.$formatMessage(msg);
|
|
39
|
-
this.adapter.emit("message.receive", message);
|
|
40
|
-
this.pluginLogger.debug(`${this.$config.name} recv ${message.$channel.type}(${message.$channel.id}): ${segment.raw(message.$content)}`);
|
|
41
|
-
}
|
|
42
|
-
async handleSlashCommand(interaction) {
|
|
43
|
-
const commandName = interaction.commandName;
|
|
44
|
-
const handler = this.slashCommandHandlers.get(commandName);
|
|
45
|
-
if (handler) {
|
|
46
|
-
try {
|
|
47
|
-
await handler(interaction);
|
|
48
|
-
this.pluginLogger.info(`Executed slash command: /${commandName} by ${interaction.user.tag}`);
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
this.pluginLogger.error(`Error executing slash command /${commandName}:`, error);
|
|
52
|
-
const errorMessage = "An error occurred while executing this command.";
|
|
53
|
-
if (interaction.replied || interaction.deferred) {
|
|
54
|
-
await interaction.followUp({
|
|
55
|
-
content: errorMessage,
|
|
56
|
-
ephemeral: true,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
await interaction.reply({ content: errorMessage, ephemeral: true });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
this.pluginLogger.warn(`Unknown slash command: /${commandName}`);
|
|
66
|
-
if (!interaction.replied) {
|
|
67
|
-
await interaction.reply({
|
|
68
|
-
content: "Unknown command.",
|
|
69
|
-
ephemeral: true,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async $connect() {
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
// 监听消息事件
|
|
77
|
-
this.on("messageCreate", this.handleDiscordMessage.bind(this));
|
|
78
|
-
// 监听交互事件(Slash Commands)
|
|
79
|
-
if (this.$config.enableSlashCommands) {
|
|
80
|
-
this.on("interactionCreate", async (interaction) => {
|
|
81
|
-
if (interaction.isChatInputCommand()) {
|
|
82
|
-
await this.handleSlashCommand(interaction);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
// 监听就绪事件
|
|
87
|
-
this.once("clientReady", async () => {
|
|
88
|
-
this.$connected = true;
|
|
89
|
-
this.pluginLogger.info(`Discord bot ${this.$config.name} connected successfully as ${this.user?.tag}`);
|
|
90
|
-
// 设置活动状态
|
|
91
|
-
if (this.$config.defaultActivity) {
|
|
92
|
-
this.user?.setActivity(this.$config.defaultActivity.name, {
|
|
93
|
-
type: this.getActivityType(this.$config.defaultActivity.type),
|
|
94
|
-
url: this.$config.defaultActivity.url,
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
// 注册 Slash Commands
|
|
98
|
-
if (this.$config.enableSlashCommands && this.$config.slashCommands) {
|
|
99
|
-
await this.registerSlashCommands();
|
|
100
|
-
}
|
|
101
|
-
resolve();
|
|
102
|
-
});
|
|
103
|
-
// 监听错误事件
|
|
104
|
-
this.on("error", (error) => {
|
|
105
|
-
this.pluginLogger.error("Discord client error:", error);
|
|
106
|
-
this.$connected = false;
|
|
107
|
-
reject(error);
|
|
108
|
-
});
|
|
109
|
-
// 登录
|
|
110
|
-
this.login(this.$config.token).catch((error) => {
|
|
111
|
-
this.pluginLogger.error("Failed to login to Discord:", error);
|
|
112
|
-
this.$connected = false;
|
|
113
|
-
reject(error);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
async $disconnect() {
|
|
118
|
-
try {
|
|
119
|
-
await this.destroy();
|
|
120
|
-
this.$connected = false;
|
|
121
|
-
this.pluginLogger.info(`Discord bot ${this.$config.name} disconnected`);
|
|
122
|
-
}
|
|
123
|
-
catch (error) {
|
|
124
|
-
this.pluginLogger.error("Error disconnecting Discord bot:", error);
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
$formatMessage(msg) {
|
|
129
|
-
// 确定聊天类型和ID
|
|
130
|
-
let channelType;
|
|
131
|
-
let channelId;
|
|
132
|
-
if (msg.channel.type === ChannelType.DM) {
|
|
133
|
-
channelType = "private";
|
|
134
|
-
channelId = msg.channel.id;
|
|
135
|
-
}
|
|
136
|
-
else if (msg.channel.type === ChannelType.GroupDM) {
|
|
137
|
-
channelType = "group";
|
|
138
|
-
channelId = msg.channel.id;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
channelType = "channel";
|
|
142
|
-
channelId = msg.channel.id;
|
|
143
|
-
}
|
|
144
|
-
// 转换消息内容为 segment 格式
|
|
145
|
-
const content = this.parseMessageContent(msg);
|
|
146
|
-
const result = Message.from(msg, {
|
|
147
|
-
$id: msg.id,
|
|
148
|
-
$adapter: "discord",
|
|
149
|
-
$bot: this.$config.name,
|
|
150
|
-
$sender: {
|
|
151
|
-
id: msg.author.id,
|
|
152
|
-
name: msg.member?.displayName || msg.author.displayName,
|
|
153
|
-
},
|
|
154
|
-
$channel: {
|
|
155
|
-
id: channelId,
|
|
156
|
-
type: channelType,
|
|
157
|
-
},
|
|
158
|
-
$content: content,
|
|
159
|
-
$raw: msg.content,
|
|
160
|
-
$timestamp: msg.createdTimestamp,
|
|
161
|
-
$recall: async () => {
|
|
162
|
-
await msg.delete();
|
|
163
|
-
},
|
|
164
|
-
$reply: async (content, quote) => {
|
|
165
|
-
if (!Array.isArray(content))
|
|
166
|
-
content = [content];
|
|
167
|
-
const sendOptions = {};
|
|
168
|
-
// 处理回复消息
|
|
169
|
-
if (quote) {
|
|
170
|
-
const replyId = typeof quote === "boolean" ? result.$id : quote;
|
|
171
|
-
try {
|
|
172
|
-
const replyMessage = await msg.channel.messages.fetch(replyId);
|
|
173
|
-
sendOptions.reply = { messageReference: replyMessage };
|
|
174
|
-
}
|
|
175
|
-
catch (error) {
|
|
176
|
-
this.pluginLogger.warn(`Could not find message to reply to: ${replyId}`);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
const res = await this.adapter.sendMessage({
|
|
180
|
-
context: "discord",
|
|
181
|
-
bot: this.$config.name,
|
|
182
|
-
id: msg.channel.id,
|
|
183
|
-
type: msg.channel.type,
|
|
184
|
-
content: content,
|
|
185
|
-
});
|
|
186
|
-
return res;
|
|
187
|
-
},
|
|
188
|
-
});
|
|
189
|
-
return result;
|
|
190
|
-
}
|
|
191
|
-
// 解析 Discord 消息内容为 segment 格式
|
|
192
|
-
parseMessageContent(msg) {
|
|
193
|
-
const segments = [];
|
|
194
|
-
// 回复消息处理
|
|
195
|
-
if (msg.reference) {
|
|
196
|
-
segments.push({
|
|
197
|
-
type: "reply",
|
|
198
|
-
data: {
|
|
199
|
-
id: msg.reference.messageId,
|
|
200
|
-
channel_id: msg.reference.channelId,
|
|
201
|
-
guild_id: msg.reference.guildId,
|
|
202
|
-
},
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
// 文本消息(包含提及、表情等)
|
|
206
|
-
if (msg.content) {
|
|
207
|
-
segments.push(...this.parseTextContent(msg.content, msg));
|
|
208
|
-
}
|
|
209
|
-
// 附件消息
|
|
210
|
-
for (const attachment of msg.attachments.values()) {
|
|
211
|
-
segments.push(...this.parseAttachment(attachment));
|
|
212
|
-
}
|
|
213
|
-
// Embed 消息
|
|
214
|
-
for (const embed of msg.embeds) {
|
|
215
|
-
segments.push({
|
|
216
|
-
type: "embed",
|
|
217
|
-
data: {
|
|
218
|
-
title: embed.title,
|
|
219
|
-
description: embed.description,
|
|
220
|
-
color: embed.color,
|
|
221
|
-
url: embed.url,
|
|
222
|
-
thumbnail: embed.thumbnail,
|
|
223
|
-
image: embed.image,
|
|
224
|
-
author: embed.author,
|
|
225
|
-
footer: embed.footer,
|
|
226
|
-
fields: embed.fields,
|
|
227
|
-
timestamp: embed.timestamp,
|
|
228
|
-
},
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
// 贴纸消息
|
|
232
|
-
for (const sticker of msg.stickers.values()) {
|
|
233
|
-
segments.push({
|
|
234
|
-
type: "sticker",
|
|
235
|
-
data: {
|
|
236
|
-
id: sticker.id,
|
|
237
|
-
name: sticker.name,
|
|
238
|
-
url: sticker.url,
|
|
239
|
-
format: sticker.format,
|
|
240
|
-
tags: sticker.tags,
|
|
241
|
-
},
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
return segments.length > 0
|
|
245
|
-
? segments
|
|
246
|
-
: [{ type: "text", data: { text: "" } }];
|
|
247
|
-
}
|
|
248
|
-
// 解析文本内容,处理提及、频道引用、角色引用等
|
|
249
|
-
parseTextContent(content, msg) {
|
|
250
|
-
const segments = [];
|
|
251
|
-
let lastIndex = 0;
|
|
252
|
-
// 匹配用户提及 <@!?用户ID>
|
|
253
|
-
const userMentionRegex = /<@!?(\d+)>/g;
|
|
254
|
-
// 匹配频道提及 <#频道ID>
|
|
255
|
-
const channelMentionRegex = /<#(\d+)>/g;
|
|
256
|
-
// 匹配角色提及 <@&角色ID>
|
|
257
|
-
const roleMentionRegex = /<@&(\d+)>/g;
|
|
258
|
-
// 匹配自定义表情 <:名称:ID> 或 <a:名称:ID>
|
|
259
|
-
const emojiRegex = /<a?:(\w+):(\d+)>/g;
|
|
260
|
-
const allMatches = [];
|
|
261
|
-
// 收集所有匹配项
|
|
262
|
-
let match;
|
|
263
|
-
while ((match = userMentionRegex.exec(content)) !== null) {
|
|
264
|
-
allMatches.push({ match, type: "user" });
|
|
265
|
-
}
|
|
266
|
-
while ((match = channelMentionRegex.exec(content)) !== null) {
|
|
267
|
-
allMatches.push({ match, type: "channel" });
|
|
268
|
-
}
|
|
269
|
-
while ((match = roleMentionRegex.exec(content)) !== null) {
|
|
270
|
-
allMatches.push({ match, type: "role" });
|
|
271
|
-
}
|
|
272
|
-
while ((match = emojiRegex.exec(content)) !== null) {
|
|
273
|
-
allMatches.push({ match, type: "emoji" });
|
|
274
|
-
}
|
|
275
|
-
// 按位置排序
|
|
276
|
-
allMatches.sort((a, b) => a.match.index - b.match.index);
|
|
277
|
-
// 处理每个匹配项
|
|
278
|
-
for (const { match, type } of allMatches) {
|
|
279
|
-
const matchStart = match.index;
|
|
280
|
-
const matchEnd = matchStart + match[0].length;
|
|
281
|
-
// 添加匹配项前的文本
|
|
282
|
-
if (matchStart > lastIndex) {
|
|
283
|
-
const beforeText = content.slice(lastIndex, matchStart);
|
|
284
|
-
if (beforeText.trim()) {
|
|
285
|
-
segments.push({ type: "text", data: { text: beforeText } });
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
// 添加特殊内容段
|
|
289
|
-
switch (type) {
|
|
290
|
-
case "user":
|
|
291
|
-
const userId = match[1];
|
|
292
|
-
const user = msg.mentions.users.get(userId);
|
|
293
|
-
segments.push({
|
|
294
|
-
type: "at",
|
|
295
|
-
data: {
|
|
296
|
-
id: userId,
|
|
297
|
-
name: user?.username || "Unknown",
|
|
298
|
-
text: match[0],
|
|
299
|
-
},
|
|
300
|
-
});
|
|
301
|
-
break;
|
|
302
|
-
case "channel":
|
|
303
|
-
const channelId = match[1];
|
|
304
|
-
const channel = msg.mentions.channels.get(channelId);
|
|
305
|
-
segments.push({
|
|
306
|
-
type: "channel_mention",
|
|
307
|
-
data: {
|
|
308
|
-
id: channelId,
|
|
309
|
-
name: channel?.name || "unknown-channel",
|
|
310
|
-
text: match[0],
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
break;
|
|
314
|
-
case "role":
|
|
315
|
-
const roleId = match[1];
|
|
316
|
-
const role = msg.mentions.roles.get(roleId);
|
|
317
|
-
segments.push({
|
|
318
|
-
type: "role_mention",
|
|
319
|
-
data: {
|
|
320
|
-
id: roleId,
|
|
321
|
-
name: role?.name || "unknown-role",
|
|
322
|
-
text: match[0],
|
|
323
|
-
},
|
|
324
|
-
});
|
|
325
|
-
break;
|
|
326
|
-
case "emoji":
|
|
327
|
-
const emojiName = match[1];
|
|
328
|
-
const emojiId = match[2];
|
|
329
|
-
const isAnimated = match[0].startsWith("<a:");
|
|
330
|
-
segments.push({
|
|
331
|
-
type: "emoji",
|
|
332
|
-
data: {
|
|
333
|
-
id: emojiId,
|
|
334
|
-
name: emojiName,
|
|
335
|
-
animated: isAnimated,
|
|
336
|
-
url: `https://cdn.discordapp.com/emojis/${emojiId}.${isAnimated ? "gif" : "png"}`,
|
|
337
|
-
text: match[0],
|
|
338
|
-
},
|
|
339
|
-
});
|
|
340
|
-
break;
|
|
341
|
-
}
|
|
342
|
-
lastIndex = matchEnd;
|
|
343
|
-
}
|
|
344
|
-
// 添加最后剩余的文本
|
|
345
|
-
if (lastIndex < content.length) {
|
|
346
|
-
const remainingText = content.slice(lastIndex);
|
|
347
|
-
if (remainingText.trim()) {
|
|
348
|
-
segments.push({ type: "text", data: { text: remainingText } });
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
return segments.length > 0
|
|
352
|
-
? segments
|
|
353
|
-
: [{ type: "text", data: { text: content } }];
|
|
354
|
-
}
|
|
355
|
-
// 解析附件
|
|
356
|
-
parseAttachment(attachment) {
|
|
357
|
-
const segments = [];
|
|
358
|
-
if (attachment.contentType?.startsWith("image/")) {
|
|
359
|
-
segments.push({
|
|
360
|
-
type: "image",
|
|
361
|
-
data: {
|
|
362
|
-
id: attachment.id,
|
|
363
|
-
name: attachment.name,
|
|
364
|
-
url: attachment.url,
|
|
365
|
-
proxy_url: attachment.proxyURL,
|
|
366
|
-
size: attachment.size,
|
|
367
|
-
width: attachment.width,
|
|
368
|
-
height: attachment.height,
|
|
369
|
-
content_type: attachment.contentType,
|
|
370
|
-
},
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
else if (attachment.contentType?.startsWith("audio/")) {
|
|
374
|
-
segments.push({
|
|
375
|
-
type: "audio",
|
|
376
|
-
data: {
|
|
377
|
-
id: attachment.id,
|
|
378
|
-
name: attachment.name,
|
|
379
|
-
url: attachment.url,
|
|
380
|
-
proxy_url: attachment.proxyURL,
|
|
381
|
-
size: attachment.size,
|
|
382
|
-
content_type: attachment.contentType,
|
|
383
|
-
},
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
else if (attachment.contentType?.startsWith("video/")) {
|
|
387
|
-
segments.push({
|
|
388
|
-
type: "video",
|
|
389
|
-
data: {
|
|
390
|
-
id: attachment.id,
|
|
391
|
-
name: attachment.name,
|
|
392
|
-
url: attachment.url,
|
|
393
|
-
proxy_url: attachment.proxyURL,
|
|
394
|
-
size: attachment.size,
|
|
395
|
-
width: attachment.width,
|
|
396
|
-
height: attachment.height,
|
|
397
|
-
content_type: attachment.contentType,
|
|
398
|
-
},
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
segments.push({
|
|
403
|
-
type: "file",
|
|
404
|
-
data: {
|
|
405
|
-
id: attachment.id,
|
|
406
|
-
name: attachment.name,
|
|
407
|
-
url: attachment.url,
|
|
408
|
-
proxy_url: attachment.proxyURL,
|
|
409
|
-
size: attachment.size,
|
|
410
|
-
content_type: attachment.contentType,
|
|
411
|
-
},
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
return segments;
|
|
415
|
-
}
|
|
416
|
-
async $sendMessage(options) {
|
|
417
|
-
try {
|
|
418
|
-
const channel = await this.channels.fetch(options.id);
|
|
419
|
-
if (!channel || !channel.isTextBased()) {
|
|
420
|
-
throw new Error(`Channel ${options.id} is not a text channel`);
|
|
421
|
-
}
|
|
422
|
-
const result = await this.sendContentToChannel(channel, options.content);
|
|
423
|
-
this.pluginLogger.debug(`${this.$config.name} send ${options.type}(${options.id}): ${segment.raw(options.content)}`);
|
|
424
|
-
return result.id;
|
|
425
|
-
}
|
|
426
|
-
catch (error) {
|
|
427
|
-
this.pluginLogger.error("Failed to send Discord message:", error);
|
|
428
|
-
throw error;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
// 发送内容到频道
|
|
432
|
-
async sendContentToChannel(channel, content, extraOptions = {}) {
|
|
433
|
-
if (!Array.isArray(content))
|
|
434
|
-
content = [content];
|
|
435
|
-
const messageOptions = { ...extraOptions };
|
|
436
|
-
let textContent = "";
|
|
437
|
-
const embeds = [];
|
|
438
|
-
const files = [];
|
|
439
|
-
for (const segment of content) {
|
|
440
|
-
if (typeof segment === "string") {
|
|
441
|
-
textContent += segment;
|
|
442
|
-
continue;
|
|
443
|
-
}
|
|
444
|
-
const { type, data } = segment;
|
|
445
|
-
switch (type) {
|
|
446
|
-
case "text":
|
|
447
|
-
textContent += data.text || "";
|
|
448
|
-
break;
|
|
449
|
-
case "at":
|
|
450
|
-
textContent += `<@${data.id}>`;
|
|
451
|
-
break;
|
|
452
|
-
case "channel_mention":
|
|
453
|
-
textContent += `<#${data.id}>`;
|
|
454
|
-
break;
|
|
455
|
-
case "role_mention":
|
|
456
|
-
textContent += `<@&${data.id}>`;
|
|
457
|
-
break;
|
|
458
|
-
case "emoji":
|
|
459
|
-
textContent += data.animated
|
|
460
|
-
? `<a:${data.name}:${data.id}>`
|
|
461
|
-
: `<:${data.name}:${data.id}>`;
|
|
462
|
-
break;
|
|
463
|
-
case "image":
|
|
464
|
-
case "audio":
|
|
465
|
-
case "video":
|
|
466
|
-
case "file":
|
|
467
|
-
await this.handleFileSegment(data, files, textContent);
|
|
468
|
-
break;
|
|
469
|
-
case "embed":
|
|
470
|
-
embeds.push(this.createEmbedFromData(data));
|
|
471
|
-
break;
|
|
472
|
-
default:
|
|
473
|
-
// 未知类型作为文本处理
|
|
474
|
-
textContent += data.text || `[${type}]`;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
// 设置消息内容
|
|
478
|
-
if (textContent.trim()) {
|
|
479
|
-
messageOptions.content = textContent.trim();
|
|
480
|
-
}
|
|
481
|
-
if (embeds.length > 0) {
|
|
482
|
-
messageOptions.embeds = embeds.slice(0, 10); // Discord 限制最多10个embed
|
|
483
|
-
}
|
|
484
|
-
if (files.length > 0) {
|
|
485
|
-
messageOptions.files = files;
|
|
486
|
-
}
|
|
487
|
-
// 发送消息
|
|
488
|
-
return await channel.send(messageOptions);
|
|
489
|
-
}
|
|
490
|
-
async $recallMessage(id) { }
|
|
491
|
-
// ==================== 服务器管理 API ====================
|
|
492
|
-
/**
|
|
493
|
-
* 踢出成员
|
|
494
|
-
* @param guildId 服务器 ID
|
|
495
|
-
* @param userId 用户 ID
|
|
496
|
-
* @param reason 原因
|
|
497
|
-
*/
|
|
498
|
-
async kickMember(guildId, userId, reason) {
|
|
499
|
-
try {
|
|
500
|
-
const guild = await this.guilds.fetch(guildId);
|
|
501
|
-
const member = await guild.members.fetch(userId);
|
|
502
|
-
await member.kick(reason);
|
|
503
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 踢出成员 ${userId}(服务器 ${guildId})`);
|
|
504
|
-
return true;
|
|
505
|
-
}
|
|
506
|
-
catch (error) {
|
|
507
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 踢出成员失败:`, error);
|
|
508
|
-
throw error;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
/**
|
|
512
|
-
* 封禁成员
|
|
513
|
-
* @param guildId 服务器 ID
|
|
514
|
-
* @param userId 用户 ID
|
|
515
|
-
* @param reason 原因
|
|
516
|
-
* @param deleteMessageDays 删除消息天数
|
|
517
|
-
*/
|
|
518
|
-
async banMember(guildId, userId, reason, deleteMessageDays) {
|
|
519
|
-
try {
|
|
520
|
-
const guild = await this.guilds.fetch(guildId);
|
|
521
|
-
await guild.members.ban(userId, { reason, deleteMessageSeconds: deleteMessageDays ? deleteMessageDays * 86400 : undefined });
|
|
522
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 封禁成员 ${userId}(服务器 ${guildId})`);
|
|
523
|
-
return true;
|
|
524
|
-
}
|
|
525
|
-
catch (error) {
|
|
526
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 封禁成员失败:`, error);
|
|
527
|
-
throw error;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* 解除封禁
|
|
532
|
-
* @param guildId 服务器 ID
|
|
533
|
-
* @param userId 用户 ID
|
|
534
|
-
* @param reason 原因
|
|
535
|
-
*/
|
|
536
|
-
async unbanMember(guildId, userId, reason) {
|
|
537
|
-
try {
|
|
538
|
-
const guild = await this.guilds.fetch(guildId);
|
|
539
|
-
await guild.members.unban(userId, reason);
|
|
540
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 解除封禁 ${userId}(服务器 ${guildId})`);
|
|
541
|
-
return true;
|
|
542
|
-
}
|
|
543
|
-
catch (error) {
|
|
544
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 解除封禁失败:`, error);
|
|
545
|
-
throw error;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* 超时(禁言)成员
|
|
550
|
-
* @param guildId 服务器 ID
|
|
551
|
-
* @param userId 用户 ID
|
|
552
|
-
* @param duration 超时时长(秒),0 表示取消超时
|
|
553
|
-
* @param reason 原因
|
|
554
|
-
*/
|
|
555
|
-
async timeoutMember(guildId, userId, duration = 600, reason) {
|
|
556
|
-
try {
|
|
557
|
-
const guild = await this.guilds.fetch(guildId);
|
|
558
|
-
const member = await guild.members.fetch(userId);
|
|
559
|
-
if (duration === 0) {
|
|
560
|
-
await member.timeout(null, reason);
|
|
561
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 取消成员 ${userId} 超时(服务器 ${guildId})`);
|
|
562
|
-
}
|
|
563
|
-
else {
|
|
564
|
-
await member.timeout(duration * 1000, reason);
|
|
565
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 超时成员 ${userId} ${duration}秒(服务器 ${guildId})`);
|
|
566
|
-
}
|
|
567
|
-
return true;
|
|
568
|
-
}
|
|
569
|
-
catch (error) {
|
|
570
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 超时操作失败:`, error);
|
|
571
|
-
throw error;
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* 修改成员昵称
|
|
576
|
-
* @param guildId 服务器 ID
|
|
577
|
-
* @param userId 用户 ID
|
|
578
|
-
* @param nickname 新昵称
|
|
579
|
-
*/
|
|
580
|
-
async setNickname(guildId, userId, nickname) {
|
|
581
|
-
try {
|
|
582
|
-
const guild = await this.guilds.fetch(guildId);
|
|
583
|
-
const member = await guild.members.fetch(userId);
|
|
584
|
-
await member.setNickname(nickname);
|
|
585
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 设置成员 ${userId} 昵称为 "${nickname}"(服务器 ${guildId})`);
|
|
586
|
-
return true;
|
|
587
|
-
}
|
|
588
|
-
catch (error) {
|
|
589
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 设置昵称失败:`, error);
|
|
590
|
-
throw error;
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* 添加角色
|
|
595
|
-
* @param guildId 服务器 ID
|
|
596
|
-
* @param userId 用户 ID
|
|
597
|
-
* @param roleId 角色 ID
|
|
598
|
-
*/
|
|
599
|
-
async addRole(guildId, userId, roleId) {
|
|
600
|
-
try {
|
|
601
|
-
const guild = await this.guilds.fetch(guildId);
|
|
602
|
-
const member = await guild.members.fetch(userId);
|
|
603
|
-
await member.roles.add(roleId);
|
|
604
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 给成员 ${userId} 添加角色 ${roleId}(服务器 ${guildId})`);
|
|
605
|
-
return true;
|
|
606
|
-
}
|
|
607
|
-
catch (error) {
|
|
608
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 添加角色失败:`, error);
|
|
609
|
-
throw error;
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
/**
|
|
613
|
-
* 移除角色
|
|
614
|
-
* @param guildId 服务器 ID
|
|
615
|
-
* @param userId 用户 ID
|
|
616
|
-
* @param roleId 角色 ID
|
|
617
|
-
*/
|
|
618
|
-
async removeRole(guildId, userId, roleId) {
|
|
619
|
-
try {
|
|
620
|
-
const guild = await this.guilds.fetch(guildId);
|
|
621
|
-
const member = await guild.members.fetch(userId);
|
|
622
|
-
await member.roles.remove(roleId);
|
|
623
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 移除成员 ${userId} 的角色 ${roleId}(服务器 ${guildId})`);
|
|
624
|
-
return true;
|
|
625
|
-
}
|
|
626
|
-
catch (error) {
|
|
627
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 移除角色失败:`, error);
|
|
628
|
-
throw error;
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
/**
|
|
632
|
-
* 获取服务器角色列表
|
|
633
|
-
* @param guildId 服务器 ID
|
|
634
|
-
*/
|
|
635
|
-
async getRoles(guildId) {
|
|
636
|
-
try {
|
|
637
|
-
const guild = await this.guilds.fetch(guildId);
|
|
638
|
-
await guild.roles.fetch();
|
|
639
|
-
return guild.roles.cache.map(role => ({
|
|
640
|
-
id: role.id,
|
|
641
|
-
name: role.name,
|
|
642
|
-
color: role.hexColor,
|
|
643
|
-
position: role.position,
|
|
644
|
-
permissions: role.permissions.bitfield.toString(),
|
|
645
|
-
}));
|
|
646
|
-
}
|
|
647
|
-
catch (error) {
|
|
648
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 获取角色列表失败:`, error);
|
|
649
|
-
throw error;
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
/**
|
|
653
|
-
* 获取成员列表
|
|
654
|
-
* @param guildId 服务器 ID
|
|
655
|
-
* @param limit 数量限制
|
|
656
|
-
*/
|
|
657
|
-
async getMembers(guildId, limit = 100) {
|
|
658
|
-
try {
|
|
659
|
-
const guild = await this.guilds.fetch(guildId);
|
|
660
|
-
const members = await guild.members.fetch({ limit });
|
|
661
|
-
return Array.from(members.values()).map(member => ({
|
|
662
|
-
id: member.id,
|
|
663
|
-
username: member.user.username,
|
|
664
|
-
nickname: member.nickname,
|
|
665
|
-
roles: member.roles.cache.map(r => r.id),
|
|
666
|
-
joined_at: member.joinedAt?.toISOString(),
|
|
667
|
-
}));
|
|
668
|
-
}
|
|
669
|
-
catch (error) {
|
|
670
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 获取成员列表失败:`, error);
|
|
671
|
-
throw error;
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
/**
|
|
675
|
-
* 获取服务器信息
|
|
676
|
-
* @param guildId 服务器 ID
|
|
677
|
-
*/
|
|
678
|
-
async getGuildInfo(guildId) {
|
|
679
|
-
try {
|
|
680
|
-
const guild = await this.guilds.fetch(guildId);
|
|
681
|
-
return {
|
|
682
|
-
id: guild.id,
|
|
683
|
-
name: guild.name,
|
|
684
|
-
icon: guild.iconURL(),
|
|
685
|
-
owner_id: guild.ownerId,
|
|
686
|
-
member_count: guild.memberCount,
|
|
687
|
-
created_at: guild.createdAt?.toISOString(),
|
|
688
|
-
};
|
|
689
|
-
}
|
|
690
|
-
catch (error) {
|
|
691
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 获取服务器信息失败:`, error);
|
|
692
|
-
throw error;
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
async createThread(channelId, name, messageId, autoArchiveDuration) {
|
|
696
|
-
try {
|
|
697
|
-
const channel = await this.channels.fetch(channelId);
|
|
698
|
-
if (!channel || !('threads' in channel))
|
|
699
|
-
throw new Error(`Channel ${channelId} 不支持创建帖子`);
|
|
700
|
-
const options = { name, autoArchiveDuration: autoArchiveDuration || 1440 };
|
|
701
|
-
if (messageId)
|
|
702
|
-
options.startMessage = messageId;
|
|
703
|
-
const thread = await channel.threads.create(options);
|
|
704
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 创建帖子 "${name}" (channel ${channelId})`);
|
|
705
|
-
return thread;
|
|
706
|
-
}
|
|
707
|
-
catch (error) {
|
|
708
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 创建帖子失败:`, error);
|
|
709
|
-
throw error;
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
async addReaction(channelId, messageId, emoji) {
|
|
713
|
-
try {
|
|
714
|
-
const channel = await this.channels.fetch(channelId);
|
|
715
|
-
if (!channel || !channel.isTextBased())
|
|
716
|
-
throw new Error(`Channel ${channelId} 不是文本频道`);
|
|
717
|
-
const message = await channel.messages.fetch(messageId);
|
|
718
|
-
await message.react(emoji);
|
|
719
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 添加反应 ${emoji} (message ${messageId})`);
|
|
720
|
-
}
|
|
721
|
-
catch (error) {
|
|
722
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 添加反应失败:`, error);
|
|
723
|
-
throw error;
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
async sendEmbed(channelId, embedData) {
|
|
727
|
-
try {
|
|
728
|
-
const channel = await this.channels.fetch(channelId);
|
|
729
|
-
if (!channel || !channel.isTextBased())
|
|
730
|
-
throw new Error(`Channel ${channelId} 不是文本频道`);
|
|
731
|
-
const embed = this.createEmbedFromData(embedData);
|
|
732
|
-
const msg = await channel.send({ embeds: [embed] });
|
|
733
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 发送 Embed 到 ${channelId}`);
|
|
734
|
-
return msg;
|
|
735
|
-
}
|
|
736
|
-
catch (error) {
|
|
737
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 发送 Embed 失败:`, error);
|
|
738
|
-
throw error;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
async createForumPost(channelId, name, content, tags) {
|
|
742
|
-
try {
|
|
743
|
-
const channel = await this.channels.fetch(channelId);
|
|
744
|
-
if (!channel || channel.type !== ChannelType.GuildForum)
|
|
745
|
-
throw new Error(`Channel ${channelId} 不是论坛频道`);
|
|
746
|
-
const forumChannel = channel;
|
|
747
|
-
const options = {
|
|
748
|
-
name,
|
|
749
|
-
message: { content },
|
|
750
|
-
};
|
|
751
|
-
if (tags?.length && forumChannel.availableTags?.length) {
|
|
752
|
-
const tagIds = forumChannel.availableTags
|
|
753
|
-
.filter((t) => tags.includes(t.name))
|
|
754
|
-
.map((t) => t.id);
|
|
755
|
-
if (tagIds.length)
|
|
756
|
-
options.appliedTags = tagIds;
|
|
757
|
-
}
|
|
758
|
-
const thread = await forumChannel.threads.create(options);
|
|
759
|
-
this.pluginLogger.info(`Discord Bot ${this.$id} 创建论坛帖 "${name}" (channel ${channelId})`);
|
|
760
|
-
return thread;
|
|
761
|
-
}
|
|
762
|
-
catch (error) {
|
|
763
|
-
this.pluginLogger.error(`Discord Bot ${this.$id} 创建论坛帖失败:`, error);
|
|
764
|
-
throw error;
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
// 处理文件段
|
|
768
|
-
async handleFileSegment(data, files, textContent) {
|
|
769
|
-
if (data.file && (await this.fileExists(data.file))) {
|
|
770
|
-
// 本地文件
|
|
771
|
-
files.push(new AttachmentBuilder(createReadStream(data.file), {
|
|
772
|
-
name: data.name || path.basename(data.file),
|
|
773
|
-
}));
|
|
774
|
-
}
|
|
775
|
-
else if (data.url) {
|
|
776
|
-
// URL 文件
|
|
777
|
-
files.push(new AttachmentBuilder(data.url, {
|
|
778
|
-
name: data.name || "attachment",
|
|
779
|
-
}));
|
|
780
|
-
}
|
|
781
|
-
else if (data.buffer) {
|
|
782
|
-
// Buffer 数据
|
|
783
|
-
files.push(new AttachmentBuilder(data.buffer, {
|
|
784
|
-
name: data.name || "attachment",
|
|
785
|
-
}));
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
// 从数据创建 Embed
|
|
789
|
-
createEmbedFromData(data) {
|
|
790
|
-
const embed = new EmbedBuilder();
|
|
791
|
-
if (data.title)
|
|
792
|
-
embed.setTitle(data.title);
|
|
793
|
-
if (data.description)
|
|
794
|
-
embed.setDescription(data.description);
|
|
795
|
-
if (data.color)
|
|
796
|
-
embed.setColor(data.color);
|
|
797
|
-
if (data.url)
|
|
798
|
-
embed.setURL(data.url);
|
|
799
|
-
if (data.thumbnail?.url)
|
|
800
|
-
embed.setThumbnail(data.thumbnail.url);
|
|
801
|
-
if (data.image?.url)
|
|
802
|
-
embed.setImage(data.image.url);
|
|
803
|
-
if (data.author)
|
|
804
|
-
embed.setAuthor(data.author);
|
|
805
|
-
if (data.footer)
|
|
806
|
-
embed.setFooter(data.footer);
|
|
807
|
-
if (data.timestamp)
|
|
808
|
-
embed.setTimestamp(new Date(data.timestamp));
|
|
809
|
-
if (data.fields && Array.isArray(data.fields)) {
|
|
810
|
-
embed.addFields(data.fields);
|
|
811
|
-
}
|
|
812
|
-
return embed;
|
|
813
|
-
}
|
|
814
|
-
// 工具方法:获取活动类型
|
|
815
|
-
getActivityType(type) {
|
|
816
|
-
const activityTypes = {
|
|
817
|
-
PLAYING: 0,
|
|
818
|
-
STREAMING: 1,
|
|
819
|
-
LISTENING: 2,
|
|
820
|
-
WATCHING: 3,
|
|
821
|
-
COMPETING: 5,
|
|
822
|
-
};
|
|
823
|
-
return activityTypes[type] || 0;
|
|
824
|
-
}
|
|
825
|
-
// 注册 Slash Commands
|
|
826
|
-
async registerSlashCommands() {
|
|
827
|
-
if (!this.$config.slashCommands || !this.user)
|
|
828
|
-
return;
|
|
829
|
-
try {
|
|
830
|
-
const rest = new REST({ version: "10" }).setToken(this.$config.token);
|
|
831
|
-
if (this.$config.globalCommands) {
|
|
832
|
-
// 注册全局命令
|
|
833
|
-
await rest.put(Routes.applicationCommands(this.user.id), {
|
|
834
|
-
body: this.$config.slashCommands,
|
|
835
|
-
});
|
|
836
|
-
this.pluginLogger.info("Successfully registered global slash commands");
|
|
837
|
-
}
|
|
838
|
-
else {
|
|
839
|
-
// 为每个服务器注册命令
|
|
840
|
-
for (const guild of this.guilds.cache.values()) {
|
|
841
|
-
await rest.put(Routes.applicationGuildCommands(this.user.id, guild.id), { body: this.$config.slashCommands });
|
|
842
|
-
}
|
|
843
|
-
this.pluginLogger.info("Successfully registered guild slash commands");
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
catch (error) {
|
|
847
|
-
this.pluginLogger.error("Failed to register slash commands:", error);
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
// 添加 Slash Command 处理器
|
|
851
|
-
addSlashCommandHandler(commandName, handler) {
|
|
852
|
-
this.slashCommandHandlers.set(commandName, handler);
|
|
853
|
-
}
|
|
854
|
-
// 移除 Slash Command 处理器
|
|
855
|
-
removeSlashCommandHandler(commandName) {
|
|
856
|
-
return this.slashCommandHandlers.delete(commandName);
|
|
857
|
-
}
|
|
858
|
-
// 工具方法:检查文件是否存在
|
|
859
|
-
async fileExists(filePath) {
|
|
860
|
-
try {
|
|
861
|
-
await fs.access(filePath);
|
|
862
|
-
return true;
|
|
863
|
-
}
|
|
864
|
-
catch {
|
|
865
|
-
return false;
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
// 静态方法:格式化内容为文本(用于日志显示)
|
|
869
|
-
static formatContentToText(content) {
|
|
870
|
-
if (!Array.isArray(content))
|
|
871
|
-
content = [content];
|
|
872
|
-
return content
|
|
873
|
-
.map((segment) => {
|
|
874
|
-
if (typeof segment === "string")
|
|
875
|
-
return segment;
|
|
876
|
-
switch (segment.type) {
|
|
877
|
-
case "text":
|
|
878
|
-
return segment.data.text || "";
|
|
879
|
-
case "at":
|
|
880
|
-
return `@${segment.data.name || segment.data.id}`;
|
|
881
|
-
case "channel_mention":
|
|
882
|
-
return `#${segment.data.name}`;
|
|
883
|
-
case "role_mention":
|
|
884
|
-
return `@${segment.data.name}`;
|
|
885
|
-
case "image":
|
|
886
|
-
return "[图片]";
|
|
887
|
-
case "audio":
|
|
888
|
-
return "[音频]";
|
|
889
|
-
case "video":
|
|
890
|
-
return "[视频]";
|
|
891
|
-
case "file":
|
|
892
|
-
return "[文件]";
|
|
893
|
-
case "embed":
|
|
894
|
-
return "[嵌入消息]";
|
|
895
|
-
case "emoji":
|
|
896
|
-
return `:${segment.data.name}:`;
|
|
897
|
-
default:
|
|
898
|
-
return `[${segment.type}]`;
|
|
899
|
-
}
|
|
900
|
-
})
|
|
901
|
-
.join("");
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
//# sourceMappingURL=bot.js.map
|