【问题标题】:Cannot read property 'on' of undefined (TypeError)无法读取未定义的属性“on”(TypeError)
【发布时间】:2018-12-17 11:22:27
【问题描述】:

我正在使用 discord.js 编写音乐不和谐机器人,但我不断收到此错误:

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

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

所以我想知道它是什么。我在下面有机器人代码。如果您愿意,请查看一下,如果您知道如何解决它,我会很高兴。

机器人代码:

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

【问题讨论】:

  • 但随后出现此错误: const client = new Discord.Client(); ^ ReferenceError: Discord is not defined (ninja edit 得到了那个工作,但现在我不能得到其他工作
  • 抛出新的错误。ERR_INVALID_ARG_TYPE('listener', 'Function', listener); ^ 这是我无法工作的事情

标签: javascript discord discord.js


【解决方案1】:

问题是您通过需要 Dicord.js 来设置客户端,但您应该通过将其设置为 Discord.Client 的新实例来创建客户端。
尝试这样做:

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

那么你就可以保留剩下的代码了。

【讨论】:

  • 您可能可以继续使用相同的语法,但只需要将 C 大写,因此 const { Client } = require('discord.js');,但是您仍然需要使用 const client = new Client(); 实例化它
  • 是的,我只是认为最好保留声明 Discord.js 以防您需要另一个类
  • 这是漏洞错误:events.js:200 throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener); ^ TypeError [ERR_INVALID_ARG_TYPE]:“监听器”参数必须是函数类型。接收类型未定义
  • 此错误与问题中的问题无关:它可能是一些损坏的client.on(...) 的结果。检查您的 client.on(...) 语句并查看您是否始终将函数作为侦听器 (client.on("event", -> listener <-)) 传递,因为它表示您在代码中的某处将 undefined 作为侦听器传递
猜你喜欢
  • 2022-06-15
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多