【问题标题】:TypeError: Cannot read property 'guild' of undefinedTypeError:无法读取未定义的属性“公会”
【发布时间】:2021-07-02 04:34:54
【问题描述】:

我的机器人已停止工作,这是我的代码。我希望机器人在多个不和谐服务器上侦听@snoop,并在听到 ping snoop 时以 snoop 响应。这是一个基本的倾听和回应

const Discord = require('discord.js');
const client = new Discord.Client();
const start = new Date();
let snoop = 0;

client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); });

client.on('ready', msg => {
    if (msg.guild === null || !msg.guild.hasOwnProperty('members') || msg.guild.members === null) { return }

    const role = msg.mentions.roles.first();

    if (typeof role === "undefined") {
        return;
    }

    const member = msg.guild.members.get(client.user.id);
    const channel = msg.channel;

    // user is not role member of channel
    if (!member.roles.has("" + role.id)) {
        return;
    }

    if (msg.content.match(/(snoop)/i)) {
        console.log("snoop detected", role.name, channel.name, channel.id)

        const t = Math.floor(Math.random() * (3000 - 2500)) + 2500;
        console.debug(`sending snoop to ${channel.name} (${channel.id}) in ${t}ms`)
        channel.startTyping();

        setTimeout(() => {
            channel.send("snoop").then(() => {
                setTimeout(() => {
                    channel.stopTyping();
                }, 100);
            });
        }, t);

        boxes++
        const s = parseInt((new Date()).getTime() - start.getTime()) / (60 * 1000)
        console.info(``)
    }
    }); 
    client.login('apikeyhere')

这是我得到的错误:

typeError: 无法读取未定义的属性“id” 在 ClientDataManager.newChannel (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/client/ClientDataManager.js:81:36) 在 Guild.setup (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/structures/Guild.js:307:68) 在新公会 (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/structures/Guild.js:74:12) 在 ClientDataManager.newGuild (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/client/ClientDataManager.js:26:19) 在 ReadyHandler.handle (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/client/websocket/packets/handlers/Ready.js:20:91) 在 WebSocketPacketManager.handle (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:108:65) 在 WebSocketConnection.onPacket (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:336:35) 在 WebSocketConnection.onMessage (/home/stuart/Downloads/discordbot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:299:17) 在 WebSocket.onMessage (/home/stuart/Downloads/discordbot/node_modules/ws/lib/event-target.js:120:16) 在 WebSocket.emit (events.js:198:13)

当我执行 node index.js 时会出现这种情况

【问题讨论】:

  • 您能否修正您的格式(代码和问题)并尝试解释您想要做什么、您的代码应该做什么以及结果是什么?
  • 它基本上是在所有服务器上侦听 ping 并在听到某个单词时做出响应。抱歉我对这一切的格式不熟悉。
  • 下次你应该在提问之前阅读this article :)
  • 正如我所说,我是这个网站的新手.. 我已经更正了

标签: node.js discord discord.js


【解决方案1】:

您使用了两次“client.on('ready'”。 您正在使用一个来检查机器人是否启动,但您正在使用的另一个不应该是“准备好”而是“消息”。您的代码基于一条消息,而不是机器人的启动时间。

【讨论】:

  • 感谢您的帮助。对此,我真的非常感激。它现在可以工作了,非常感谢!!
猜你喜欢
  • 2021-11-13
  • 2021-06-25
  • 2021-09-14
  • 2020-10-03
  • 2021-05-30
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多