Skip to content

Commit 60db2b7

Browse files
authored
Merge pull request #1712 from dxlliv/main
feat: Simulate discord typing while generating a response
2 parents 395c2d6 + cb59a09 commit 60db2b7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

packages/client-discord/src/messages.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,16 @@ export class MessageManager {
389389
discordMessageHandlerTemplate,
390390
});
391391

392+
// simulate discord typing while generating a response
393+
const stopTyping = this.simulateTyping(message)
394+
392395
const responseContent = await this._generateResponse(
393396
memory,
394397
state,
395398
context
396-
);
399+
).finally(() => {
400+
stopTyping()
401+
});
397402

398403
responseContent.text = responseContent.text?.trim();
399404
responseContent.inReplyTo = stringToUuid(
@@ -1307,4 +1312,27 @@ export class MessageManager {
13071312
const data = await response.json();
13081313
return data.username;
13091314
}
1315+
1316+
/**
1317+
* Simulate discord typing while generating a response;
1318+
* returns a function to interrupt the typing loop
1319+
*
1320+
* @param message
1321+
*/
1322+
private simulateTyping(message: DiscordMessage) {
1323+
let typing = true;
1324+
1325+
const typingLoop = async () => {
1326+
while (typing) {
1327+
await message.channel.sendTyping();
1328+
await new Promise((resolve) => setTimeout(resolve, 3000));
1329+
}
1330+
};
1331+
1332+
typingLoop();
1333+
1334+
return function stopTyping() {
1335+
typing = false
1336+
}
1337+
}
13101338
}

0 commit comments

Comments
 (0)