microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.34k stars 28.9k forks source link

TS(6133) error on Discord.js program with VSC #74769

Closed FuzzyDark closed 5 years ago

FuzzyDark commented 5 years ago

Hey guys, I'm working on a basic bot Discord.js project and I'm having some trouble... I'm making some commands, that could rank people and at the same time give them info (without some arguments that I'm going to show...), and I have trouble with a variable.. Codeblocks following:

if(args > 0){
        let option = args.splice(1, 1);
    }else{
        let option;
    }

that "option" one is the one which I'm having trouble with... It constantly says " 'option' is declared but its value is never read.ts(6133)".

now the thing is, 'option' is what follows the main command, an example would be:

!rankme god

where "!" is the prefix, "rankme" the args, and "god" the option. So in that case 'option' exists, because in my program, "god" is a role with it's correspondant ID, but if had this:

!rankme

then it throws all this error code:

C:\Users\User\Desktop\Programación\Bots Discord\Testa1\index.js:42
            if(option){
            ^

ReferenceError: option is not defined
    at Client.client.on.message (C:\Users\User\Desktop\Programación\Bots Discord\Testa1\index.js:42:13)
    at Client.emit (events.js:197:13)
    at MessageCreateHandler.handle (C:\Users\User\Desktop\Programación\Bots Discord\Testa1\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\User\Desktop\Programación\Bots Discord\Testa1\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (C:\Users\User\Desktop\Programación\Bots Discord\Testa1\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\User\Desktop\Programación\Bots Discord\Testa1\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\User\Desktop\Programación\Bots Discord\Testa1\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:197:13)
    at Receiver.receiverOnMessage (C:\Users\User\Desktop\Programación\Bots Discord\Testa1\node_modules\ws\lib\websocket.js:789:20)
    at Receiver.emit (events.js:197:13)

Here's the rest of the code that "terms" the option thing, it's below the initialization:

    if(message.content.startsWith(prefx)){
         if(cmd === "test"){
             message.channel.send("Happy that you're testing");
        }
        if(cmd === "rankme"){
            if(option){
                if(option === "c"){
                    message.member.addRole("<584945636659429389>");
                }
                else if(option === "god"){
                    message.member.addRole("<584952054322298892>");
                }else{
                    message.channel.send("That guild role doesn't exist");
                }
            } else{
                message.channel.send({embed:{
                    title: "Rank commands",
                    color: 0xFF0000,
                    fields: [
                        {name: "Guild: ", value: "Losttempel Family: \nEvolution@ Family: \nFighters Family: \n", inline: true},
                        {name: "Command: ", value: "!rankme c\n!rankme e\n!rankme f", inline: true}]}});
            }
        }
    }
mjbvz commented 5 years ago

Unless I am misunderstanding your code, you need to put the declaration of option outside of conditional block:


let option;
if(args > 0){
     option = args.splice(1, 1);
    }else{
        ....
    }
   ...
    if(message.content.startsWith(prefx)
       ....
FuzzyDark commented 5 years ago

I figured it out 3 hours ago lol Thanks anyway, I'll take that