I'm making a discord bot command which returns members of a role. The interaction.deferReply()
works fine but the interaction.followUp(...)
doesn't work.
The interaction is stuck on '--- Bot is thinking...', and no errors messages are thrown.
I tried debugging my code using console.log
to give me an idea of where my code is crashing, but it tells me everything's fine.
So I have no idea what's wrong
Heres the code,
const { SlashCommandBuilder, EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, MessageButton } = require('@discordjs/builders')
const Data = require('../../data/data.json') // Data storage (Not used in this command)
Command Data
module.exports = {
data: new SlashCommandBuilder()
.setName('inrole')
.setDescription('Retrieve members of a role.')
.addRoleOption(option =>
option.setName("role")
.setDescription("Which role members to retrieve.")
.setRequired(true)
)
.addStringOption(option =>
option.setName("private")
.setDescription("Send a private or public message?")
.addChoices(
{ name: 'Public', value: '1' },
{ name: 'Private', value: '2' },)
)
.addIntegerOption(option =>
option.setName("offset")
.setDescription("Page offset, default is 0. Each page contains 10 users max.")
.setMinValue(0)
),
Command Definition
async execute(interaction, client) { // Function that defines the command
await interaction.deferReply()
let guild = interaction.guild;
await guild.members.fetch()
var role = guild.roles.cache.get(interaction.options.getRole('role').id)
var p = interaction.options.getString("private")
if (p == '2') p = true; else p = false
var n = interaction.options.getInteger("offset") ?? 0
var mList = role.members.map(x => '<@' + x.id + '> ||' + x.username + '||').slice(n*10,n*10+10).join('\n')
if ((mList === "") && n > 0) {
mList = "Page is empty."
} else if (mList === "") {
mList = "No one has this role."
}
let len = role.members.map(x => '<@' + x.id + '> ||' + x.username + '||').slice(n*10,n*10+10).length
mList = '-# Page: ' + (Number(n) + 1) + ' | Showing ' + len + ' member' + (len != 1 ? 's' : '') + '\n** **\n' + mList + "\n** **"
var embed = new EmbedBuilder()
.setTitle('Users in the `' + role.name + '` role')
.setDescription(mList)
.setFooter({text: 'Total Members: ' + role.members.size})
await interaction.followUp({embeds: [embed],ephemeral: p})
}
}
Any ideas? Thanks.
Update: This also happens to every other command my bot has, including simple ones like /ping
or /coinflip
, but chat commands using prefixes work.
2nd Update: It works sometimes, and doesn't sometimes.
interaction.deferReply() allows you to postpone the response for your actions. It does not return a message. To submit your response, try editReply instead of followUp.
For more information, please visit:
https://discordjs.guide/slash-commands/response-methods.html#deferred-responses