So when trying to make my Discord.js bot display an User Avatar, it works with other bots but not with actual users, I have this problem on all commands that use user.avatarURL
This is the code I am using
const user = message.mentions.users.first();
if(message.content.toLowerCase().startsWith(`${prefix}av`)){
let member = message.mentions.members.first();
if(member){
const emb=new Discord.MessageEmbed()
emb.setAuthor(`${user.username}'s avatar`, user.avatarURL())
emb.setImage(user.avatarURL());
message.channel.send(emb)
}
else{
message.channel.send("Sorry none found with that name")
}
You should use displayAvatarURL instead, with the optional dynamic option.
emb.setAuthor(`Text`, user.displayAvatarURL({ dynamic: true }));
https://discord.js.org/#/docs/main/stable/class/User?scrollTo=displayAvatarURL
Related
I tried to make a bot that can kick users and I have a probem with checking if user has a permision to do so. I have aready searched for it trough the whole internet and everywere it was sad to use .has but it doesn't seems to work, console always returns error: Cannot read properties of undefined (reading 'has').
Here's my code:
const { SlashCommandBuilder } = require("#discordjs/builders");
const { Permissions } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName("kick")
.setDescription("Kicks someone")
.addUserOption(option =>
option
.setName("user")
.setDescription("Select the user")
.setRequired(true)),
async execute(interaction){
const user = interaction.options.getUser("user");
const userId = interaction.guild.members.cache.get(user.id);
if(!interaction.user.permissions.has(Permissions.FLAGS.KICK_MEMBERS)) return interaction.reply("You dont have perrmisions to kick KEKW!");
if(!interaction.guild.permissions.me.has(Permissions.FLAGS.KICK_MEMBERS)) return interaction.reply("I dont have perrmisions to kick KEKW!");
if(userId == interaction.user.id) return interaction.reply(
{
content: "You cant kick yourself bro!",
ephemeral: true
});
interaction.reply({
content: `You have succesfuly kicked user: ${user}!`,
ephemeral: true
});
userId.kick();
}
}
Here's the error:
https://ibb.co/86xPdJv
interaction.user is a User object, so methods only available to GuildMember, like permissions.has cannot be applied to the interaction.user. Change it to interaction.member will solve the problem. It's also a good idea to read more about User and GuildMember objects and their differences in the discord.js documentation and guide. I've linked these below:
discord.js documentation pages on topic:
https://discord.js.org/#/docs/discord.js/stable/class/User -User
https://discord.js.org/#/docs/discord.js/stable/class/GuildMember -GuildMember
discord.js guide on topic:
https://discordjs.guide/popular-topics/faq.html#what-is-the-difference-between-a-user-and-a-guildmember
I have a discord bot that I'm attempting to assign a user a role with. From some searching around, I've discovered that the code
let role = message.guild.roles.cache.get("[role_id]")
message.author.roles.add(role)
is supposed to work.
The issue is that message.author.roles doesn't exist. Is there a new way to do this for discord.js v13, or am I missing something?
As per the discord.js docs, Message#author is a User object, which doesn't have the .roles property. What you want is Message#member, which is a GuildMember and has the property you want in order to give and remove roles.
Give this a shot:
let role = message.guild.roles.cache.get("[role_id]")
message.member.roles.add(role)
I use another method and it works as good as this, try it out
let role = message.guild.roles.cache.find((role) => role.name === "[role name here]")
message.member.roles.add(role)
const mention = (await message.mentions.members.first()) || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find((r) => r.user.username.toLowerCase().includes() === args.join(" ").toLocaleLowerCase()) || message.guild.members.cache.find((r) => r.displayName.toLowerCase().includes() === args.join(" ").toLocaleLowerCase()); // get member on mention
const role = message.mentions.roles.first(); // get role on mention
await mention.roles.add(role)
{prefix} {command} {mention a user} {mention a role}
Don't forget, this is in async function
I can see the names, ids and user numbers of the servers where my bot is located, but how can I get the list of users (nicknames)?
You can make use of guild.members.fetch() in order to get all members and then use the nickname property to receive their nicknames. Finally I removed all bots with a simple filter.
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
Working example as a command:
client.on("message", async (message) => {
if (message.author.bot) return;
if (message.content === "!list") {
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
}
});
client.login("your-token");
Thanks to #MrMythical who suggested using displayName only. That property automatically returns the normal username when no nickname has been set for a user.
Users do not have nicknames, only guild members, if you are trying to fetch a list of server member nicknames.
You can use the code snippet from below:
const map = message.guild.members.cache.filter(c=> !c.member.user.bot).map(c=>c.displayName).join('\n');
console.log(map)
The
message.guild.members.cache.filter(c=> !c.member.user.bot)
Filters bots from the list, the
.map(c=>c.displayName).join('\n');
maps the data and only the user nicknames and joins them by paragraph breaks.
If there are any issues, please comment!
I am trying to move the tagged user into the voice channel using the channel ID. I saw someone use .setUserChannel but it tells me that it isn't a function. Can someone tell me what I need to fix please :)
if(command === 'ping'){
message.channel.send('pong');
} else if (command === 'abuse'){
//const AbuseTown = client.channel.cache.get(760535995329282161);
const taggedUser = message.mentions.users.first();
message.channel.send(`You want to abuse: ${taggedUser.username}`);
taggedUser.setVoiceChannel('776202269569712168');
return;
}
First of all, you need the GuildMember object, not the User object. Learn more about the difference here
// change:
const taggedUser = message.mentions.users.first();
// to:
const taggedUser = message.mentions.members.first();
Second of all, GuildMember.setVoiceChannel() is an outdated function, so it won't work if you're using v12.x. Instead, use VoiceState.setChannel().
const taggedUser = message.mentions.members.first();
message.channel.send(`You want to abuse: ${taggedUser.username}`);
taggedUser.voice.setChannel('776202269569712168');
I am trying to make a tempmute command, I followed a tutorial online which worked... But my own server has users with multiple roles, and these roles allow them to talk even when they receive the "muted" role.
Is there any way to save all the roles from a mentioned user and then to remove and add those roles?
I already tried to make a new let variable
let roleHistory = tomute.member.roles;
and then adding and removing them with:
await(tomute.removerole(roleHistory));
tomute.addRole(roleHistory);
But that didn't work
module.exports.run = async (bot, message, args) => {
let tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if(!tomute) return message.reply("Couldn't find user.");
if(tomute.hasPermission("MANAGE_MESSAGES")) return message.reply("Can't mute them!");
let muterole = message.guild.roles.find(`name`, "muted");
if(!muterole){
try{
muterole = await message.guild.createRole({
name: "muted",
color: "#000000",
permissions:[]
})
message.guild.channels.forEach(async (channel, id) => {
await channel.overwritePermissions(muterole, {
SEND_MESSAGES: false,
ADD_REACTIONS: false
});
});
}catch(e){
console.log(e.stack);
}
}
let mutetime = args[1];
if(!mutetime) return message.reply("You didn't specify a time!");
await(tomute.addRole(muterole.id));
message.reply(`<#${tomute.id}> has been muted for ${ms(ms(mutetime))}`);
setTimeout(function(){
tomute.removeRole(muterole.id);
message.channel.send(`<#${tomute.id}> has been unmuted!`);
}, ms(mutetime));
}
I want the bot to take the roles away, tempmute the user and giving the roles back after the Timeout.
Your attempt is on the right track, but you missed a small detail. A Guild Member has a method addRole and removeRole which you used. However, these methods are meant for adding/removing a single role.
When you first fetch the user roles with let roleHistory = tomute.member.roles;, it returns a Collection of roles. If you then attempt to use removeRole(roleHistory) it attempts to remove a single role equal to the complete collection (which doesn't exist obviously).
To make it work, you need the methods addRoles and removeRoles which adds/removes an entire collection. So your code would be:
let roleHistory = tomute.roles;
// Removing all the roles
await(tomute.removeRoles(roleHistory));
// Adding all the roles
tomute.addRoles(roleHistory);
P.s. Since your tomute variable is already a user you need to change your code to fetch the roles from let roleHistory = tomute.member.roles; to let roleHistory = tomute.roles;