client.on("message", (message) => {
if(message.content.startsWith("!Bonk")){
let userToMention = message.mentions.users.first();
message.channel.send("bonks "+userToMention.toString() + " https://tenor.com/view/bonk-meme-dog-doge-gif-14889944");
}
});
What this does is when you run the command !bonk #devildog the bot displays a gif + #devildog.
But when you run the command !bonk alone the bot randomly crashes. Is there any way you can solve this issue with if else?
Error:
message.channel.send("bonks "+userToMention.toString() + "https://tenor.com/view/bonk-meme-dog-doge-gif-14889944");
^
TypeError: Cannot read property 'toString' of undefined
Since in your example, the invocation command doesn't include any mentions, the call to message.mentions.users.first(); will cause a crash. When no users are mentioned (like in your example), the first() method won't be able to find the first user in users, because there are no users mentioned at all! Thus, calling toString() on a null/undefined object will result in an exception.
Check for users in your initial if statement to prevent this behavior:
client.on("message", (message) => {
if(message.content.startsWith("!Bonk") && message.mentions.users.array.length > 0){
let userToMention = message.mentions.users.first();
message.channel.send("bonks "+userToMention.toString() + " https://tenor.com/view/bonk-meme-dog-doge-gif-14889944");
}
});
Hello again #Blissy89!
Yes, you can fix it by just using:
if(!userToMention) return message.say('Mention a user to run this command!')
Put it ABOVE message.channel.send
Related
I'm trying to make a command when you mention the bot, it will send a message saying “Slash Command: /help”. I have tried to make it and I tried to find an answer but I can’t find anything that works for me.
Here is the code (I use the code in index.js):
client.on("messageCreate", (message) => {
if (message.author.bot) return false;
if (message.content.includes("#here") || message.content.includes("#everyone") || message.type == "REPLY") return false;
if (message.mentions.has(client.user.id)) {
message.channel.send("Hello there!");
}
});
I added the GatewayIntentBits.Guilds intent to my intents array. I don't get any errors, it just doesn’t work.
If you're only using the GatewayIntentBits.Guilds intent, you won't receive messages. You'll also need to add GatewayIntentBits.GuildMessages. It allows you to receive messages if the client/bot is mentioned.
If you also want to receive the message content when the bot is not mentioned, you'll also need to add GatewayIntentBits.MessageContent. However, if you only want to see if the bot is mentioned, adding GatewayIntentBits.GuildMessages to your intents array will be enough.
I recommend you use RegExp, it would be done like this:
client.on("messageCreate", (message) => {
if (message.author.bot) return;
if (message.content.includes("#here") || message.content.includes("#everyone") || message.type == "REPLY") return;
if (message.content.match(new RegExp(`^<#!?${client.user.id}>( |)$`))) {
message.channel.send("Hello there!");
}
});
The RegExp object is used for matching text with a pattern. So if it matches what you've typed (in this case the #bot ), send the message
I have tried so many things out, here's my current code
var green = toString(msg.reactions.cache.get('🟩').count);
var red = toString(msg.reactions.cache.get('🟥').count);
msg.reply("Poll has ended which was created by " + msg.author.username)
msg.reply(green + " " + red)
It gives this error
var green = toString(msg.reactions.cache.get('🟩').count);
^
TypeError: Cannot read properties of undefined (reading 'count')
Please help!
I want it to display how many people reacted on 🟩 and 🟥.
I'm not able to do that even after asking Discord.js experts, I can't understand how to get them.
I also want them to be a string, that's why I used The toString() function.
This is for a poll/vote bot I am making.
I react to the messages and still it shows this error.
This is Discord.js v13.
Thanks!
TheThiefingKing
let msg = await message.channel.messages.fetch("974311411314475059")
let reactsSize = msg.reactions.cache.get("✅").count
message.channel.send("✅ Reacts on that message is: " + (+reactsSize))
You need to await message first after that you can fetch reactions.
So i tried making a kick command in dicors.js(creating a bot). I did a little bit of research and I ended up with copying a code from stack overflow. The code was working fine but i wanned to make it so if "x" that has a lower rank tries to kick "y" which has higher rank, the bot would then send a message to "x" that he can't kick a higher rank person. (both x and y are users)
The code that i copied also has this function included and i keep getting this error
let authorHighestRole = msg.member.highestRole.position;
^
TypeError: Cannot read property 'position' of undefined
I searched online: nothing.
I tried some stupid things like adding () to .position
I searched in the Discord.js docs and still nothing.
I came to the conclusion that the code is old.
Here is my code
case 'kick' :
if(msg.channel.type === 'DM') {
msg.channel.send('This command can use only in guide');
return;
};
if(!msg.member.hasPermission('KICK_MEMBERS')) {
msg.channel.send('You have no permissions to do that');
return;
};
let mentionMember = msg.mentions.members.first();
if(!mentionMember) {
msg.channel.send('Please specify the person you want to kick!');
return;
}
if(!mentionMember.kickable) {
msg.channel.send('I can\'t kick this user!');
return
};
//Get the highest role of user for compare
let authorHighestRole = msg.member.highestRole.position;
let mentionHighestRole = mentionMember.highestRole.position;
//If mention user have same or higher role, so show this error msg
if(mentionHighestRole >= authorHighestRole) {
msg.channel.send('You can`t kick members with equal or higher position');
return;
};
mentionMember.kick()
msg.channel.send(`${mentionMember.displayName} was kicked`)
break;
Any help would be appreciated.
member doesn't contain a property called highestRole, I believe the property you're looking for is: member.roles.heighest.
see docs: https://discord.js.org/#/docs/main/stable/class/GuildMemberRoleManager?scrollTo=highest
msg.member.roles.highest.position
I was trying to make a temporary ban command for a discord bot that I am currently developing, and I keep running into an issue (node:15340) UnhandledPromiseRejectionWarning: ReferenceError: ban is not defined and I was trying to see if there is any guide or way to step so it can work, what I was trying to do is make when a command was executed it temporary bans someone and it displays the reason why in discord's audit log, also to send a embed saying who was the user banned by for what, so when someone would execute the command it would look like this - !ban (user) (time) (reason).
let toBan = message.mentions.members.first();
let reason = args.slice(2).join(" ");
let member = message.guild.member(toBan)
if(!args[0]) return message.channel.send('Please mention some to ban, so I can ban that fooly!');
if(!toBan) return message.channel.send(`${args[0]} is not a member of this guild, fooly.`);
if(!reason) return message.channel.send('Please specify a reason for the ban fooly!');
if(!toBan.bannable){
return message.channel.send("This dude has super powers, fooly. I can't ban this person.")
}
if(member) {
toBan.ban({
reason: `${reason}`
})
}
let bantime = args[1];
if(!bantime) return message.reply("Fooly! You need to provide a time!");
await(bantime(ban));
message.reply(`Fooly has been banned for ${ms(ms(bantime))}`);
if(toBan.bannable){
const messageEmbed = new Discord.MessageEmbed()
.setTitle('Fooly was banned')
.addField('Person that was banned:', toBan)
.addField('Banned by:', message.author)
.addField('Reason:', reason)
.setTimestamp(Date.now())
.setColor('#FF0000')
.setFooter('Fooly Discord Bot');
message.channel.send(messageEmbed);
toBan.ban();
}
setTimeout(function(){
message.guild.members.unban(toBan)
message.channel.send(`<#${toBan.id}> has been unbanned (temporary ban is finished).`);
}, ms(bantime));
}
}
So firstly, bantime is a string from the message and not a function, so that would return an error later on.
Secondly, await(bantime(ban)); is giving you the error you specified, and it is exactly as it says. You do not have ban declared anywhere, thus it is unable to know what ban is referring to. You might be looking for
let bantime = args[1];
if(!bantime) return message.reply("Fooly! You need to provide a time!");
message.reply(`Fooly has been banned for ${ms(ms(bantime))}`);
I just removed the await(bantime(ban)) in the snippet above, I don't think there's a purpose for it after looking at your code.
I'm new at JavaScript. I'm making a discord bot but I always get error saying that message.startsWith is not a function the reason is I wan't to make the bot chat respond to be not case insensitive. I'm on discord.js version 12.2.0 and
this is my code:
client.on("message", (message) => {
if (message.author.bot) return;
msg = message.content.toLowerCase();
if (message.startsWith(prefix + "laugh")) {
message.author.send ("```haha lmao```")
}
}
I've been searching my problem on the internet but none of them solve my problem. It's been 3 days since I got this error.
Maybe startsWith is not supported. Check if String.prototype.startsWith is true.
Also you can use indexOf instead.
if (message.indexOf(prefix + "laugh") === 0) {
message.author.send ("```haha lmao```")
}
You have to replace message to message.content if you want to retrieve the full message sent by the user.
The message constructor contains much more than the user's message, see the docs.