discord.js embed profile picture command with mentions - javascript

I'm making a bot in discord.js using the Visual Studio Code app. I'm trying to create a command for profile pictures so when you type -pfp it would show you your profile picture and when you type -pfp #user it would show the person's you mentioned profile picture. (- is the prefix). Though the bot only sends the message without the embed part with the picture. When I mention someone else it does the same thing but mentioning me and not the user.
This is what I have:
if (!message.content.startsWith(prefix) || message.author.bot) return;
if (message.content.startsWith(prefix + 'pfp')) {
message.channel.send('Here is <#'+ message.author.id+ ">'s pfp :)")
const avatarEmbed = new Discord.MessageEmbed()
.setColor('#446580')
.setAuthor('user.username')
.setImage(message.author.displayAvatarURL());
} else if (message.content.startsWith(prefix+ 'pfp'+ message.mentions.users)) {
message.channel.send('Here is <#'+ message.user.id+ ">'s pfp :)")
const avatarEmbed = new Discord.MessageEmbed()
.setColor('#446580')
.setAuthor('user.username')
.setImage(message.user.displayAvatarURL());
}
});

There are two parts here.
First, the bot is only sending the message and not the embed because you only ever send the message. You need a separate line of code to send the embeds.
message.channel.send(avatarEmbed);
Secondly, the bot only ever tags you because of this message.content.startsWith(prefix + 'pfp'). The way you devide between the author and someone else means that it will always match the first case, meaning that the message always starts with prefix + pfp regardless if you tag someone after that.
Now you have a few ways to fix this but I would do it this way.
First you define a new variable, lets call that pfpMember, and you assign that to either the first person you tag or the author of the message.
var pfpMember = message.mentions.members.first() || message.member;
Now that we have a fixed member that is either someone who gets tagged or the author we can just assign the displayAvatarURL function to that member.
.setImage(pfpMember.user.displayAvatarURL());
So your entire command should look a little something like this.
if (message.content.startsWith(prefix + 'pfp')) {
var pfpMember = message.mentions.members.first() || message.member;
// we can just put the member object into the string here, that will tag the person
message.channel.send(`Here is ${pfpMember}'s pfp :)`);
const avatarEmbed = new Discord.MessageEmbed()
.setColor('#446580')
.setAuthor(pfpMemer.user.username)
.setImage(pfpMember.user.displayAvatarURL());
message.channel.send(avatarEmbed);
}

Related

Name shows up as "undefined"

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`)

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)
});

How to track the number of command uses and mentions by the user?

I just wanted to know if there's a way to count how many times a command has been used in my Discord server like on screenshot.
I'm new with coding. I used discord.js 12.1.1
Thank you in advance!
enter image description here
upd.
for example, user1 hugs user2.
User1 hugged N times
User2 has been hugged N times
how can i make count how many times the message author hugs users?
I tried to insert the count into my finished code, but it only takes into account the mentioned user.
Even if i dont mention anyone, the bot thinks i mention myself..
i tried differently, but my brain is already exploding.. 2 days i cant understand my mistakes
i changed let member to different values but it works adequately only all at once.. -_-
dont laugh much, i try(
const Discord = module.require("discord.js");
const fs = require("fs");
const config = require(`../config.json`)
const hugs = require('../db.json');
module.exports.run = async (bot, message, args) => {
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
let msg =
member === message.member
? `${message.author} hugs everyone`
: `${message.author} hugged ${member}`;
let gif = [
"IRUb7GTCaPU8E",
"u9BxQbM5bxvwY",
"3EJsCqoEiq6n6",
];
let selected = gif[Math.floor(Math.random() * gif.length)];
let id = member.id || message.author.id
let hugCount = hugs[id];
if (!hugCount) {
hugs[id] = 1;
let embed = new Discord.MessageEmbed()
.setDescription(`${msg}`)
.setColor("RANDOM")
.setFooter(`${member.user.username} hugged for the first time.`)
.setImage(`https://media.giphy.com/media/${selected}/giphy.gif`);
message.channel.send(embed);
} else {
hugCount = (hugs[id] = hugs[id] + 1);
let embed = new Discord.MessageEmbed()
.setDescription(`**${msg}**`)
.setColor("RANDOM")
.setFooter(`${config.prefix}${module.exports.help.name} | ${member.user.username} has been hugged ${hugCount} times.`)
.setImage(`https://media.giphy.com/media/${selected}/giphy.gif`);
message.channel.send(embed);
return message.delete()
}
fs.writeFileSync(
"./db.json",
JSON.stringify(hugs),
(err) => console.log(err)
);
}
module.exports.help = {
name: "hugg"
};
and if possible, help me in this - how to make that the user write a message (for example, !hug user2 "hello") and the bot display message in an embed
I guess you can just have some database where you can store ID of users and values how many times somebody executed you command.
And every time someone uses command you just increase value for that person ID by one.
Edit #1
I think that this code
hugCount = (hugs[id] = hugs[id] + 1);
can be reduce to this:
hugCount = (hugs[id] + 1);
And the issue I think is here... Basically you have to know who you need to get. Here you are getting so many thing what you cannot be sure what is inside.
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
So I would suggest to change it to different variables... for example let author and let mentionedUser and you need to get both of them in different variables to save both of their counts.
That means you gonna have user1 who types !hug user2 "Hello" you need to store in let author = user1 and for let mentionedUser = user2
And for each user you need to have 2 different count values in database (one for how many times they hugged someone, second for how many times they have been hugged by other person)
I hope this is gonna be helpful.
I use Redis along side Heroku, to track how many times a command has been used,
I tried to insert the count into my finished code, but it only takes into account the mentioned user.
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
Whose id are you trying to retrieve here?
Even if i dont mention anyone, the bot thinks i mention myself..
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
message.member is the reason that even when you dont mention anyone, it will get your id.
how to make that the user write a message (for example, !hug user2 "hello") and the bot display message in an embed
Do you mean the "hello" should be embedded?

Categories

Resources