类型错误:无法读取未定义的属性“公会”

时间:2021-04-06 11:34:21

标签: node.js discord discord.js

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

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 时出现这个

1 个答案:

答案 0 :(得分:0)

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