#How to use Global commands correctly?

17 messages · Page 1 of 1 (latest)

modest valley
#

as title
my last discord bot is using v11 or maybe older version discord.js to build on heroku.
but heroku free dyno will stop since 2022/11, so i'm try to make a new version discord bot with v14.3.0 and try some new stuff ( like slash command)

now i'm follow the guide's CREATING YOUR BOT, and i finish that and run successful
but when i try to make those Guild ID version commands to global commands, it doesn't work anymore, i was turn

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(() => console.log('Successfully registered application commands.'))
    .catch(console.error);```
to
```js
rest.put(Routes.applicationCommands(clientId), { body: commands })
    .then(() => console.log('Successfully registered application commands.'))
    .catch(console.error);```
 and all commands fail at all server (what ever they are in or not in guild ID server list)
how to fix it ?

i know this is a very basic question but i stuck on it.

if you need full code they are at github https://github.com/Nebulosa-Cat/Discord-Bot

thank you.
GitHub

新 Discord Bot. Contribute to Nebulosa-Cat/Discord-Bot development by creating an account on GitHub.

simple basaltBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

modest valley
#

and i give my bot admin when invite them(with outh2)

#

and looks like this

cedar bloom
#

Your interactionCreate Event doesn’t dispatch to the command files. It only console.logs nothing more

#

So the reply never gets executed

modest valley
#

so should it looks like

module.exports = {
    name: 'interactionCreate',
    execute(interaction) {
        if (!interaction.isChatInputCommand()) 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 });
        }
        console.log(`${interaction.user.tag} in #${interaction.channel.name} triggered an interaction.`);
    },
};```
?
dim igloo
#

i don’t see client defined anywhere

cedar bloom
#

Use interaction.client

modest valley
#

Sorry, I've been thinking about it for a while and still don't understand
now i'm make it work again but i don't know doesn't this correctly or not

#

now version

#

(i put in test branch

#
//啟動模組
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

//於cmd回傳啟動訊息&設定BOT自訂遊戲狀態

//功能區
client.commands = new Collection();
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);
    // Set a new item in the Collection
    // With the key as the command name and the value as the exported module
    client.commands.set(command.data.name, command);
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const filePath = path.join(eventsPath, file);
    const event = require(filePath);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}

client.on('interactionCreate', async interaction => {
    if (!interaction.isChatInputCommand()) 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 });
    }
});

client.login(token);