简体   繁体   English

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

[英]Cannot read property 'on' of undefined (TypeError)

I am coding a music discord bot with discord.js and I keep getting this error: 我正在使用discord.js编码音乐discord机器人,但不断出现此错误:

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

TypeError: Cannot read property 'on' of undefined

So I wonder what it is. 所以我想知道这是什么。 I have the bot code down below. 我的机器人代码如下。 Take a look through if you want and if you know how to fix it I would be happy. 看看是否需要,如果您知道如何解决,我将很高兴。

The bot code: 机器人代码:

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);

The problem is that you're setting the client by requiring Dicord.js, but you should create the client by setting it as a new instance of Discord.Client . 问题是您通过要求Dicord.js来设置客户端,但是应该通过将其设置为Discord.Client的新实例来创建客户端。
Try to do this: 尝试这样做:

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

Then you can keep the rest of the code. 然后,您可以保留其余代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM