n8n-nodes-discord-dnd 0.1.75 → 0.1.77
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.
@@ -4,6 +4,14 @@ exports.DiscordAction = void 0;
|
|
4
4
|
const discord_js_1 = require("discord.js");
|
5
5
|
const voice_1 = require("@discordjs/voice");
|
6
6
|
const client_1 = require("./client");
|
7
|
+
let discordClient = null; // 🧠 Client được cache
|
8
|
+
async function getOrCreateDiscordClient(token, options) {
|
9
|
+
if (discordClient && discordClient.isReady()) {
|
10
|
+
return discordClient;
|
11
|
+
}
|
12
|
+
discordClient = await (0, client_1.initializeDiscordClient)(token, options);
|
13
|
+
return discordClient;
|
14
|
+
}
|
7
15
|
class DiscordAction {
|
8
16
|
constructor() {
|
9
17
|
this.description = {
|
@@ -33,12 +41,10 @@ class DiscordAction {
|
|
33
41
|
{
|
34
42
|
name: "Join Voice Channel",
|
35
43
|
value: "joinVoiceChannel",
|
36
|
-
description: "Join a voice channel",
|
37
44
|
},
|
38
45
|
{
|
39
46
|
name: "Leave Voice Channel",
|
40
47
|
value: "leaveVoiceChannel",
|
41
|
-
description: "Leave the current voice channel",
|
42
48
|
},
|
43
49
|
],
|
44
50
|
default: "joinVoiceChannel",
|
@@ -50,7 +56,6 @@ class DiscordAction {
|
|
50
56
|
type: "string",
|
51
57
|
default: "",
|
52
58
|
required: true,
|
53
|
-
description: "The ID of the Discord server (guild)",
|
54
59
|
},
|
55
60
|
{
|
56
61
|
displayName: "Channel ID",
|
@@ -63,7 +68,6 @@ class DiscordAction {
|
|
63
68
|
},
|
64
69
|
},
|
65
70
|
required: true,
|
66
|
-
description: "The ID of the voice channel to join",
|
67
71
|
},
|
68
72
|
],
|
69
73
|
};
|
@@ -73,71 +77,43 @@ class DiscordAction {
|
|
73
77
|
const operation = this.getNodeParameter("operation", 0);
|
74
78
|
const guildId = this.getNodeParameter("guildId", 0);
|
75
79
|
const channelId = this.getNodeParameter("channelId", 0);
|
76
|
-
// Initialize Discord client with voice intents
|
77
80
|
const clientOptions = {
|
78
|
-
intents: [
|
79
|
-
discord_js_1.GatewayIntentBits.Guilds,
|
80
|
-
discord_js_1.GatewayIntentBits.GuildMessages,
|
81
|
-
discord_js_1.GatewayIntentBits.MessageContent,
|
82
|
-
discord_js_1.GatewayIntentBits.DirectMessages,
|
83
|
-
discord_js_1.GatewayIntentBits.GuildVoiceStates,
|
84
|
-
],
|
81
|
+
intents: [discord_js_1.GatewayIntentBits.Guilds, discord_js_1.GatewayIntentBits.GuildVoiceStates],
|
85
82
|
partials: [discord_js_1.Partials.Channel],
|
86
83
|
};
|
87
|
-
const client = await (
|
84
|
+
const client = await getOrCreateDiscordClient(credentials.botToken, clientOptions);
|
88
85
|
try {
|
89
86
|
const guild = await client.guilds.fetch(guildId);
|
90
|
-
if (!guild)
|
91
|
-
throw new Error(`Could not find guild
|
92
|
-
}
|
93
|
-
const voiceChannel = (await guild.channels.fetch(channelId));
|
94
|
-
if (!voiceChannel || voiceChannel.type !== 2) {
|
95
|
-
// 2 = GUILD_VOICE
|
96
|
-
throw new Error("Channel is not a voice channel!");
|
97
|
-
}
|
87
|
+
if (!guild)
|
88
|
+
throw new Error(`Could not find guild ${guildId}`);
|
98
89
|
switch (operation) {
|
99
90
|
case "joinVoiceChannel": {
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
// Check bot permissions
|
104
|
-
const permissions = voiceChannel.permissionsFor(client.user);
|
105
|
-
if (!(permissions === null || permissions === void 0 ? void 0 : permissions.has(discord_js_1.PermissionsBitField.Flags.Connect))) {
|
106
|
-
throw new Error("Bot does not have permission to join this voice channel");
|
107
|
-
}
|
108
|
-
if (!(permissions === null || permissions === void 0 ? void 0 : permissions.has(discord_js_1.PermissionsBitField.Flags.Speak))) {
|
109
|
-
throw new Error("Bot does not have permission to speak in this voice channel");
|
91
|
+
const voiceChannel = (await guild.channels.fetch(channelId));
|
92
|
+
if (!voiceChannel || voiceChannel.type !== 2) {
|
93
|
+
throw new Error(`Channel ${channelId} is not a valid voice channel`);
|
110
94
|
}
|
111
|
-
// Create connection
|
112
95
|
const connection = (0, voice_1.joinVoiceChannel)({
|
113
|
-
channelId,
|
114
|
-
guildId,
|
96
|
+
channelId: voiceChannel.id,
|
97
|
+
guildId: voiceChannel.guild.id,
|
115
98
|
adapterCreator: voiceChannel.guild
|
116
99
|
.voiceAdapterCreator,
|
117
100
|
selfDeaf: false,
|
118
101
|
selfMute: false,
|
119
102
|
});
|
120
|
-
// Create an audio player to keep the connection alive
|
121
103
|
const player = (0, voice_1.createAudioPlayer)();
|
122
104
|
connection.subscribe(player);
|
123
105
|
try {
|
124
|
-
// Wait for the connection to be ready with a timeout
|
125
106
|
await Promise.race([
|
126
107
|
(0, voice_1.entersState)(connection, voice_1.VoiceConnectionStatus.Ready, 30000),
|
127
108
|
new Promise((_, reject) => {
|
128
|
-
connection.on(voice_1.VoiceConnectionStatus.Disconnected, () =>
|
129
|
-
|
130
|
-
});
|
131
|
-
connection.on("error", (error) => {
|
132
|
-
reject(error);
|
133
|
-
});
|
109
|
+
connection.on(voice_1.VoiceConnectionStatus.Disconnected, () => reject(new Error("Voice connection disconnected")));
|
110
|
+
connection.on("error", (err) => reject(err));
|
134
111
|
}),
|
135
112
|
]);
|
136
113
|
}
|
137
|
-
catch (
|
138
|
-
// Clean up if connection fails
|
114
|
+
catch (err) {
|
139
115
|
connection.destroy();
|
140
|
-
throw new Error(`Failed to join voice channel: ${(
|
116
|
+
throw new Error(`Failed to join voice channel: ${(err === null || err === void 0 ? void 0 : err.message) || "Unknown error"}`);
|
141
117
|
}
|
142
118
|
break;
|
143
119
|
}
|
@@ -149,25 +125,18 @@ class DiscordAction {
|
|
149
125
|
break;
|
150
126
|
}
|
151
127
|
default:
|
152
|
-
throw new Error(`
|
128
|
+
throw new Error(`Unsupported operation "${operation}"`);
|
153
129
|
}
|
154
|
-
// Clean up: Destroy the client after operation is complete
|
155
|
-
client.destroy();
|
156
|
-
// Return success with additional details
|
157
130
|
return [
|
158
131
|
this.helpers.returnJsonArray({
|
159
132
|
success: true,
|
160
133
|
operation,
|
161
134
|
guildId,
|
162
|
-
channelId: operation === "joinVoiceChannel"
|
163
|
-
? this.getNodeParameter("channelId", 0)
|
164
|
-
: undefined,
|
135
|
+
channelId: operation === "joinVoiceChannel" ? channelId : undefined,
|
165
136
|
}),
|
166
137
|
];
|
167
138
|
}
|
168
139
|
catch (error) {
|
169
|
-
// Clean up on error
|
170
|
-
client.destroy();
|
171
140
|
throw error;
|
172
141
|
}
|
173
142
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DiscordAction.node.js","sourceRoot":"","sources":["../../src/nodes/DiscordAction.node.ts"],"names":[],"mappings":";;;AAMA,
|
1
|
+
{"version":3,"file":"DiscordAction.node.js","sourceRoot":"","sources":["../../src/nodes/DiscordAction.node.ts"],"names":[],"mappings":";;;AAMA,2CAA+E;AAC/E,4CAO0B;AAC1B,qCAAmD;AAEnD,IAAI,aAAa,GAAkB,IAAI,CAAC,CAAC,uBAAuB;AAEhE,KAAK,UAAU,wBAAwB,CACrC,KAAa,EACb,OAAY;IAEZ,IAAI,aAAa,IAAI,aAAa,CAAC,OAAO,EAAE,EAAE;QAC5C,OAAO,aAAa,CAAC;KACtB;IAED,aAAa,GAAG,MAAM,IAAA,gCAAuB,EAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC9D,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,MAAa,aAAa;IAA1B;QACE,gBAAW,GAAyB;YAClC,WAAW,EAAE,gBAAgB;YAC7B,IAAI,EAAE,eAAe;YACrB,IAAI,EAAE,iCAAiC;YACvC,KAAK,EAAE,CAAC,QAAQ,CAAC;YACjB,OAAO,EAAE,CAAC;YACV,WAAW,EAAE,0CAA0C;YACvD,QAAQ,EAAE;gBACR,IAAI,EAAE,gBAAgB;aACvB;YACD,MAAM,EAAE,CAAC,MAAM,CAAC;YAChB,OAAO,EAAE,CAAC,MAAM,CAAC;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