【问题标题】:405 Error while creating a channel in Discord using Discord.js使用 Discord.js 在 Discord 中创建频道时出现 405 错误
【发布时间】:2021-07-20 04:32:36
【问题描述】:

我在尝试创建频道时收到405: Method Not Allowed 错误。

这是我目前使用的代码:

const Discord = require('discord.js'),
    dotenv = require('dotenv').config()

const client = new Discord.Client()
const server = new Discord.Guild(client)

client.login(process.env.TOKEN)

client.on('ready', () => {
    console.log('Bot Online!')
})

client.on('message', (message) => {
    if (message.author.bot) return

    message.channel.send(`Hello ${message.author}, how are you?`)
})

server.channels
    .create('test', {
        parent: 'test-channels',
    })
    .then((result) => {
        console.log(result)
    })
    .catch((error) => {
        console.log(error)
    })

这是确切的错误:

DiscordAPIError: 405: Method Not Allowed
    at RequestHandler.execute (D:\Programming\Web Development\Projects\Dispatch\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async RequestHandler.push (D:\Programming\Web Development\Projects\Dispatch\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async GuildChannelManager.create (D:\Programming\Web Development\Projects\Dispatch\node_modules\discord.js\src\managers\GuildChannelManager.js:112:18) {
  method: 'post',
  path: '/guilds/channels',
  code: 0,
  httpStatus: 405
}

我已在服务器中授予机器人管理员权限,因此这应该不是问题。请帮我解决这个问题。

【问题讨论】:

  • 您确定您使用的令牌正确吗?
  • 是的@DJ,我是。那么客户端命令是如何工作的呢?
  • 我建议使用公会 ID client.guilds.cache.get('the-guild-id-here'); 获取公会,然后在 ready 事件中做任何你想做的事情。
  • 好的,如何获取公会的ID? @DJ

标签: node.js discord discord.js


【解决方案1】:

我建议以这种方式使用其 ID 获取公会,然后在准备好的事件中使用它做任何你想做的事情:

client.on('ready', () => {
    console.log('Bot Online!')
    const server = client.guilds.cache.get('the-server-id-here');
    server.channels
    .create('test', {
        parent: 'id-of-the-parent-category',
    })
    .then((result) => {
        console.log(result)
    })
    .catch((error) => {
        console.log(error)
    })
})

要手动获取服务器的 ID,请按照以下步骤操作:

  1. 在 Discord 中转到用户设置 > 高级。
  2. 打开开发者模式。
  3. 右键单击任何服务器的图标,然后单击“复制 ID”。
  4. 在需要的地方粘贴它!

注意-频道的父级必须是 ID 或频道对象。

【讨论】:

  • 如何获取服务器 ID?
  • 编辑了答案。
  • 啊,但是我找到了从代码中获取它的方法。 client.guilds.cache.map((guild) => console.log(guild.id))
  • 这将记录 bot 所在的所有服务器的 ID。如果您的 bot 打算在几个服务器中,您也可以使用它!
  • 是的,这只是我熟悉图书馆的过程。但是,现在它引入了一个新的错误。 Invalid Form Body parent_id: Value "test-channels" is not snowflake.
猜你喜欢
  • 2021-08-05
  • 2023-03-27
  • 2023-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多