Search code examples
javascriptdiscorddiscord.js

Discord bot returning error 20012 on slashing commands registeration - JS SDK 14.7.1


Dear all hope you are well,

I have created a discord bot with which I register some commands as well as interact with the user's messages, The first bot is working fine but when I tried to make a second one (with the same configurations as the first one) it is returning the error below:

{
  rawError: {
    message: 'You are not authorized to perform this action on this application',
    code: 20012
  }
}

I made a link with application.commands permission as well as even administrative privileges but it is still not working.

Below you can find my code:

const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, ".env") });

// Discord Bot Configuration/Setup
const Discord, { ButtonStyle } = require("discord.js");

const commands = [];
const command = require("./commands/ping");
commands.push(command.data.toJSON());


const rest = new Discord.REST({ version: "10" }).setToken(
  process.env.DISCORD_BOT_TOKEN
);

(async () => {
  try {
    const data = await rest.put(
      Discord.Routes.applicationCommands("{APPLICATION_ID}"),
      { body: commands }
    );
  } catch (error) {
    // And of course, make sure you catch and log any errors!
    console.error(error);
  }
})();

const client = new Discord.Client({
  partials: [Discord.Partials.Channel, Discord.Partials.Message],
  intents: [
    Discord.GatewayIntentBits.DirectMessages,
    Discord.GatewayIntentBits.DirectMessageTyping,
    Discord.GatewayIntentBits.Guilds,
    Discord.GatewayIntentBits.GuildMessages,
    Discord.GatewayIntentBits.MessageContent,
    Discord.GatewayIntentBits.GuildMembers,
  ],
});
client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on("interactionCreate", async (interaction) => {
// my interaction logics. This part is not relevant to the issue
});

client.on("messageCreate", async (message) => {
// my interaction logics. This part is not relevant to the issue
});

try {
  client.login(process.env.DISCORD_BOT_TOKEN);
} catch (err) {
  console.log(err);
}

The same code is working with my former bot which makes me more puzzled

Update Notes

The code has been updated and I tried to clarify the problem a bit better, if it is still unclear, please comment on it.


Solution

  • While I was writing this bug I figured the problem out, the primary issue was that I used my old application id instead of the new one:

    
        const data = await rest.put(
          Discord.Routes.applicationCommands("{APPLICATION_ID}"), // <==== Causing issue
          { body: commands }
        );
    

    So I extracted it to an environment variable and then used the the right value like below:

    
        const data = await rest.put(
          Discord.Routes.applicationCommands(process.env.APPLICATION_ID),
          { body: commands }
        );
    

    while I was using the token of the new one therefore it was giving unauthorized as it was trying to add slashing commands to the already active bot. the interactions were also returning undefined as there were no interactions existing due to the same issue. You can find the correct application id on the General Information tab of your application on discord developer portal

    enter image description here

    I hope these insights become useful to another person who is also struggling with the new updates of discord.js and that they can save some time :)

    Update Notes

    I have updated the answer and added some codes to it. If the answer for any reason is still unclear, please let me know in the comments