Skip to content

Your First Command

Play
  • Directorynode_modules/
  • config.json
  • index.js
  • package-lock.json
  • package.json
index.js
const { token } = require("./config.json");
const { Events, SlashCommandBuilder, InteractionContextType, ApplicationIntegrationType } = require("discord.js");
client.once(Events.ClientReady, async c => {
console.log(`Logged in as ${c.user.username}`);
const ping = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with "Pong!"')
.setContexts(InteractionContextType.Guild)
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall);
await client.application.commands.create(ping, "GUILD_ID");
});
client.on(Events.InteractionCreate, async interaction => {
if(!interaction.isChatInputCommand()) return;
if(interaction.commandName === "ping"){
await interaction.reply("Pong!");
}
});
config.json
{
"token": "YOUR_BOT_TOKEN"
}
Why isn’t npm init -y working?

If when running npm init -y you get an error something along the lines of:

Terminal window
npm : The term 'npm' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
  1. Open Windows PowerShell as Administrator.

    • Open the Windows search bar
    • Search for “Windows PowerShell
    • Right-click it and select Run as administrator

  2. Set the Execution Policy

    In the PowerShell window, run:

    Terminal window
    Set-ExecutionPolicy RemoteSigned -Scope LocalMachine

    PowerShell will ask how you want to apply the change.


  3. Choose [A] Yes to All

    Type A and press Enter.

    This updates a Windows security setting that can otherwise prevent tools like Node.js from running properly.


How do I find IDs? (Enabling Developer Mode)

If you are trying to get the ID of a channel, user, or role but you don’t see the Copy User/Channel ID option when you right-click, it is because you do not have Developer Mode enabled.

  1. Open User Settings

    • Click the Gear Icon ⚙️ next to your username in the bottom left corner of Discord.

  2. Navigate to Advanced Settings

    • On the left sidebar, scroll down to the App Settings category.
    • Click on Advanced.

  3. Enable Developer Mode

    • Find the switch labeled Developer Mode.
    • Toggle it ON (Green).

  4. Retrieve your ID

    • Close the settings and go back to your server.
    • Right-click on any user, channel, role, or server icon.
    • You will now see a new option at the bottom of the menu: Copy ID.

Why are my commands showing up in DMs or uninvited servers OR no guild on the interaction?

If your slash commands are appearing in Direct Messages or in servers where your bot hasn’t actually been added, it means your command configuration is missing specific context and integration limits. You need to explicitly restrict them to Guilds only.

const { SlashCommandBuilder, InteractionContextType, ApplicationIntegrationType } = require("discord.js")
new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!')
.setContexts(InteractionContextType.Guild)
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
  1. Locate your Command Builder

    Open the file where you define your SlashCommandBuilder. You need to chain two specific methods to your command definition.

  2. Set the Contexts

    Add .setContexts(InteractionContextType.Guild). This ensures the command can only be executed inside a server (Guild) and disables it within Direct Messages (DMs).

  3. Set the Integration Types

    Add .setIntegrationTypes(ApplicationIntegrationType.GuildInstall). This ensures the command is associated with the Guild installation of the bot, rather than being installed to the user’s personal profile.

Why does the bot stop running when I close vscode/terminal?

Your Discord bot lives inside of your terminal, hence if you stop running it, close the application running it, or turn off your computer the bot will stop working.

How do I stop the running process (bot) in my terminal?

To stop any running processing inside of your terminal click somewhere inside of the terminal, then press CTRL + C to stop the current process.

Interaction has already been acknowledged

If you encounter the error Interaction has already been acknowledged or InteractionAlreadyReplied, and you are certain your code does not contain duplicate reply() or deferReply() calls, it almost always means your bot is running in two places at once.

Both instances receive the command. The “zombie” instance replies first, so when your local code tries to reply a millisecond later, Discord rejects it because the interaction is already “done.”

  1. Option A: Hunt down the process Check your task manager, other terminal tabs, or your hosting dashboard. If you find the extra running instance, kill the process.

  2. Option B: The “Nuclear” Option (Reset Token) If you cannot find where the other bot is running, you can force it to disconnect by invalidating its credentials.

    • Go to the Discord Developer Portal.
    • Select your application and go to the Bot tab.
    • Click Reset Token.
    • Update your .env or config.json file with the new token.

    This will immediately disconnect any “ghost” processes because their old token is no longer valid.

