DiscordJS "interactionCreate" listener not working

574 Views Asked by At

having a tremendously difficult time getting the client.on("interactionCreate") listener to work. It seems as though when I use slash commands in the client, there is a brief delay and then I get the class "the interaction failed" error state below the button that is triggering the command. For context here's my server code.

const client: Client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildEmojisAndStickers,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessages,
  ],
})

client.once('ready', async () => {
  try {
    //... server logic
  } catch (error) {
    console.log('CLIENT ERROR', error)
  }
})

client.on('interactionCreate', async (interaction: Interaction) => {
  console.log('nothing is ever logged here')
  try {
    let command
    if (interaction.type === InteractionType.ApplicationCommand) {
      command = client.commands.get(interaction.commandName)
    }
    if (interaction.isSelectMenu() || interaction.isButton()) {
      command = client.commands.get(interaction.customId)
    }
    if (!command) return
    await command.execute(interaction)
  } catch (error) {
    console.log('****** INTERACTION ERROR ******', error)
  }
})

Here's the button builder:

new ActionRowBuilder().addComponents(
        new ButtonBuilder()
          .setCustomId('select-player')
          .setLabel('Choose your player')
          .setStyle(ButtonStyle.Primary),

And the deployCommands.ts file

// Discord
import { REST } from '@discordjs/rest'
import { Routes } from 'discord-api-types/v9'
// Node
import fs from 'node:fs'
import path from 'node:path'

const clientId = process.env.DISCORD_CLIENT_ID as string
const guildId = process.env.DISCORD_GUILD_ID as string
const token = process.env.DISCORD_TOKEN as string

const commands = []
const commandsPath = path.join(__dirname, 'commands')
const commandFiles = fs
  .readdirSync(commandsPath)
  .filter((file) => file.endsWith('.js'))

for (const file of commandFiles) {
  const filePath = path.join(commandsPath, file)
  const command = require(filePath)
  if (command.enabled) {
    commands.push(command.data.toJSON())
  }
}

const rest = new REST({ version: '9' }).setToken(token)

rest
  .put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
  .then(() => console.log('Successfully registered application commands.'))
  .catch(console.error)

My commands are structured as follows but we aren't even getting here as the console.log in the listener isn't even firing

module.exports = {
  data: new SlashCommandBuilder()
    .setName('select-player')
    .setDescription(`Pick which Daruma you'd like to compete`),
  enabled: true,
  async execute(interaction: ButtonInteraction) {
  // ... command logic
   }
}

Here's my package.json. Most of my packages are @latest or close

{
  "name": "project name",
  "version": "2.3.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node --no-warnings -r dotenv/config dist/index.js",
    "dev": "nodemon --no-warnings -r dotenv/config dist/index.js",
    "commands": "node -r dotenv/config dist/deployCommands.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@discordjs/rest": "1.0.1",
    "algosdk": "^1.16.0",
    "axios": "^0.27.2",
    "canvas": "^2.9.1",
    "discord.js": "^14.1.2",
    "dotenv": "^16.0.1",
    "mongodb": "^4.6.0",
    "nodemon": "^2.0.16",
    "ts-node": "^10.8.0"
  },
  "devDependencies": {
    "typescript": "^4.6.4"
  },
  "nodemonConfig": {
    "ignore": [
      "dist/txnData/*"
    ]
  }
}

MY permissions, including intents enabled in the Discord Developer portal, roles on the server are all enabled. This includes the applications.commands and bot scopes checked during the URL generator phase when setting up the bot. I have several other bots that work with the same exact code and this is driving me insane. Thanks in advance for your help :)

0

There are 0 best solutions below