I wanted to know the error in this code and a solution to it. Thanks in advance! When the giveaway bot announces the winner, it starts with Congratulations <@user> . . . The bot needs to check the @user's roles and let the host know the claim time for the winner. I would be thankful if i get a response at the earliest. I'm using repl.it to code the bot (it's private, only for my server) and it shows error in line ,
if (message.member.roles.has(allowedRole.id) {
I want a solution for this to resolve my problem.
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・supporter");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have **15** seconds to DM the host!`)
.then(message => { setTimeout(function() { message.edit('**Time up!**')) }, 15000)})
} else {
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・donator");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have **25** seconds to DM the host!`)
.then(message => { setTimeout(function() { message.edit('**Time up!**')) }, 25000)})
} else {
let winner = msg.mentions.users.first();
if (!winner) msg.reply("Unexpected Error");
let allowedRole = message.guild.roles.find("name", "・booster");
if (message.member.roles.has(allowedRole.id) {
msg.channel.send(`GG! You have infinite time to DM!`)})
}
}
I see a few issues with your code here.
First, you're switching between msg
and message
constantly. I'm going to assume that those aren't two seperate objects you're trying to access, so you should only be using one of those.
Second, you're not searching for roles correctly. Since discord.js v12, you have to use cache
when requesting data from a collection (see here for more info on what changed in v12). Also, when searching for roles, you can't use ("name", "<name>")
as far as I know. In your case, you would need to use msg.member.roles.cache.has(allowedRole.id)
and msg.guild.roles.cache.find(r => r.name === "・booster")
Third, you were checking the roles for the message author, not the winner. I assume you want to check if the winner has those certain roles, not the user who chose said winner.
Finally, you had a ton of issues with leaving parentheses open. That would also cause a ton of errors.
I cleaned up your code quite a bit. This should work a lot better for you
const winner = msg.mentions.members.first(); //Get the winner
if (!winner) { //Send an error if there's no winner specified
return msg.reply("Unexpected Error");
};
var time = 0; //This is blank because we'll edit it later
//The line below will look through the winner's roles, find either of the three specified roles, and return the first one it finds. It will look for booster first, followed by donator, then supporter
const allowedRole = winner.roles.cache.find(r => r.name === "・booster") || winner.roles.cache.find(r => r.name === "・donator") || winner.roles.cache.find(r => r.name === "・supporter");
if (!allowedRole) { //Do something if the winner doesn't have one of the needed roles
return;
};
switch (allowedRole.name) { //Set the time appropriately depending on the role name
case "・supporter":
time = 1500;
break;
case "・donator":
time = 2500;
break;
case "・booster":
time = "infinite";
break;
};
if (time === "infinite") { //Send one message if they have infinite time, and another if not
msg.channel.send(`GG! You have infinite time to DM!`);
} else {
msg.channel.send(`GG! You have **${time / 1000}** seconds to DM the host!`) //Divide the time by one thousand since we stored it in milliseconds
.then(message => {
setTimeout(function () {
message.edit('**Time up!**');
}, time); //Set the timeout to use our specified time
});
};
Typically when coding you want to avoid repeating yourself as much as possible. You essentially had the exact same code three times when you only had to have most of it once. This code eliminates the need for repeating yourself.