Cannot read property 'length' of undefined

180165
,

Bonjour,

Voila je rencontre un petit problème avec mon code.

Ce que je fais

J'utilise le code d'un bot ratp sur GitHub mais quand j'execute une commande, j'ai une erreur dans la console

const Discord = require('discord.js');
const fs =require('fs');

const bot = new Discord.Client({disableEveryone: true});

bot.commands = new Discord.Collection();

fs.readdir('./commands/', (err, files) => {
    if(err) console.log(err);

    let jsFile = files.filter(f => f.split('.').pop() === 'js');
    if (jsFile.length <= 0){
        console.log('Je ne trouve pas la commande ');
        return;
    }

    jsFile.forEach((f, i ) => {
        let props = require(`./commands/${f}`);
        bot.commands.set(props.help.name, props);
    });
});

bot.on('ready', async() => {
    console.log(`${bot.user.username} est en ligne`);
    bot.user.setActivity('Regardes les ecrans de la RATP');
});

bot.on('message', async message => {
    if (message.author.bot) return;
    if (message.channel.type === 'dm') return;

    let prefix = process.env.PREFIX;
    let messageArray = message.content.split(" ");
    let command =  messageArray [0];
    let args = messageArray.slice(1);

    let commandFile = bot.commands.get(command.slice(prefix.length));
    if (commandFile) commandFile.run(bot, message, args);

});

bot.login('MON TOKEN');


Ce que je veux

J'aimerais que quand j'execute une commande, le bot me retourne un messages sans erreurs.

Ce que j'obtiens

Voici l'erreur que j'obtiens dans ma console :

(node:36968) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
    at Client.bot.on (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\index.js:37:61)
    at Client.emit (events.js:198:13)
    at MessageCreateHandler.handle (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:198:13)
    at Receiver._receiver.onmessage (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\node_modules\ws\lib\websocket.js:137:47)
    at Receiver.dataMessage (C:\Users\Alexa\Downloads\BOT_RATP-master\BOT_RATP-master\node_modules\ws\lib\receiver.js:409:14)
(node:36968) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)

Je vous remercie d'avance pour votre aide !

1 Réponse

44803
,

Bonjour,

Ton code semble planter ligne 37:
let commandFile = bot.commands.get(command.slice(prefix.length));

Ta variable prefix est initilisé par une variable d'environnement PREFIX qui doit être non défini.

Pour corriger je donnerai une valeur de fallback a ta variable prefix:

let prefix = let prefix = process.env.PREFIX || ''