无法读取未定义的属性“ on”(TypeError)

时间:2018-07-09 17:41:39

标签: javascript discord discord.js

我正在用discord.js编码音乐不一致机器人,但不断出现此错误:

client.on('warn', console.warn);
       ^

TypeError: Cannot read property 'on' of undefined

所以我想知道它是什么。我的机器人代码如下。看看是否有必要,如果知道如何解决,我会很高兴的。

机器人代码:

const { client } = require('discord.js');
const { TOKEN, PREFIX } = require('./config');
const ytdl = require('ytdl-core');


client.on('warn', console.warn);

client.on('error', console.error);

client.on('ready', console.log('Primary systems online'));

client.on('disconnect', console.log('So you know i disconnected, i will reconnect soon...'));

client.on('reconnecting', console.log('I am reconnecting'));

client.on('message', async msg => {
    if (msg.author.bot) return undefined;
    if (msg.content.startswith(PREFIX)) return undefined;
    const args = msg.content.split(' ');

    if (msg.content.startswith('${PREFIX}play')) {
       const voiceChannel = msg.member.voiceChannel;
       if (!voiceChannel) return msg.channel.send('you need to be in a voice channel to use this function.')
       const permissions = voiceChannel.premissionsfor(msg.client.user)
       if (!permissions.has('CONNECT')) {
            return msg.channel.send('i cant go there give me the permission to go there')
       }
       if (!permissions.has('SPEAK')){
           return msg.channel.send('i cannot speak here! I dont have the rights too')
       }

       try {
            var connection = await voiceChannel.join();
       } catch (error) {

            console.error('i could not join the voice channel: ${error}');
            return msg.channel.send('i could not join the voice channel');
       }

       const dispatcher = connection.playstream(ytdl(args[1]))
            .on('end', () => {
                console.log('song ended!')
                voiceChannel.leave();
            })
            .on('error', error => {
                console.error(error)
            });
        dispatcher.setVolumeLogarithmic(5 / 5);
    }
});

client.login(TOKEN);

1 个答案:

答案 0 :(得分:1)

问题是您通过要求Dicord.js来设置客户端,但是应该通过将客户端设置为Discord.Client的新实例来创建客户端。
尝试执行此操作:

const Discord = require("discord.js");
const client = new Discord.Client();

然后,您可以保留其余代码。