Deferring a command reply

Some commands that are ran could take longer time to respond. However, Discord only allows a maximum of 3 seconds for the bot to respond.

What is deferReply?

deferReply is a code that tells Discord API to allow you to delay responses. This feature allows the bot to have approximately 15 minutes to respond to Slash Commands, while Legacy Commands can respond at any time.

Why?

This helps to keep your command file short and clean without having to run interaction.deferReply() then interaction.followUp() or message.channel.sendTyping() then message.reply() every time.

Let's try adding this feature to our "Ping" command.

ping.js
const { NoCliCommandType } = require("nocli-handler.js");

/**
 * @type {import("nocli-handler.js").ICommand}
 */
const Command = {
    deferReply: true, // <-- Delays the response
    type: NoCliCommandType.Both,
    description: "Checks client latency",
    callback: ({ client, message, args, text }) => {
        return `๐Ÿ“ Pong! \`${client.ws.ping}ms\``;
    }
}

module.exports = Command;

End Result:

Before bot replies:

Slash Command

Legacy Command

For Slash commands, you can defer a command reply and send it as ephemeral. You just need to set deferReply to "ephemeral"

Last updated