#Error Command

69 messages · Page 1 of 1 (latest)

elder burrow
#

create a react-role command, but when I run it it gives me the following error in the console.

      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read properties of null (reading 'name')
    at Object.execute (c:\Users\tiuel\Desktop\Yuji\command\utilidades\reactionrole.js:87:26)
    at Object.execute (c:\Users\tiuel\Desktop\Yuji\event\interactionCreate.js:7:25)
    at Client.<anonymous> (c:\Users\tiuel\Desktop\Yuji\funcao\handleCommands.js:14:45)
    at Client.emit (node:events:513:28)
    at InteractionCreateAction.handle (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketShard.js:494:22)
    at WebSocketShard.onMessage (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10)
    at callListener (c:\Users\tiuel\Desktop\Yuji\node_modules\ws\lib\event-target.js:290:14)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21)```
desert valeBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

elder burrow
jade pine
# elder burrow edited the post

cargo4 is sometimes null, so the error occurs when accessing .name. Either require that option, or check if it exists before accessing it

elder burrow
jade pine
#

When someone runs the command, if they don't supply a value for cargo4, you are going to have errors when trying to access it here .setLabel(${cargo4.name}).

You either need to make that option required, or check if cargo4 exists before accessing it

elder burrow
#

it exists, as I said, I want to make it a position that you are not obliged to put in, and just one more option if the person wants to put it in or not

jade pine
#

If the command option isn't required then cargo4 might not exist sometimes. So again, check it exists before accessing it

elder burrow
#

and how am i going to do that? you don't understand very well.. if it's to check the code, I did it just now

jade pine
#

I understand perfectly fine. I am telling you how to fix it.

Check if cargo4 exists before accessing the name property

#

Don't physically go to your code and check, I mean write code to check if it exists

if (!exists) {
  // don't check it
}
elder burrow
#

Ok

umbral token
jade pine
elder burrow
elder burrow
jade pine
# elder burrow I'm sorry, but I'm a layman, there are times and I still don't understand what t...

Try something like this

        let botao = new ActionRowBuilder()
            .addComponents(
                new ButtonBuilder()
                    .setCustomId('botao1')
                    .setLabel(`${cargo1.name}`)
                    .setStyle(ButtonStyle.Primary),

                new ButtonBuilder()
                    .setCustomId('botao2')
                    .setLabel(`${cargo2.name}`)
                    .setStyle(ButtonStyle.Primary),

                new ButtonBuilder()
                    .setCustomId('botao3')
                    .setLabel(`${cargo3.name}`)
                    .setStyle(ButtonStyle.Primary),
            );

        if (cargo4) {
            botao.addComponents(
                new ButtonBuilder()
                    .setCustomId('botao4')
                    .setLabel(`${cargo4.name}`)
                    .setStyle(ButtonStyle.Primary)
            )
        }
elder burrow
#
      throw er; // Unhandled 'error' event
      ^

TypeError [ColorConvert]: Unable to convert color to a number.
    at resolveColor (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\util\Util.js:260:63)
    at EmbedBuilder.setColor (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\structures\EmbedBuilder.js:22:36)
    at Object.execute (c:\Users\tiuel\Desktop\Yuji\command\utilidades\reactionrole.js:96:5)
    at Object.execute (c:\Users\tiuel\Desktop\Yuji\event\interactionCreate.js:7:25)
    at Client.<anonymous> (c:\Users\tiuel\Desktop\Yuji\funcao\handleCommands.js:14:45)
    at Client.emit (node:events:513:28)
    at InteractionCreateAction.handle (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketShard.js:494:22)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
  code: 'ColorConvert'
}```
formal gorge
#

What color did you put in reactionrole.js line 96?

elder burrow
formal gorge
#

Then your error is to be expected if the user puts one that is invalid

#

You should handle that

elder burrow
#

but in case it is a string value, then it can put it in hexadecimal right? because this is the logic that I have when making the code

formal gorge
#

If it‘s formatted as a correct hexadecimal color, yes. #ff0000 would work, #da wouldn’t for example

#

Neither would #discord

elder burrow
#

yes, that's why I put an example in the description, but I believe it makes a mistake in thinking here. I'll show you how it is now

