NodeJS 检查机器人可以访问的频道并发送消息

vlju58qv  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(136)

我试着让我的机器人在加入公会时输入消息,但是好像不起作用。
我尝试过的:(在一些其他变化中)

const { PermissionsBitField } = require('discord.js');
module.exports = async (client, guild) =>{

    const channel = guild.channels.cache.find(channel => channel.type === 0 && channel.permissionsFor(guild.members.me).has(PermissionsBitField.Flags.SendMessages))
    channel.send("Thank you for inviting me!")

}
const { PermissionsBitField } = require('discord.js');
module.exports = async (client, guild) =>{

    const channel = guild.channels.cache.find(channel => channel.type === 0 && guild.members.me.permissionsIn(channel).has(PermissionsBitField.Flags.SendMessages))
    channel.send("Thank you for inviting me!")

}

出于某种原因,它仍然试图在机器人没有权限发送消息的通道中发送消息。

throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
              ^

DiscordAPIError[50001]: Missing Access
    at SequentialHandler.runRequest (C:\Users\Gleyv\3D Objects\Botveon [Pre-Alpha]\node_modules\@discordjs\rest\dist\index.js:659:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (C:\Users\Gleyv\3D Objects\Botveon [Pre-Alpha]\node_modules\@discordjs\rest\dist\index.js:458:14)
    at async REST.request (C:\Users\Gleyv\3D Objects\Botveon [Pre-Alpha]\node_modules\@discordjs\rest\dist\index.js:902:22)
    at async TextChannel.send (C:\Users\Gleyv\3D Objects\Botveon [Pre-Alpha]\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:15) {
  requestBody: {
    files: [],
    json: {
      content: 'Thank you for inviting me!',
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: undefined,
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined,
      thread_name: undefined
    }
  },
  rawError: { message: 'Missing Access', code: 50001 },
  code: 50001,
  status: 403,
  method: 'POST',
  url: 'https://discord.com/api/v10/channels/1007005313968390306/messages'
}
vnzz0bqm

vnzz0bqm1#

结果发现
&&channel.permissionsFor(guild.members.me).has(PermissionsBitField.Flags.ViewChannel)已经解决了我的问题。

相关问题