Name shows up as "undefined" - javascript

So, I have been working on a kick command, and I pretty much got the whole thing working, except when I have the bot send a message telling us who was kicked, it just shows up as "undefined was kicked"
client.on("message", message => {
if (message.content.startsWith('grimm!kick')) {
var member = message.mentions.members.first();
if (message.member.hasPermission('KICK_MEMBERS')) {
const kicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.mentions.members.first.username} Was kicked`)
message.channel.send(kicked)
member.kick()
} else {
const notKicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.author.username}! you do not have permission to kick people! you must have the "Kick Members" Permission to use this command`)
message.channel.send(notKicked)
}}});
I have tried "member.username" and that still wont work
Could someone tell me what exactly I'm doing wrong, so I wont make this same mistake again? Thanks!

There are 2 problems in this code.
You are using first as a property, not a method.
the property username doesnt exist on The GuildMember class.
what you are looking for is GuildMember.user.username.
replace
.setDescription(`${message.mentions.members.first.username} Was kicked`)
with
.setDescription(`${message.mentions.members.first().user.username} Was kicked`)

Related

discord.js mention command not picking up users but picks up itself

I've started my bot after a while and for some reason the mention command is not working. Code and response to the command is below:
client.on('message', message => {
if (message.content.startsWith('L!bite')) {
let targetMember = message.mentions.members.first();
if (!targetMember) return message.reply('you need to tag a user in order to bite them!!');
// message goes below!
message.channel.send(`${targetMember.user}, You just got a bitten!`);
message.channel.send({files:["./nom/"]})
//let embed = new Discord.RichEmbed()
//embed.setImage(`https://media1.tenor.com/images/4f5666861516073c1c8015b2af7e2d15/tenor.gif?itemid=14720869`)
message.channel.send(embed);
}
});
Response on Discord: (it does picks itself up as a user but not anyone else)
Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·Today at 1:18 PM
L!bite #Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·
._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._.BOT Today at 1:18 PM
#Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·, you need to tag a user in order to bite them!!
I'm just confused on why it's saying that I'm or other people are not users, but it picks up itself? E.g.:
Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·Today at 1:18 PM
L!bite #._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._.
._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._.BOT Today at 1:18 PM
#._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._., You just got a bitten!
GIF HERE
Try converting the message.mentions.members to array and then filter your bot! -
const mentions = message.mentions.members.array() // will return members array of GuildMember class
const firstMention = mentions.filter(mention=> !mention.user.id === "BOT_ID")[0]
Alternatively, there's already a parameter you can implement in your code where it can automatically check if the message came from a bot. Although, this process may not work if you want to have other bots automatically tag it, but I think that's unlikely.
if (message.author.bot) return; FYI, not sure of which version of discord.js you're using, but the embed naming convention has changed. Instead of RichEmbed(), you should use MessageEmbed().
So the place you would implement this is right after your first if conditional block, like this:
if (message.content.startsWith('L!bite')) {
//the thing that checks if the user who posted was the bot
if (message.author.bot) return;
Hopefully this helps!

making a discord bot say the rules when aksed

hello I am trying to code a Discord bot, I am trying to make it so that if someone says $rules the rules will be sent, I can't send the code because I have a problem with posting something on this website, I have searched a lot on the internet but I can not find anything
edit: I have found out how I can post the code https://glitch.com/edit/#!/melted-messy-leotard
this is the link to see it
You have a syntax error in your code - you never close the listener with any brackets.
To fix this you can use this:
let Discord = require("discord.js");
let client = new Discord.Client();
client.on("message", message => {
if (message.content === "ping") {
message.channel.send("pong!")
}
if(message.content === "embed") {
let embed = new Discord.MessageEmbed()
.setTitle("this is Emded title")
.setDescription("this is Embed description")
.setColor("RANDOM")
.setFooter("This is embed footer")
message.channel.send(embed)
}
if (message.content.startswith("$rules")) {
let member = message.mentions.members.first()
if (!member) message.send("here are the rules, 1. no politics 2. be nice 3. respect others 4. think before you speak 5. no nsfw 6. don't leak personal info 7.don't argue abt opinions, 3 strikes and you're out")
}
});
client.login("token");
Also your line where you do if(!member) probably won't do what you want it to do - this will respond if it DMs the bot, if this is what you wanted then it is fine.
If you want to check if the member is in a guild, you can check if they have a common role (such as everyone) using this:
if (!member.roles.cache.some(role => role.name === '<role name>')) {
// member is not in guild
}

Why does My Bot Keep Crashing (Cannot read property 'send' of undefined)

bot.on(`guildMemberAdd`, (member) => {
const embed = new Discord.MessageEmbed()
.setColor(`#ffffff`)
.setAuthor(member.user.username)
.setDescription(
`Please read the <#762600414183948308> and get Free roles in <#763749111286464562>.`
)
.setTitle(`Welcome to **Fahad Kinq's Club!**`)
.setImage(
`https://media.discordapp.net/attachments/766990205931880480/769939170642362368/6826b0508f5b88a53774c7f574bd18dd.png`
);
member.guild.channels.cache.get(`762601972255162428`).send(embed);
});
Cannot read property 'send' of undefined
All the code is right, and the channel id is correct. What am I doing wrong?!
Like said in the comments, chances are that the channel does not exist, or your ID is invalid. Placing in a return statement is sometimes helpful to make sure that if there is no channel found, it'll stop the code, preventing the bot from crashing. Additionally, the method you are trying to use might be the issue itself. So, with these points in mind, the code is below
bot.on(`guildMemberAdd`, (member) => {
// Define the channel
const welcome = member.guild.channels.cache.find(c => c.id === `762601972255162428`)
// What if it doesn't exist, or isn't found?
if(!welcome) return;
// Create the embed
const embed = new Discord.MessageEmbed()
.setColor(`#ffffff`)
.setAuthor(member.user.username)
.setDescription(
`Please read the <#762600414183948308> and get Free roles in <#763749111286464562>.`
)
.setTitle(`Welcome to **Fahad Kinq's Club!**`)
.setImage(`https://media.discordapp.net/attachments/766990205931880480/769939170642362368/6826b0508f5b88a53774c7f574bd18dd.png`);
// Send the message
welcome.send(embed)
});

Temporary Ban Command Issues

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.

Can't send embed message on join

No idea why it won't work, the exact code worked on my old bot. Code:
client.on("guildMemberAdd", member => {
const Discord = require("discord.js");
const embed = new Discord.RichEmbed()
.setTitle("**Please be sure to read our rules carefully thanks**")
.setAuthor("Welcome to BACKUP")
.setColor(3447003)
.setDescription("Please enjoy your stay")
.setThumbnail(message.author.avatarURL)
client.channels.get('505107608391254030').send({embed});
})
}
The thing that confuses me most, is that if I replace that code with this code, it works fine.
client.on('guildMemberAdd', member => {
member.guild.channels.get('505107608391254030').send("This works, but embed does not, fix it boi, line 102");
});
(On the code that did not work, I tried: client.channels.get, member.channels.get, member.guild.channels.get, client.guild.channels.get
The problem is when you are finding the channel, client.channels.get isn't a method. doesn't work in this situation for reasons I'm not aware of
You have to use client.guilds.get(GUILD_ID).channels.get(CHANNEL_ID).send({embed});

Categories

Resources