else code not working properly discord.js - discord.js

I have tried to make a hug command on discord to hug the mentioned user. I set up a command to check if there has been a user mentioned. If I do not mention a user, it works as intended, but when mentioning a user, it outputs the same thing. Here is my code:
if (message.mentions.users.length > 1) {
const hug = new Discord.MessageEmbed()
.setAuthor(`${message.mentions.users.first().username} got a hug from ${message.author.username}!`, `${message.author.avatarURL({ dynamic:true })}`)
.setImage(hugs[hugged])
.setColor('#ffb9f4')
message.channel.send(hug);
} else {
message.channel.send('Please mention a user to hug!');
};
I don't think anything else should be needed but let me know

Your condition checks if there is more than one user tagged, but you can check if there is any tagged users with
if (message.mentions.users == true)
Second problem is that users is a collection so you have to use .size instead of .length
if (message.mentions.users.size > 0)
Both are good.

If you are doing it in a guild, I'm pretty sure that all mentions are to a GuildMember, try setting your if statement to
if(message.mentions.members.length > 1 || message.mentions.users.length > 1)

Related

Discord.js checking for mention in args

Hey so I am making a discord bot and when I use this to check for mention: message.mentions.members.first(); it will look for mention in whole message user has sent. I am trying to work this out because if user send message ?ban bla bla bla #user it will work. I want to check for mention only in args[0]. Is that possible? I am using discord v12. Thanks!
this is what i found to work...
if(args[0].slice(2).slice(0, -1) !== message.mentions.users.first()?.id) {
return message.reply("Please start with a user...")
}
the args[0].slice(2).slice(0, -1) if a mention... will be the id of the first mention... and if the mention is the first arg, it will also be the first mention. So what I did was took ID of the first mention and compared it to the sliced args[0] to see if they match, else it will return telling them to please start with a user... Make sure to keep the ? in message.mentions.users.first()?.id just in the case of no mention in the message, it will not cause an error to the process and will also return the please start with a user message.
Mentions
USERS_PATTERN uses RegEx for checking if string mentions about a User. If string matches with the pattern it returns true, otherwise returns false.
const { MessageMentions: { USERS_PATTERN } } = require('discord.js');
...
if (!args[0].match(USERS_PATTERN)) return message.channel.send("Please mention a user");
...

How do i make discord.js list all the members in a role?

if(message.content == `${config.prefix}mods`) {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Mods:')
.setDescription(message.guild.roles.cache.get('813803673703809034').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
Hey so iam making a command which displays all the members with that role but it only seems to be sending 1 of the mods
await message.guild.roles.fetch();
let role = message.guild.roles.cache.find(role => role.id == args[0]);
if (!role) return message.channel.send('Role does not exist'); //if role cannot be found by entered ID
let roleMembers = role.members.map(m => m.user.tag).join('\n');
const ListEmbed = new Discord.MessageEmbed()
.setTitle(`Users with \`${role.name}\``)
.setDescription(roleMembers);
message.channel.send(ListEmbed);
};
Make sure your command is async, otherwise it will not run the await message.guild.roles.fetch(); - this fetches all roles in the server to make sure the command works reliably.
In Discord.jsV12 the get method was redacted and find was implemented.
Aswell as this, I would highly recommend defining variables to use in embeds and for searching since it is much more easy to error trap.
If you have many members with the role, you will encounter the maximum character limit for an embeds description. Simply split the arguments between multiple embeds.

Set autorole on specific guilds only

I'd like to auto set roles for new users when they join server X, problem is that this bot is on server Y as well, and server Y doesn't have the role.
client.on('guildMemberAdd', member => {
console.log('User ' + member.user.tag + ' has joined Steampunk.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
})
I was hoping that I could do a simple check before applying the role, if user have joined server X, add the role, if else, do nothing. So far my attempts have failed.
Any input would be appreciated!
The original code is trying add a specific role you put in, and not the role you just looked up a line before that. There shouldn't be any issues with other servers the bot is in, because member.guild gives the guild of the member object, and since this member object is coming from a guildMemberAdd it means that member.guild will only return the guild that was just joined. The code in the edit has a lot of strange things going on, though.
The first issue I see is that in order to look through the channels in a guild, you have to use guild.channels.cache.find(), guild.channels.find() is old now and doesn't work in v12. Same goes for client.guild, guild.roles, etc. The part at the bottom of your edited code doesn't seem to make much sense at all. The code should get the roles of the newly joined guild (member.guild.roles.cache), look through them to find the role you want (you did this correctly with the .find()) and then apply the role to the member. Neither server nor role ever get used in your code, and guild.id is a property, not a method.
Let me know if you need help following these instructions, although it looks like you should be able to do it now. Always look at the discord.js docs first to check properties, methods, etc.
I ended up defining the guild in question by name, saved it as a const, and made a check for the specific guild.
If your bot is on several servers but you only want the greet/addRole on a specific one, all integrated into the message displayed on joining;
//Welcome message and autorole for specific server and channel.
client.on('guildMemberAdd', member => {
const = client.guilds.find(guild => guild.name === "Steampunk");
if (!guild) return;
const channel = member.guild.channels.find(ch => ch.name === "greetings");
if (!channel) return;
let botembed = new Discord.RichEmbed()
.setColor("#ff9900")
.setThumbnail(member.user.avatarURL)
.setDescription(`${member} Welcome to the Ark Steampunk Sponsored Mod Discord!\nPlease report all <#244843742568120321> and if u need any help view or ask your question in <#334492216292540417>. Have any ideas we got a section for that too <#244843792132341761>. Enjoy!:stuck_out_tongue: Appreciate all the support! <http://paypal.me/ispezz>`)
.setTimestamp()
.setFooter("Steampunk Bot", client.user.avatarURL);
channel.send(botembed);
console.log('User ' + member.user.tag + ' has joined ' + member.guild.name + '.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
});

Discord.js how to assign person sending the command to a variable

My question may sound odd I apologize. But im working on a command for my bot that changes your own nickname when you type '>callme newNickName'.
case 'callme':
let nick = args[1];
setNickname(nick); //this is where i am stuck
break;
I'm not sure how to define the person who sent the command so that their own nickname gets changed accordingly.
Be sure that your bot has the appropriate permissions to manage and change nicknames otherwise this will not work.
You should be able to do it like this:
if (message.content.includes('callme')) {
if (!message.guild.me.hasPermission('MANAGE_NICKNAMES')) {
return message.channel.send('Not allowed');
}
message.member.setNickname(message.content.replace('changeNick ', ''));
}
Please see the GuildMember documentation for more detail: https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=setNickname

How can i get mentioned user's username in discord.js

Read the title and will be glad if anyone can help me guys
For an example messages.mentions.first(). Like that, is there anything for mentioned user's username?
The best way would be to use the message.mentions.users.first(), as this will actually give you the first user mentioned in a message. (message.mentions is not a collection). If there are no users mentioned, the first will result in undefined, so you will probably want to check for that. Once you have a user, you will want to access the username property.
Example:
// Assuming we just need a message event
client.on('message', (message) => {
const user = message.mentions.users.first();
if (user === undefined) {
return; // Do not proceed, there is no user.
}
const name = user.username;
// Do stuff with the username
})
Sources:
message.mentions
message.mentions.users
message.mentions.members.first().user.username;
This would give you the username of the first mention.
You could also do message.mentions.users.first().username;

Resources