【问题标题】:Why can't my bot get channels? discord.js为什么我的机器人无法获取频道?不和谐.js
【发布时间】:2021-10-25 21:06:30
【问题描述】:

我在控制台中收到此错误:TypeError: Cannot read property 'channels' of undefined

导致问题的代码:

const channel = guild.channels.cache.find(c => c.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))

Index.js:

client.on("guildCreate", guild => {
  client.commands.get("joinserver").execute(guild)
});

加入服务器.js:

const Discord = require("discord.js")

module.exports = {
  name: 'joinserver',
  description: '',
  execute(message, args, client, guild) {
    const channel = guild.channels.cache.find(c => c.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))
    const embeds = new Discord.MessageEmbed()
    channel.send(embeds)
  }
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您将guild 对象作为第一个参数(不是guild 而是message 参数)传递给execute 函数。所以你的公会现在可以在execute 函数中作为message 访问。您应该将公会传递给正确的参数(示例中的第 4 个参数)或更改 execute 函数接收参数的方式。

    client.on("guildCreate", guild => {
      // This should work: you need to pass null to `message`, `args` and `client`
      // because these arguments marked as required
      client.commands.get("joinserver").execute(null, null, null, guild)
    });
    

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2019-10-03
      • 1970-01-01
      • 2021-08-03
      • 2021-09-11
      • 2021-01-20
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多