Issue with Discord.js avatarURL - javascript

.avatarURL for discord.js is not displaying any image.
I'm trying to make a command for my discord bot that lets someone ping another and have the bot display their profile picture. I quickly homed in on .avatarURL being the solution for this, but it doesn't seem to be displaying any image at all. Any help would be appreciated.
if (message.content.startsWith(`${prefix}showpfp`)) {
let embed = new Discord.RichEmbed().setTitle(tMember.displayName + "'s Profile Picture").setImage(tMember.avatarURL).setColor('#add8e6')
message.channel.send(embed);
}
after typing in command _showpfp #user the bot will respond with
user's Profile Picture
and thats it...

The issue is that tMember doesn't have a property avatarURL. In your comment you say that you get the value of tMember by doing message.mentions.members.first(), however message.mentions.members is a collection of GuildMembers. A GuildMember doesn't have a property avatarURL.
However, the class User does have the property avatarURL. Therefor you need to fetch the member as an instance of User first. Luckily for you, the class GuildMember has a property .user which does just that.
So the fix for your problem is to change the parameter of the setImage to tMember.user.avatarURL, as can be seen below.
if (message.content.startsWith(`${prefix}showpfp`)) {
let embed = new Discord.RichEmbed().setTitle(tMember.displayName + "'s Profile Picture").setImage(tMember.user.avatarURL).setColor('#add8e6')
message.channel.send(embed);
}

Related

I have a problem with the discord bot that gives an automatic roll to every new user

