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

时间:2021-02-23 14:33:51

标签: javascript event-handling discord discord.js typeerror

我正在使用 discord.js 编写一个 discord bot,在添加了一个事件文件夹来清理我的 index.js 文件后,我拥有的 ready 事件不再起作用,我也没有改变一行。

这是新的 ready 活动

const { testserver, creator } = require('../../../config.json')

module.exports = (client, Discord) => {
        console.log(`Connected as ${client.user.tag}`);
        const MyUsername = client.guilds.resolve(testserver).members.resolve(creator).user
        const OnlineEmbed = new Discord.MessageEmbed()
        .setColor('#0099ff')
        .setTitle('Online!')
        .setAuthor(client.user.username, client.user.displayAvatarURL())
        .setTimestamp()
        .setFooter('By ' + MyUsername.tag, MyUsername.displayAvatarURL());

        const channel = client.channels.cache.get('channelid');
        channel.send(OnlineEmbed);
        channel.send('=======================')
}

这是我得到的错误

(node:22076) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'tag' of undefined
    at module.exports (MyDir\MyBot\main\events\client\ready.js:4:49)
    at Client.emit (events.js:315:20)
    at WebSocketManager.triggerClientReady (MyDir\MyBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:431:17)
    at WebSocketManager.checkShardsReady (MyDir\MyBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:415:10)
    at WebSocketShard.<anonymous> (MyDir\MyBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:197:14)
    at WebSocketShard.emit (events.js:315:20)
    at WebSocketShard.checkReady (MyDir\MyBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:473:12)
    at WebSocketShard.onPacket (MyDir\MyBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:445:16)
    at WebSocketShard.onMessage (MyDir\MyBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:299:10)
    at WebSocket.onMessage (MyDir\MyBot\node_modules\ws\lib\event-target.js:132:16)

这是事件处理的代码

const fs = require('fs');

module.exports = (client, Discord) => {
    const load_dir = (dirs) => {
        const event_files = fs.readdirSync(`./main/events/${dirs}`).filter(file => file.endsWith('.js'))

        for (const file of event_files) {
            const event = require(`../events/${dirs}/${file}`);
            const event_name = file.split('.')[0];
            client.on(event_name, event.bind(null, Discord, client))

        }
    }
    ['client', 'guild'].forEach(e => load_dir(e))
}

这是我的 index.js

const Discord = require('discord.js');
const client = new Discord.Client();
const { token } = require('../config.json');

client.commands = new Discord.Collection();
client.events = new Discord.Collection();

['command_handler', 'event_handler'].forEach(handler => {
    require(`./handlers/${handler}`)(client, Discord);
})

client.login(token)

我能做什么?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

用这个替换你的事件处理文件夹:

const fs = require('fs');

module.exports = (client, Discord) => {
  const load_dir = (dirs) => {
    const event_files = fs.readdirSync(`./main/events/${dirs}`).filter(file => 
    file.endsWith('.js'))

    for (const file of event_files) {
        const event = require(`../events/${dirs}/${file}`);
        const event_name = file.split('.')[0];
        client.on(event_name, event.bind(null, client, Discord))

    }
}
  ['client', 'guild'].forEach(e => load_dir(e))
}

你做错了什么?

<块引用>

client.on(event_name, event.bind(null, Discord, client));

在这一行中,您首先提到了“Discord”,因此在调用 ready.js 时,它会在返回 undefined 的“Discord”中找到 .user。