@tinyclaw/plugin-channel-discord 1.1.0-staging.a410a62 → 2.0.0-dev.31dac1b
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 +33 -8
- package/dist/pairing.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
* userId format: "discord:<discord-user-id>"
|
|
15
15
|
* Prefixed to prevent collisions with web UI user IDs.
|
|
16
16
|
*/
|
|
17
|
-
import { Client, GatewayIntentBits, Partials, Events, } from 'discord.js';
|
|
18
17
|
import { logger } from '@tinyclaw/logger';
|
|
19
|
-
import {
|
|
18
|
+
import { Client, Events, GatewayIntentBits, Partials, } from 'discord.js';
|
|
19
|
+
import { createDiscordPairingTools, DISCORD_ENABLED_CONFIG_KEY, DISCORD_TOKEN_SECRET_KEY, } from './pairing.js';
|
|
20
20
|
let client = null;
|
|
21
21
|
const discordPlugin = {
|
|
22
22
|
id: '@tinyclaw/plugin-channel-discord',
|
|
@@ -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
|
},
|
|
@@ -55,16 +56,12 @@ const discordPlugin = {
|
|
|
55
56
|
if (msg.author.bot)
|
|
56
57
|
return;
|
|
57
58
|
const isDM = msg.channel.isDMBased();
|
|
58
|
-
const isMention = client?.user
|
|
59
|
-
? msg.mentions.users.has(client.user.id)
|
|
60
|
-
: false;
|
|
59
|
+
const isMention = client?.user ? msg.mentions.users.has(client.user.id) : false;
|
|
61
60
|
// Only respond to DMs or @mentions
|
|
62
61
|
if (!isDM && !isMention)
|
|
63
62
|
return;
|
|
64
63
|
// Strip @mention tokens from guild messages
|
|
65
|
-
const rawContent = msg.content
|
|
66
|
-
.replace(/<@!?[\d]+>/g, '')
|
|
67
|
-
.trim();
|
|
64
|
+
const rawContent = msg.content.replace(/<@!?[\d]+>/g, '').trim();
|
|
68
65
|
if (!rawContent)
|
|
69
66
|
return;
|
|
70
67
|
// Prefix userId to isolate Discord sessions from web UI sessions
|
|
@@ -100,6 +97,34 @@ const discordPlugin = {
|
|
|
100
97
|
await client.login(token);
|
|
101
98
|
logger.info('Discord bot connected');
|
|
102
99
|
},
|
|
100
|
+
async sendToUser(userId, message) {
|
|
101
|
+
if (!client) {
|
|
102
|
+
throw new Error('Discord client is not connected');
|
|
103
|
+
}
|
|
104
|
+
// Parse the Discord user ID from the prefixed format "discord:<id>"
|
|
105
|
+
const discordId = userId.replace(/^discord:/, '');
|
|
106
|
+
if (!discordId) {
|
|
107
|
+
throw new Error(`Invalid Discord userId: ${userId}`);
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
const user = await client.users.fetch(discordId);
|
|
111
|
+
const text = message.content;
|
|
112
|
+
if (text.length <= 2000) {
|
|
113
|
+
await user.send(text);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const chunks = splitIntoChunks(text, 1900);
|
|
117
|
+
for (const chunk of chunks) {
|
|
118
|
+
await user.send(chunk);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
logger.info(`Discord: sent outbound message to ${userId}`);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
logger.error(`Discord: failed to send to ${userId}`, err);
|
|
125
|
+
throw err;
|
|
126
|
+
}
|
|
127
|
+
},
|
|
103
128
|
async stop() {
|
|
104
129
|
if (client) {
|
|
105
130
|
client.destroy();
|
package/dist/pairing.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* These tools are injected into the agent's tool list at boot so the agent
|
|
10
10
|
* can invoke them conversationally when a user asks to connect Discord.
|
|
11
11
|
*/
|
|
12
|
-
import type {
|
|
12
|
+
import type { ConfigManagerInterface, SecretsManagerInterface, Tool } from '@tinyclaw/types';
|
|
13
13
|
/** Secret key for the Discord bot token. */
|
|
14
14
|
export declare const DISCORD_TOKEN_SECRET_KEY: string;
|
|
15
15
|
/** Config key for the enabled flag. */
|