I’m working on a Discord bot and I’m having trouble with the typing indicator. The latest discord.js update combined typingStart and typingStop into one event. It also set a 10-second limit for sendTyping().
My problem is that my bot’s API requests often take 10-20 seconds. I want the typing indicator to keep going until the response is ready.
I tried using setInterval to repeat the typing every 5 seconds:
let typingState = await channel.sendTyping();
let typeLoop = setInterval(() => { typingState; }, 5000);
// When response is ready
if (apiAnswer) {
setTimeout(() => { clearInterval(typeLoop); }, 1000);
}
But it stops after the first 10 seconds anyway. Am I missing something about how setInterval works with the typing indicator? Any ideas on how to keep it going longer?
Having dealt with similar issues, I can offer some insights. The crux of your problem lies in how you’re using setInterval. You’re not actually calling sendTyping() in your interval function, which is why it stops after 10 seconds. Here’s a more effective approach:
let typeLoop = setInterval(() => {
channel.sendTyping();
}, 8000);
// When response is ready
if (apiAnswer) {
clearInterval(typeLoop);
channel.send(apiAnswer);
}
This will keep the typing indicator active by sending it every 8 seconds until your API response is ready. I’ve found 8 seconds to be a sweet spot, giving a buffer before Discord’s 10-second cutoff.
However, for API calls consistently taking 20+ seconds, consider implementing a progress update message. It helps manage user expectations during longer waits.
As someone who’s worked with Discord bots quite a bit, I can tell you that the typing indicator can be tricky. Your approach with setInterval is on the right track, but you’re not actually calling sendTyping() in your interval function. Here’s what I’ve found works:
let typeLoop = setInterval(() => {
channel.sendTyping();
}, 7000);
// When response is ready
if (apiAnswer) {
clearInterval(typeLoop);
channel.send(apiAnswer);
}
This keeps the typing indicator going every 7 seconds until your API response is ready. I use 7 seconds to avoid any potential race conditions with Discord’s 10-second limit. One word of caution though - if your API calls consistently take 20+ seconds, you might want to consider optimizing them or implementing a fallback message to let users know the bot is still working on their request. Long waits can be frustrating for users, even with the typing indicator.