Discord JS - DiscordAPIError: Missing Access

Viewed 16022

So i follow worn off keys tutorial to discord bot and i don't know what the problem is, here is the error

/home/container/node_modules/discord.js/src/rest/RequestHandler.js:349
throw new DiscordAPIError(data, res.status, request);
      ^
    
DiscordAPIError: Missing Access
    at RequestHandler.execute (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:349:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
    at async GuildApplicationCommandManager.create (/home/container/node_modules/discord.js/src/managers/ApplicationCommandManager.js:117:18) {
  method: 'post',
  path: '/applications/901999677011005472/guilds/905266476573950023/commands',
  code: 50001,
  httpStatus: 403,
  requestData: {
    json: {
      name: 'ping',
      description: 'Bot uptime/latency checker.',
      type: undefined,
      options: undefined,
      default_permission: undefined
    },
    files: []
  }
}

I also try looking at my code but I didn't see something wrong.

This is my code, I really think something is wrong in the code.

const DiscordJS = require('discord.js')
const { Intents } = require('discord.js')
const dotenv = require('dotenv')
dotenv.config()
    
const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES
    ]
})
    
client.on('ready', () => {
    console.log("The bot is online")
    // Carlos: 883425101389914152
    const guildId = '905266476573950023'
    const guild = client.guilds.cache.get(guildId)
    let commands
        
    if (guild) {
        commands = guild.commands
    } else {
        commands = client.application.commands
    }
        
    commands.create({
        name: 'ping',
        description: 'Bot uptime/latency checker.',
    })
        
    commands.create({
        name: 'add',
        description: 'Adds two numbers given by user.',
        options: [
            {
                name: 'number1',
                description: 'The first number',
                required: true,
                type: DiscordJS.Constants.ApplicationCommandOptionTypes.NUMBER,
            },
            {
                name: 'number2',
                description: 'The second number',
                required: true,
                type: DiscordJS.Constants.ApplicationCommandOptionTypes.NUMBER,
            },
        ]
    })
})
    
client.on('interactionCreate', async (interaction) => {
    if (!interaction.isCommand()) {
        return
    }
        
    const { commandName, Options } = interaction
        
    if (commandName === 'ping') {
        interaction.reply({
            content: 'Pong! **60ms**',
            // If anyone can see = True, Only command user can see = False
            ephemeral: true,
        })
    } else if (commandName === 'add') {
        interaction.reply({
            content: 'The sum is ${number1 + number2}'
        })
    }
})
  
client.login(process.env.KEY)
3 Answers

For anyone that having the same problem. I fix this by simply going to bot developer portal then go to OAuth2 > URL generator. And for the scope select both "bot" and "applications.commands". Then scroll down choose whatever permission your bot need copy the URL.

You did not select the right permissions when creating bot login link for your discord server.

Goto developers / oauth again, click bot and select all the required permissions that you're using in this bot.

Then copy the link generated and use it to login with your bot into your server.

don't forget to change this:

content: 'The sum is ${number1 + number2}'

to this:

content: `The sum is ${number1 + number2}`
Related