How can I prevent spelling mistakes?

Spelling mistakes can be hard to catch and really annoying, it might be worth looking into a Code Spell Checker Extension. While I don’t formally recommend any particular extension this is the one I personally use at this time.

Application not responding

The error message “The application did not respond” usually means your bot crashed or failed to acknowledge the interaction within the required 3-second timeout.

QuestionTroubleshootFix
Is the bot online?Check the member list in your Discord server. If the bot appears offline, the host or runtime likely crashed.Restart the bot process and review your logs for errors such as unhandled exceptions or failed imports.
Did your command handler crash?Look at your terminal/hosting logs after running the command. If there is a thrown error, the bot may stop midway and never reply.Wrap code in try/catch, validate all inputs, and avoid assuming properties like interaction.options always exist.
Did you reply within 3 seconds?Discord requires interaction.reply() or interaction.deferReply() within 3 seconds, or the command will fail.Use await interaction.deferReply() at the start of long-running commands.
Are you replying multiple times?Sending more than one initial response (e.g., calling interaction.reply() twice) causes a crash or rejection.Use reply() once, then use editReply() or followUp() for all future messages.
Are environment variables missing?If token, clientId, guildId, or API keys are missing, startup may fail silently.Create a config.json file and confirm variables load correctly. Log them during startup (but never commit them).
Are your commands registered properly?If commands aren’t registered or contain invalid structures, Discord won’t execute them.Run your deploy-commands script and check for validation errors in your console.
Are you using the correct intents and permissions?Missing intents can cause interactionCreate to fail or not fire at all.Add required intents to your Client initialization (e.g., Guilds, GuildMembers, GuildMessages).
Does your bot crash after sending a response?Post-reply errors (e.g., accessing undefined variables) won’t prevent the initial reply but may block later logic.Monitor logs in real time and fix downstream logic or failed fetches/DB operations.
My commands are duplicating

One of the most common causes of “duplicated” slash commands in Discord bots is a mismatch between where your commands were published:
Guild commands vs Global commands.

Discord treats these two types of commands as completely separate lists.
This means:

  • Commands published to a guild stay in that guild until you overwrite or remove them.
  • Commands published globally stay in the global command registry until replaced.

Because the lists are separate, you can accidentally end up with copies of the same command appearing if you switch your bot from guild-specific development to global deployment or vice-versa.

When building your bot for the first time, many developers start by registering their commands as guild commands because they update instantly (within seconds) and are easy to test.

Later on, when the bot is ready for production, they switch their deploy script to use global commands, expecting the guild commands to “automatically go away”.
But they don’t.

Discord keeps whatever you last published so the server ends up with:

  • The original guild versions
    and
  • The new global versions

→ Result: every slash command appears twice.

To fix this, you must “clear out” the registry you no longer want to use by publishing an empty array of commands to that specific endpoint.

If you previously used guild commands and now want to use ONLY global commands:

Section titled “If you previously used guild commands and now want to use ONLY global commands:”

You need to push an empty array to the guild commands endpoint:

const { REST } = require("@discordjs/rest");
const rest = new REST({ version: '10' }).setToken("YOUR_BOT_TOKEN");
rest.put(Routes.applicationCommands(clientId), { body: commands.map(c => c.data) });
Everything Else…

We understand that sometimes, despite all the documentation, tutorials, and examples, you might still run into a tricky issue. Before reaching out for help, please make sure you have exhausted the following self help methods:

  • Review the Basics: Double-check your code against the relevant documentation and tutorials. Did you miss a small step, installing a dependency, a typo, etc.
  • Search the Docs: Use the search bar, many common issues may be fixed just by looking for our examples again.
  • CHECK YOUR ERROR LOGS: If your bot is crashing or behaving unexpectedly, look closely at the error messages in your console. Often, the message will point DIRECTLY to the file and line number causing the problem. Search for that specific error message online!

If you have genuinely tried all of the steps above and are still stuck, we are happy to help!

  • Join our discord server: This is the fastest and most efficient way to get support from our community and the development team. https://discord.goldendev.net
  1. A clear description of the problem. What is the bot supposed to do, and what is it doing instead?
  2. The specific error message (if any). Copy-paste the full traceback from your console.
  3. The relevant snippet of code. Do not paste hundreds of lines; only share the section where you believe the issue lies, formatted using a service like GitHub Gist or Pastebin.