DiscordJS - Error: Access Denied

After following the worn off keys tutorial for a Discord bot, I encountered an error that says:

/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: []
  }
}

Despite checking my code thoroughly, I couldn't identify any errors.

I have attached my code below, and believe there might be an issue within it:

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**',
            ephemeral: true,
        })
    } else if (commandName === 'add') {
        interaction.reply({
            content: 'The sum is ${number1 + number2}'
        })
    }
})
  
client.login(process.env.KEY)

Answer №1

If you're facing a similar issue, here's how I resolved it: I visited the bot developer portal and navigated to OAuth2 > URL generator. From there, I selected both "bot" and "applications.commands" for the scope. Next, I scrolled down to choose the necessary permissions for my bot and copied the generated URL.

Answer №2

Your discord server's bot login link was created with incorrect permissions selected.

To fix this, navigate to developers/oauth once more, choose the bot option, and ensure all necessary permissions are selected for your bot.

Afterwards, copy the newly generated link and use it to successfully log in your bot to your server.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

``Is there a way to activate an on-click event when a user selects a page number from the pagination menu in dat

I am currently implementing datatables and I would like to have an alert box pop up when a user clicks on the pagination numbers in the table. Although I attempted to implement this functionality, it only works for the "next" button as demonstrated below: ...

Randomly reorganize elements in a JavaScript array

I am facing an unusual issue while shuffling an array in JavaScript and I am unable to identify the root cause. Can someone provide assistance? When attempting to shuffle an array, the output I receive is unexpected: [1,2,3,4,5,6,7,8,9,10] Instead of ...

Remove the ability to select from the dropped list item

Here is the HTML and Javascript code I used to enable drag and drop functionality for list items from one div to another: HTML: <div class="listArea"> <h4> Drag and Drop list in Green Area: </h4> <ul class="unstyle"> & ...

Secure login verification coupled with intuitive navigation features

Just starting out with react native and I've created a UI for a restaurant app. Now I'm working on converting all static components to dynamic ones. My first task is figuring out how to implement login authentication and then navigate to a specif ...

Prevent IonContent from scrolling to the bottom or top when using Ionic framework

In my Ionic app, I have a long text page with 2 buttons that trigger the actions scrollToBottom and scrollToTop. Due to the length of the page, I have set the scroll duration to be 30 seconds. I am facing two issues here: How can I stop the scrolling ...

Is there a way to determine the image type from a link like this?

I am wondering how to determine the image type from a link like the one below: For example, in HTML like this: <img src="https://www.gravatar.com/avatar/71b0d516013802a2d67aeb7c2e77ed32?s=48&amp;d=identicon&amp;r=PG&amp;f=1" alt="" width= ...

Recording attributes in a React component with Typescript

Is there a way to efficiently document React component props so that the comments are visible in the component's documentation? For example, consider this component: type TableProps = { /** An array of objects with name and key properties */ colu ...

JavaScript event array

I have a JavaScript array that looks like this: var fruits=['apple','orange','peach','strawberry','mango'] I would like to add an event to these elements that will retrieve varieties from my database. Fo ...

Attempting to streamline this function in order to avoid running it nine separate times

I have created a day scheduler and successfully saved data in local storage for one hour field. However, I am looking for a way to streamline this function so that I can use it across all 8-hour fields without duplicating the code. Can someone provide me w ...

"Dynamically setting the selected value in a drop-down box to the

Is there a way to dynamically set the value of a select box using JavaScript to show the current date and a selected period in the past? For example, if today's date is 2018-01-09 and I want to show the date from 30 days ago as selected 2017-12-10, th ...

When utilizing Google Analytics in conjunction with Next.Js, encountering the error message "window.gtag is not

Encountering an error on page load with the message: window.gtag is not a function Using Next.js version 14.0.4. All existing solutions seem to hinder data collection, preventing the website from setting cookie consent correctly. I am uncertain about the ...

The issue arises when the logout component fails to render even after the user has been authenticated. This problem resembles the one discussed in the React Router

While attempting to run the react-router docs example on the browser, I encountered an issue with the AuthButton component. The problem arises when the isAuthenticated value changes to true but the signOut button fails to display. import React from ' ...

How can I create an efficient chat system using Ajax and settimeout without causing excessive virtual memory usage?

I'm in the process of creating a chat application using AJAX that fetches data every second with setTimeout. I have drafted a basic code where there is a number that increments each second by the number retrieved from the PHP page2. Upon testing it on ...

Choose an image from the gallery, assign a class to it, and then store it in the database

I am looking to implement a feature where users can select an image from a collection of images by clicking on a check mark. Once selected, the image should have a green checkbox indicating it is chosen, and then the ID or name of the selected image should ...

Having trouble with images not showing up on React applications built with webpack?

Hey there! I decided not to use the create react app command and instead built everything from scratch. Below is my webpack configuration: const path = require("path"); module.exports = { mode: "development", entry: "./index.js", output: { pa ...

Transmit intricate data structure to a web API endpoint using AJAX requests

When attempting to send a complex object named vm containing an object 'Budget' and an array 'BudgetDetails' populated with rows from an HTML table to my API controller using AJAX, I encounter the error message "Uncaught RangeError: Max ...

Node.js Express - Run a function on each incoming HTTP request

My local Node.js server has a function called unzip() that is responsible for downloading a .json.gz file from an AWS CloudFront URL () and unzipping it into a .json file whenever the server is started with node app.js. Prior to responding to routes, the ...

How to locate the cell with a specific class using JavaScript/jQuery

I need help with the following code snippet: var output = '<tr>'+ '<td class="class1">One</td>'+ '<td class="selected">Two</td>'+ '<td>< ...

What is the best way to dynamically adjust the size of a table or div when clicking in Angular 2?

Is there a way to dynamically resize a table or div in Angular 2 by clicking on a button or other element? I am attempting to change the width of a table from 300px to 100px using a function that utilizes CSS and TypeScript. table{ width: 300px; res ...

Transferring SQL server dates to jQuery Calendar through AJAX communication

I am currently working on implementing a jQuery calendar example, and I want to load the dates from my SQL database instead of using hardcoded values. I am considering using Ajax post to send a request to my web method and retrieve the data. However, I am ...