Skip to content

Commit

Permalink
Merge pull request #1712 from dxlliv/main
Browse files Browse the repository at this point in the history
feat: Simulate discord typing while generating a response
  • Loading branch information
shakkernerd authored Jan 4, 2025
2 parents 395c2d6 + cb59a09 commit 60db2b7
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/client-discord/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,16 @@ export class MessageManager {
discordMessageHandlerTemplate,
});

// simulate discord typing while generating a response
const stopTyping = this.simulateTyping(message)

const responseContent = await this._generateResponse(
memory,
state,
context
);
).finally(() => {
stopTyping()
});

responseContent.text = responseContent.text?.trim();
responseContent.inReplyTo = stringToUuid(
Expand Down Expand Up @@ -1307,4 +1312,27 @@ export class MessageManager {
const data = await response.json();
return data.username;
}

/**
* Simulate discord typing while generating a response;
* returns a function to interrupt the typing loop
*
* @param message
*/
private simulateTyping(message: DiscordMessage) {
let typing = true;

const typingLoop = async () => {
while (typing) {
await message.channel.sendTyping();
await new Promise((resolve) => setTimeout(resolve, 3000));
}
};

typingLoop();

return function stopTyping() {
typing = false
}
}
}

0 comments on commit 60db2b7

Please sign in to comment.