I am trying to create a discord bot that will give an automatic roll to any new user who enters the site and it does not work (I am actually interested in this way that the code should work according to the roll ID because my rollers' names are in a foreign language) I would be happy if someone could help me understand
The code is attached.
client.on('guildMemberAdd', (member) => {
let welcomeRole = member.guild.roles.cache.get("814461419298750475");
if (!welcomeRole) return console.log('Couldn\'t find the member role.');
member.roles.add(welcomeRole);
})
According to the error message that you show in : https://prnt.sc/10dbibn
It clearly stated that your bot doesn't have the permission to do that.
Try giving your bot the "Manage Role" permission and retry to see if that fixes your issue !

Detect message's name of sent image

I'm using a pokecord bot on my discord server, and I also have my own bot in the server. I want to make a feature, that when pokecord sends messageEmbed with pokemon spawn, my bot reacts to it. I tried the message.content.startsWith() method, but because it is messageEmbed, it isn't working. Is there some other way to do that? I was thinking, when you click on the pokemon picture, it opens in a new tab, called PokecordSpawn.jpg, maybe it could be done somehow in this way?
You can check for the image file name in the MessageEmbed like so:
if (message.embeds) {
if (message.embeds[0].image.url.includes('PokecordSpawn.jpg')) {
// Do something
}
}
You can also use message.embeds.forEach() in case the message has multiple embeds

How to make a bot find a channel owner / compare to the author's ID

I'm making a bot where if you do d!move, the bot will move the channel where the message was sent in under a category via ID. I also want to make it so that whoever does the command has permissions such as MANAGE_CHANNELS, which I've already added. The problem is that when I want to confirm whoever created that channel is the person that activated the command, the bot says yes. I did this on an alt account, where I made the channel and my alt was the one initializing it, and the bot said "success!" I also wanted to make it so if someone else made the channel, and when I did it, it would work because I made the bot know my ID.
I've researched Google and found nothing.
I've tried using a function with fetchAuditlog but get anywhere.
if(!message.channel.client.user.id == message.author || !message.author.id == `329023088517971969`) return message.channel.send("You don't own this channel!")
else message.channel.send("success!");
message.channel.setParent(`576976244575305759`);
I expect the bot to be able to check if the author created the channel, and lead to You don't own this channel if they don't own it. But if they do then the bot moves the channel.
The actual result is the bot moving the channel anyway regardless if they own the channel or not.
As #André has pointed out, channel.client represents the client itself, not the user who created the channel. Also, the last line in your code is not part of the else statement, so that's why it's run regardless of the conditions you defined.
To reach a solution, you can make use of the guild's audit logs. You can search for entries where the user is the message author and a channel was created. Then, all you have left is to check if one of those entries is for the current channel, and run the rest of your code if so.
Sample:
message.guild.fetchAuditLogs({
user: message.author,
type: 'CHANNEL_CREATE'
}).then(logs => {
if (!logs.entries.find(e => e.target && e.target.id === message.channel.id)) return message.channel.send('You don\'t own this channel.');
else {
// rest of code
}
}).catch(err => console.error(err));
When you go <anything>.client.user it will return the bot client.
If you want to see who created the channel you would have to check the Audit Logs or save it internally.
I've checked the documents. Here's what it says for .client about the
channel. It says the person that initialized the channel, or the
person that created it.
On documentation I see this:
The client that instantiated the Channel
instantiated is different of initialized

Assigned Roles by Reactions

I have recently been trying to make a Reaction-to-Role bot and have been struggling with some criteria for making it.
I have researched and looked at other peoples' versions of a similar bot on it, yet I have not been able to use it purposefully in my own code.
The goal of the bot is to have multiple options on a message. Here's an example of what it would look like as a message.
**What do you drive?**
🚙 - A car
🚲 - A bicycle
❌ - Nothing
There would be reactions under the message of each emoji that was specified. Once a user clicked on one of these emojis, it would add a role to them such as "Drives a Bike" or "Rides a Bicycle."
I have managed to go as far as making an addrole command (which receives the message the bot will react to, the emoji, and the role it assigns) and stores some info to a JSON file.
The area in which I have been having issues is actually capturing each user that reacts to the specific message, finding out which reaction they sent, and assigning the role assigned to that emoji.
Here is my current code, I'm thank you for any help you may provide :)
addrole.js
if(!args[0] || !args[1] || !args[2] || !args[3]) return message.reply("please use the following format: `autorole #channel messageID icon #role`.");
let channel = message.mentions.channels.first();
let messageID = args[1];
let icon = args[2];
let role = message.mentions.roles.first();
if(!channel.fetchMessage(messageID)) return message.reply("could not find the specified message. Please check the channel and message ID again.");
channel.fetchMessage(messageID).then(msg => {
msg.react(icon);
info[role.name] = {
roleID: role.id,
channelID: channel.id,
icon: icon,
};
fs.writeFile("./configs/info.json", JSON.stringify(info), (err)=>{
if(err) console.log(err);
});
});
I know that creating an event named messageReactionAdd and messageReactionRemove are needed, but I'm not sure how to find their variables and match them accordingly.
Additionally, I am not sure how to make it constantly watch one message even after a restart. From some testing, I noticed that messageReactionAdd would not continue watching the specified messages reactions after a restart.
I am looking for any guidance/help that anyone may provide, thank you!
I've seen on: https://github.com/Sam-DevZ/Discord-RoleReact/blob/master/roleReact.js that there is an event. I've tried this out and it's working.

Checking if my bot has permissions to manage roles

So, the title briefly explains what I want.
When a member joins my Discord server, I want the bot to assign them the User role automatically. Before that happens, I want to verify if the bot has permission to manage roles and so far I have the following in my code.
/** Event will trigger when a user joins the server **/
bot.on("guildMemberAdd", function(member) {
if (!bot.user.hasPermission("MANAGE_ROLES")) {
var embed2 = new discord.RichEmbed()
.setTitle("Error")
.setDescription("The bot doesn't have the appropriate permissions to assign new members roles automatically.\nTo resolve this problem, go to **Server Settings** and then navigate to the **Roles** option. Next, click on **Bots**, and then click on the slider next to **Manage Roles** to activate it.")
.setColor("#3937a5")
bot.channels.get("466878539980079105").send(embed2);
} else {
member.addRole(member.guild.roles.find("name", "User"));
}
});
If the bot doesn't have the permissions, it will get the attention of staff by posting an alert to the #alert channel.
However, I attempt to join with a new Discord account, it reports in the console that hasPermission isn't a function, and then stops.
Would there be any suggestions on how to resolve this code?
On Discord.js we have users and members. Users is a user of Discord. Member is a member of a Discord Server. The user object doesn't have information about roles, nicknames, permissions. Members do, because they are related to that server.
So if you want to grab the bot as the member of the guild you need to use something like this:
<guild>.me
This will return the member object of the bot from the selected guild.
When a member is added to the guild you get the GuildMember object, and with that you also get the guild. So you can use this:
member.guild.me
And finally to check the bot has permissions:
if(member.guild.me.hasPermission("MANAGE_ROLES"))
Note:
hasPermission() and hasPermissions() will be deprecated in DiscordJS v13 and replaced with permissions.has()
e.g : member.permissions.has(Permissions.FLAGS.SEND_MESSAGES);
the error message is correct, user has no function called hasPermission. but the role property does.
https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission

Categories