SyntaxError: Unexpected token "else"


#1

Hello! I’m a rookie and I started this just a few days ago, I’m trying to make a Bot with Discord.js that depending on the role of whoever uses the command, send a random message from a different list,

I have the error on line 48, specifically.

I leave absolutely all the code here, I hope you can help me, because I really do not see the problem anywhere, was made with node.js

console. log('El bot esta listo');
const Discord = require('discord.js');
const bot = new Discord.Client();
const prefix = "m!";
var bron = ['Bronce1', 'Bronce2', 'Bronce3',
'Bronce4', 'Bronce5', 'Bronce6',
'Bronce7', 'Bronce8', 'Bronce9',
'Bronce10', 'Bronce11', 'Bronce12'
];

var pla = ['Plata1', 'Plata2', 'Plata3',
'Plata4', 'Plata5', 'Plata6',
'Plata7', 'Plata8', 'Plata9',
'Plata10', 'Plata11', 'Plata12'
];

var oro = ['Oro1', 'Oro2', 'Oro3',
'Oro4', 'Oro5', 'Oro6',
'Oro7', 'Oro8', 'Oro9',
'Oro10', 'Oro11', 'Oro12'
];

var plati = ['Platino1', 'Platino2', 'Platino3',
'Platino4', 'Platino5', 'Platino6',
'Platino7', 'Platino8', 'Platino9',
'Platino10', 'Platino11', 'Platino12'
];

var diam = ['Diamante1', 'Diamante2', 'Diamante3',
'Diamante4', 'Diamante5', 'Diamante6',
'Diamante7', 'Diamante8', 'Diamante9',
'Diamante10', 'Diamante11', 'Diamante12'
];

var p1 = [Math.round(Math.random() * pla.length)];

bot.login('the id of my bot');

bot.on('message', (message) => {
	if (message.content.startsWith(prefix + 'carta')) {
		if (message.member.roles.find("name", "Bronce"))
		var b1 = bron[Math.round(Math.random() * bron.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + b1);
	} else {
		if (message.member.roles.find("name", "Plata"))
		var p1 = pla[Math.round(Math.random() * pla.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + p1);
	} else {
		if (message.member.roles.find("name", "Oro"))
		var or1 = oro[Math.round(Math.random() * oro.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + or1);
	} else {
		if (message.member.roles.find("name", "Platino"))
		var pl1 = plati[Math.round(Math.random() * plati.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + pl1);
	} else {
		if (message.member.roles.find("name", "Diamante"))
		var d1 = diam[Math.round(Math.random() * diam.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + d1);
	} else {
		return;

	}
});

#2

you can’t do:

else {

} else {

}

else means literally that, everything else. So if you have two else in a row, the second one can never be reached/the first else clause will always run

use else if instead


#3

Thank’s but, if i use “else if”
example:

} else if { (message.member.roles.find("name", "Plata"))
		var p1 = pla[Math.round(Math.random() * pla.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + p1);

All the rest of the code goes dump (it literally does not work)
I know it’s a lot to ask for this, but could you edit the code to make it work, please? I think that in that way could help me better understand what I did and, for the next, it will not happen again.


#4

using else if requires a condition, see documentation:

https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Statements/if...else#Using_else_if

i am convinced that if i fix the code, you will not learn as much. Fixing the error yourself would be better. Is this an exercise? Can you share me the url so i can have a look at the instructions?

What are you trying to achieve? The code doesn’t contain comments which explain what you try to achieve?


#5

You must be thinking I’m fool or something xD
I followed what the link says and … it generates the same error, in the same place.
I imagine that, to better understand what I did, I should send the complete code again.

bot.on('message', (message) => {
	if (message.content.startsWith(prefix + 'carta')) {
		if (message.member.roles.find("name", "Bronce"))
		var b1 = bron[Math.round(Math.random() * bron.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + b1);
	} else if (message.member.roles.find("name", "Plata"))
		var p1 = pla[Math.round(Math.random() * pla.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + p1); {
	} else if (message.member.roles.find("name", "Oro"))
		var or1 = oro[Math.round(Math.random() * oro.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + or1); {
	} else if (message.member.roles.find("name", "Platino"))
		var pl1 = plati[Math.round(Math.random() * plati.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + pl1); {
	} else if (message.member.roles.find("name", "Diamante"))
		var d1 = diam[Math.round(Math.random() * diam.length)]
		message.channel.sendMessage(message.author + ' Su mision es: ' + d1); {
	} else {
		return;

	}
});

#6

but on a higher abstract level, what are you trying to do? If there is a carta prefix look for different roles? I can’t help you, if you can’t explain what your code should do.

so then you get:

if (message.content.startsWith(prefix + 'carta')) {
    if (message.member.roles.find("name", "Bronce")) {
   
    } else if (message.member.roles.find("name", "Plata")){

    }
}

you miss a lot of curly brackets, if unsure always use {} for if, else if and else


#7

Oh I’m sorry
You will see my code should check what role the user has to use the command: prefix ‘carta’ or in such case: m!carta
If the user has a ‘bronce’ role, he will send in the message some random data of the var bron
but if the user has the role plata send the variable pla and so with the others.

let me try the last edition you sent me, maybe it’s finally


#8

Well, it turns out that all the problem was the brackets, thank you very much for having me patience haha!


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.