@tinyclaw/plugin-channel-discord 1.1.0-dev.96838af → 1.1.0-dev.9c9ed78
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/dist/index.js +29 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -24,6 +24,7 @@ const discordPlugin = {
|
|
|
24
24
|
description: 'Connect Tiny Claw to a Discord bot',
|
|
25
25
|
type: 'channel',
|
|
26
26
|
version: '0.1.0',
|
|
27
|
+
channelPrefix: 'discord',
|
|
27
28
|
getPairingTools(secrets, configManager) {
|
|
28
29
|
return createDiscordPairingTools(secrets, configManager);
|
|
29
30
|
},
|
|
@@ -100,6 +101,34 @@ const discordPlugin = {
|
|
|
100
101
|
await client.login(token);
|
|
101
102
|
logger.info('Discord bot connected');
|
|
102
103
|
},
|
|
104
|
+
async sendToUser(userId, message) {
|
|
105
|
+
if (!client) {
|
|
106
|
+
throw new Error('Discord client is not connected');
|
|
107
|
+
}
|
|
108
|
+
// Parse the Discord user ID from the prefixed format "discord:<id>"
|
|
109
|
+
const discordId = userId.replace(/^discord:/, '');
|
|
110
|
+
if (!discordId) {
|
|
111
|
+
throw new Error(`Invalid Discord userId: ${userId}`);
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const user = await client.users.fetch(discordId);
|
|
115
|
+
const text = message.content;
|
|
116
|
+
if (text.length <= 2000) {
|
|
117
|
+
await user.send(text);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
const chunks = splitIntoChunks(text, 1900);
|
|
121
|
+
for (const chunk of chunks) {
|
|
122
|
+
await user.send(chunk);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
logger.info(`Discord: sent outbound message to ${userId}`);
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
logger.error(`Discord: failed to send to ${userId}`, err);
|
|
129
|
+
throw err;
|
|
130
|
+
}
|
|
131
|
+
},
|
|
103
132
|
async stop() {
|
|
104
133
|
if (client) {
|
|
105
134
|
client.destroy();
|