#
            .setColor(`${cor.name}`)
            .setTitle(`${titulo.name}`)
            .setDescription(`${descricao.name}`);```
formal gorge
#

You need to catch that error either way. Always account for user error

elder burrow
#

but I think the correct formula is not this one but this one

#
            .setColor(cor)
            .setTitle(titulo)
            .setDescription(descricao);```
formal gorge
elder burrow
elder burrow
formal gorge
elder burrow
#

defines it like this, my mistake is wanting to put the name and not the declared value of the const to appear

#

soon then he can put it in hexadecimal or even write the name of the color (which I think might work)

#

I'll test here

#
            else if (i.guild.members.me.roles.highest.position < cargo4.position) {
                                                                        ^

TypeError: Cannot read properties of null (reading 'position')
    at InteractionCollector.<anonymous> (c:\Users\tiuel\Desktop\Yuji\command\utilidades\reactionrole.js:120:64)
    at InteractionCollector.emit (node:events:525:35)
    at InteractionCollector.handleCollect (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\structures\interfaces\Collector.js:133:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)```
#

now this error is occurring to me here, I even imagine what it is. Because in my line of reasoning I created 4 options for a position, 3 of which are mandatory to place. only I want to leave one or even more being optional to the user

formal gorge
#

Then do the same you did for cargo4 earlier. Check for it existing before trying to use it

elder burrow
#

I did this verification method

#
        if (!cargo1 || !cargo2 || !cargo3) {
            return inter.reply({ content: 'Por favor, forneça as opções obrigatórias!', ephemeral: true });
        }

        // Verifica se a opção 4 foi fornecida
        if (cargo4) {
            // código do comando para quando a opção 4 foi fornecida
            return inter.reply({ content: `Você escolheu ${cargo1}, ${cargo2}, ${cargo3} e ${cargo4}.`, ephemeral: true });
        } else {
            // código do comando para quando a opção 4 não foi fornecida
            inter.reply({ content: `Você escolheu ${cargo1}, ${cargo2} e ${cargo3}.`, ephemeral: true });
        }```
#

but it is giving me this error

formal gorge
#

Line 120 is where in that?

elder burrow
#

Wait

#
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read properties of null (reading 'name')
    at Object.execute (c:\Users\tiuel\Desktop\Yuji\command\utilidades\reactionrole.js:101:26)
    at Object.execute (c:\Users\tiuel\Desktop\Yuji\event\interactionCreate.js:7:25)
    at Client.<anonymous> (c:\Users\tiuel\Desktop\Yuji\funcao\handleCommands.js:14:45)
    at Client.emit (node:events:513:28)
    at InteractionCreateAction.handle (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketShard.js:494:22)
    at WebSocketShard.onMessage (c:\Users\tiuel\Desktop\Yuji\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10)
    at callListener (c:\Users\tiuel\Desktop\Yuji\node_modules\ws\lib\event-target.js:290:14)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21)```
#

when he checks it shows that there is an error on line 101 which is where the cargo4 button is

formal gorge
#

Do you know how to read stacktraces? It tells you what line in what file the issue is from, so fix it in that line

formal gorge
elder burrow
elder burrow
formal gorge
#

Show your lines 90-110 then

elder burrow
#
                    .setLabel(`${cargo4.name}`)
                    .setStyle(ButtonStyle.Primary)
            )
        }

        const embed = new EmbedBuilder()
            .setColor(cor)
            .setTitle(titulo)
            .setDescription(descricao.name);

        inter.reply({ embeds: [embed], components: [botao] });

        const coletor = inter.channel.createMessageComponentCollector();

        coletor.on('collect', async (i) => {

            const member = i.member;

            if (i.guild.members.me.roles.highest.position < cargo1.position) {
                i.update({ content: 'Meu cargo esta abaixo do cargo que estou tentando entregar', ephemeral: true });```
formal gorge
#

Please learn to look at the correct lines the stacktrace tells you about

elder burrow
elder burrow
formal gorge
#

Hint: it‘s near the name of the file the error is in

elder burrow
#

Ok

elder burrow
#
    i.update({ content: 'Meu cargo esta abaixo do cargo que estou tentando entregar', ephemeral: true });
    return;
}```
#

I should remove the else and leave it like this:

#
    i.update({ content: 'Meu cargo esta abaixo do cargo que estou tentando entregar', ephemeral: true });
    return;
}```
#

by the way not and that, because the error still persists

-_-

formal gorge