Discord JS - DiscordAPIError:缺少访问权限

Zel*_*ell 7 javascript typescript discord discord.js

所以我按照磨损钥匙教程来不和谐机器人,我不知道问题是什么,这是错误

/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: []
  }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试查看我的代码,但没有发现任何问题。

这是我的代码,我真的认为代码中有问题。

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)
Run Code Online (Sandbox Code Playgroud)

小智 10

对于任何有同样问题的人。我只需转到机器人开发人员门户,然后转到 OAuth2 > URL 生成器即可解决此问题。对于范围,选择“bot”和“applications.commands”。然后向下滚动选择您的机器人需要复制 URL 的任何权限。


Nab*_*han 7

您在为不和谐服务器创建机器人登录链接时没有选择正确的权限。

再次转到开发人员/oauth,单击机器人并选择您在此机器人中使用的所有必需权限。

然后复制生成的链接并使用它通过您的机器人登录到您的服务器。

  • 如果他们的答案解决了您的问题,请务必勾选它 (3认同)
  • @Zell,我很高兴让我们知道您如何解决您的问题。您的修复可以帮助其他人,就像您在此处寻求帮助一样 (2认同)