discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.23k stars 3.96k forks source link

Discord bot 'interactionCreate' listener responded a few times, then quit responding without changes in the code #7975

Closed POsten040 closed 2 years ago

POsten040 commented 2 years ago

Which package is this bug report for?

discord.js

Issue description

OS Windows 11

Versions:

I was able to register the slash commands and get the success message, then /ping the bot in my server and get a response. I committed and walked away, and now it is no longer responding to interactionCreate at all. I did not change any of the code for registering slash commands or adding commands to the client that I am aware of.

Code sample

//the below code is deploys the commands and appears to run without errors
import dotenv from"dotenv";
import { pathToFileURL, fileURLToPath } from 'url';
import fs from 'node:fs';
import path from 'node:path';
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9';

dotenv.config();
const clientId = process.env.clientId;//bot id
const guildId = process.env.guildId;//server id
const token = process.env.token;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.mjs'));

const register = async (commandsPath, commandFiles) => {
  const commands = [];
  for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = await import(pathToFileURL(filePath).toString());
    commands.push(command.default.data.toJSON());
  }
  return commands;
}
const commands = register(commandsPath, commandFiles);

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

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then((commands) => console.log(`Successfully registered application commands to ${guildId}.`))
//the below code adds commands to client 
client.commands = new Collection();
const setCommandsToClient = async () => {
  const commandsPath = path.join(__dirname, 'commands');
  const commandsFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.mjs'));
  for (const file of commandsFiles) {
    const getPath = path.join(commandsPath, file)
    const command = await import(pathToFileURL(getPath));
    client.commands.set(command.default.data.name, command.default);
  }
}
setCommandsToClient();

//the below code is not getting reached at all
client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;
    const command = client.commands.get(interaction.commandName);
    if (!command) return;
    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

Link to github repo with full code

Package version

^13.7.0

Node.js version

v16.13.0

Operating system

Windows 11

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds, GuildEmojisAndStickers, GuildMessages

I have tested this issue on a development release

No response

S222em commented 2 years ago

Your code does not work at all..... client is not defined and you never call client.login. You should refer to the discord server if you need help.

POsten040 commented 2 years ago

This is the repo, I do have the login code as well as commands and all the related files. Sorry for not posting is initially 👍

link to bot repo @S222em

iCrawl commented 2 years ago

This still doesn't have a reproducible code portion.

Please create a tiny reproducible example for this and re-open a new issue with more information.