#deferReply errors with basic discord.js bot

4 messages · Page 1 of 1 (latest)

rain shadowBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
drowsy heath
#

This is the command I use to test the app:

`// test.js
const { SlashCommandBuilder } = require('@discordjs/builders');
const { EmbedBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('test')
.setDescription('Test Command'),

async execute(interaction) {
try {
// Check if the interaction has already been replied to or deferred
if (interaction.deferred || interaction.replied) {
console.log('Interaction already replied or deferred.');
return;
}

  // Defer the initial reply
  const deferredReply = await interaction.deferReply();

  // Edit the deferred reply with the actual content
  const embed = new EmbedBuilder()
    .setTitle('Coders Companion v.beta')
    .setDescription('Running in tip-top shape!')
    .setColor('#00FF00'); // Green color

  await deferredReply.editReply({ embeds: [embed] });
} catch (error) {
  console.error(error);
}
console.log('Executing command:', interaction.commandName);

},
};`

#

This is the interactionCreate.js event file:

`// interactionCreate.js
const { CommandInteraction } = require('discord.js');

module.exports = async function(client, interaction) {
if (!interaction.isCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) {
console.error(No command matching ${interaction.commandName} was found.);
return;
}

try {
await execute(client, interaction, command);
} catch (error) {
console.error(error);
}
}

async function execute(client, interaction, command) {
if (!(interaction instanceof CommandInteraction)) {
console.log('Not a command interaction');
return;
}

console.log('Interaction deferred:', interaction.deferred);

if (command.data.name === 'setrank') {
const staffRoleId = '616160362663903252';
if (
!interaction.member.roles.cache.has(staffRoleId) &&
!interaction.member.permissions.has('MANAGE_ROLES')
) {
return interaction.followUp({
content: 'You do not have permission to use this command.',
ephemeral: true,
});
}
}

console.log('Before deferring reply');
try {
await interaction.deferReply();
console.log('After deferring reply');

console.log('Executing command:', command.data.name);
await command.execute(client, interaction);

} catch (error) {
console.error(Error executing ${command.data.name}, error);

if (!interaction.replied && !interaction.deferred) {
  await interaction.followUp({
    content: 'There was an error executing this command.',
    ephemeral: true,
  });
}

}
}
`

#

And this is the handleCommands.js function file:

`// handleCommands.js
const { Collection } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const dotenv = require('dotenv');
const config = require('../config');

dotenv.config({ path: __dirname + '/../.env' });

async function handleCommands(client) {
client.commands = new Collection();

const commandFiles = fs.readdirSync('./src/commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
const command = require(../commands/${file});
client.commands.set(command.data.name, command);
}

const rest = new REST({ version: '10' }).setToken(process.env.token);

try {
console.log('Started refreshing application (/) commands.');

await rest.put(Routes.applicationGuildCommands(config.clientId, config.guildId), {
  body: Array.from(client.commands.values()).map(command => command.data.toJSON()),
});

console.log('Successfully reloaded application (/) commands.');

} catch (error) {
console.error(error);
}

client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;

try {
  // Check if interaction is already replied or deferred
  if (interaction.deferred || interaction.replied) return;

  await interaction.deferReply({ ephemeral: false });
  await require('../events/interactionCreate')(client, interaction);
} catch (error) {
  // If the interaction has already been replied or deferred, it might not be an error
  if (error.code !== 'InteractionAlreadyReplied') {
    console.error(`Error handling interaction: ${error}`);
  }
}

});
}

module.exports = { handleCommands };`