【问题标题】:TypeError: Cannot read properties of undefined (reading 'cache') discord bot trying to read input from a chatroomTypeError:无法读取未定义的属性(读取\'cache\')不和谐机器人试图从聊天室读取输入
【发布时间】:2022-11-13 13:15:23
【问题描述】:

我正在尝试创建一个不和谐的机器人,允许 2 名队长有两个交替的挑选团队,直到每边有 4 人团队。 (1 名船长和 3 名其他人)。 8名成员签到后随机抽取队长;他们准备好了。

`

class MessageCreateAction extends Action {
  handle(data) {
    const client = this.client;
    const channel = client.channels.cache.get(data.channel_id);
    if (channel) {
      const existing = channel.messages.cache.get(data.id);
      if (existing) return { message: existing };
      const message = channel.messages.add(data);
      const user = message.author;
      let member = message.member;
      channel.lastMessageID = data.id;
      if (user) {
        user.lastMessageID = data.id;
        user.lastMessageChannelID = channel.id;
      }
      if (member) {
        member.lastMessageID = data.id;
        member.lastMessageChannelID = channel.id;
      }

      /**
       * Emitted whenever a message is created.
       * @event Client#message
       * @param {Message} message The created message
       */
      client.emit(Events.MESSAGE_CREATE, message);
      return { message };
    }

    return {};
  }
}

`

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如它目前所写的那样,很难准确地说出你在问什么。

标签: javascript discord discord.js


【解决方案1】:

您首先需要获取频道所在的公会,然后才能获取频道和消息。

你也可以看到另一个用户发的这个帖子有同样的错误你https://stackoverflow.com/a/66873076/15256312

你需要使用异步(异步)使用等待

const client = this.client;
const guild = client.guilds.cache.fetch(data.guild_id);
const channel = guild.channels.cache.get(data.channel_id);
if (channel) {
    const existing = await channel.messages.fetch(data.id);
    //...
}

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 2023-01-14
    • 1970-01-01
    • 2020-07-31
    • 2021-05-25
    • 2019-01-27
    • 2021-12-20
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多