Add On to Discord.JS Userinfo post - javascript

Alright, this may look like a copy and paste but it is NOT. I need more support. I'm trying to make my bot do more. I should've added it to the first question but I don't think it'll get viewed. Here's what I'm trying to add.
Say when user joined guild at what time and date
Tag user in userinfo.tag function
List user's nickname
I've tried using the .guildmember class but it just won't work, I'm attaching userMention to the .guildmember class, like this : userMention.guildmember.joinedAt or displayName. Most of my results when modifying my code were either TypeErrors from the bot or ReferenceErrors.
I did use other's code, and I installed Moment, so I could run it with another person's code, but again it gave out an error.
TypeError: Cannot read property 'filter' of undefined
I wasn't able to get the Moment code.
var commando = require('discord.js-commando');
var discord = require('discord.js');
class aboutuser extends commando.Command
{
constructor(client) {
super(client, {
name: 'aboutuser',
group: 'help',
memberName: 'aboutuser',
description: 'Lists information about a specific user.',
aliases: ['au', 'aboutu', 'auser', 'user'],
})
}
async run(message, args){
const userMention = message.mentions.users.first() || message.author;
let userinfo = {};
userinfo.bot = userMention.bot;
userinfo.createdat = userMention.createdAt;
userinfo.joinedat = userMention.message.guildmember.joinedat;
userinfo.discrim = userMention.discriminator;
userinfo.id = userMention.id;
userinfo.tag = userMention.tag;
userinfo.uname = userMention.username;
userinfo.status = userMention.presence.status;
userinfo.play = userMention.presence.game;
userinfo.avatar = userMention.avatarURL;
const rolesOfTheMember = userMention.roles.filter(r => r.name !== '#everyone').map(role => role.name).join(', ')
var myInfo = new discord.RichEmbed()
.setAuthor(userinfo.uname, userinfo.avatar)
.addField("Username",userinfo.uname, true)
.addField("Client Tag",userinfo.tag, true)
.addField("Created At",userinfo.createdat, true)
.addField("Joined at:",userinfo.joinedat, true)
.addField("Discriminator",userinfo.discrim, true)
.addField("Client ID",userinfo.id, true)
.addField("Bot?",userinfo.bot, true)
.addField("Status",userinfo.status, true)
.addField("Playing",userinfo.play, true)
.addField("Roles",rolesOfTheMember, true)
.setColor(0xf0e5da)
.setFooter('s!aboutserver')
.setTitle("About this user...")
.setThumbnail(userinfo.avatar)
message.channel.sendEmbed(myInfo);
}
}
module.exports = aboutuser;
Expect: A bot that lists all shown in the code, plus the bullets.
Actual: A bot that only shows Type and Reference Errors. It's things like
TypeError: Cannot read property 'guildmember' of undefined
TypeError: Cannot read property 'filter' of undefined
ReferenceError: guildmember is not defined
ReferenceError: user is not defined
I'm using these sites for reference
https://discord.js.org/#/docs/main/stable/general/welcome
How to show roles of user discord.js / userinfo command *Specifically this line!
.addField('Joined at:', `${moment.utc(user.joinedAt).format('dddd, MMMM Do YYYY, HH:mm:ss')}`, true)
https://www.youtube.com/watch?v=W2iI32FDYW8

Use
var umen = message.mentions.members.first() || message.member;
var umen2 = message.mentions.users.first() || message.author;
Also had trouble with "Booleans" and those should NOT be === "false" as they are Booleans, not strings. Do === false or === true

Try using userMention = message.mentions.members.first();
I think its what's causing the issue

Related

add a role to a user discord.js

I am trying to make a command that gives a role to a member after I type power. I already have a mute command, and that one works completely fine, But if I copy that code and change the name of the command and the role it has to give, it gives the error:
TypeError: Cannot read property of 'roles' of undefined
my code is:
if(message.content.startsWith("power")) {
let role = message.guild.roles.cache.find(r => === "Role_ID");
let member = message.mentions.members.first();
member.roles.add(role)
}
You can try:
if(message.content.startsWith("power")) {
let role = message.guild.roles.cache.find(r => === "Role_ID");
let member = message.mentions.members.first();
member.addRole(role)
}

Discord bot not giving the username of the target

So I've been trying to make a command for my discord bot in which my bot should write the name of the target (the person I'm mentioning) in an embed, but unfortunately, nothing has been working.
bot.on("message", (msg) => {
if (msg.content === "R!kill") {
const embed = new Discord.MessageEmbed();
const yoyoyo = msg.author.username;
const target = msg.mentions.members.first();
embed.setTitle(`**OKAY, LES KILL ${target}**`);
embed.setImage("https://i.redd.it/ijh28d8tw0d11.jpg"), embed.setColor("RANDOM"), embed.setFooter(`mission complete`), msg.channel.send(embed);
}
});
I have even tried changing const target = msg.mentions.members.first(); to const target = msg.mentions.members.first.username(); but it's still not working.
If you want to mention someone in the message along with the command, the content of the message (Message.content) won't be equal to only R!kill.
It will be something like R!kill <#!524243315319898133>. With the number being the id of the user you mentioned.
You more likely want to check if the content .startsWith() the command. (As Tyler2P said in their comment.)
And to get a username of the user, you can use GuildMember.user, which has property username. In your case, target is an instance of GuildMember.
if (msg.content.startsWith("R!kill")) {
const embed = new Discord.MessageEmbed();
const target = msg.mentions.members.first();
// Add check if no one is mentioned, we return from the function
if (!target) {
msg.channel.send("Who should I kill?");
return;
}
embed.setTitle(`**OKAY, LES KILL ${target.user.username}**`);
embed.setImage("https://i.redd.it/ijh28d8tw0d11.jpg"), embed.setColor("RANDOM"), embed.setFooter(`mission complete`), msg.channel.send(embed);
}
msg.mentions.members.first(); returns a <GuildMember> object, from which you can access its #displayName property to get its respective guild member name. So transform that into: msg.mentions.members.first().displayName.
The main problem is that you put only target, when you should obtain first the user, and then the username, as the following: target.user.username
bot.on("message", (msg) => {
if (msg.content === "R!kill") {
if (!target) return; // If no mention return.
const embed = new Discord.MessageEmbed();
const yoyoyo = msg.author.username;
const target = msg.mentions.members.first();
embed.setTitle(`**OKAY, LES KILL ${target.user.username}**`);
embed.setImage("https://i.redd.it/ijh28d8tw0d11.jpg"), embed.setColor("RANDOM"), embed.setFooter(`mission complete`), msg.channel.send(embed);
}
});

Why are users id being undefined for discord.js

Ok so I've been working with someone on a discord leveling system, and we are SO close to done, but when we make the leaderboard it constantly pops up with the error, "cannot read property of tag of undefined". We want to be able to use peoples discord names, rather than just their id, that way it's easier to know who is who on the leaderboard. We have tried many different things to replace this, but just can't seem to figure it out. I know that the issue is some users ids being undefined, thus the bot is unable to find their tag, im just unsure why some users id are being undefined in the first place. If you know a solution please let me know, it would be greatly appreciated.
const XP = require('quick.xp');
const xp = new XP.SQLiteManager({
deleteMessage: false
})
client.on('message', (message) => {
const level = xp.getLevel(message, message.author.id)
const userxp = xp.getXP(message, message.author.id)
if (message.author.bot) {
return;}
else {
xp.giveXP(message);
}
if (message.content === "?rank") message.channel.send(`You are on level ${level} and have ${userxp} XP`)
if (message.content === "?leaderboard") {
let lb = xp.leaderboard(message, message.guild.id, {limit: 10, raw: false});
const embed = new Discord.MessageEmbed()
.setTitle("Leaderboard")
.setColor("#FFFFFF")
lb.forEach(m => {
embed.addField(`${m.position}. ${client.users.cache.get(m.id).tag}`, `Level: ${xp.getLevel(message, m.id)}\n XP: ${m.xp}`)
})
message.channel.send(embed);
}
if (level === '10') {
member.role.add('788820930331017244')
}
})
Only .tag you try to acces in this code is:
embed.addField(`${m.position}. ${client.users.cache.get(m.id).tag}`, `Level: ${xp.getLevel(message, m.id)}\n XP: ${m.xp}`)
That means there is undefined returned from: client.users.cache.get(m.id)
As you have not provided this function just from the code here i can tell that function returns undefined - maybe if couldnt not find the given m.id in cache?
You should have some protection against such code for example
const userFromCache = client.users.cache.get(m.id);
embed.addField(`${m.position}. ${(userFromCache ? userFromCache.tag : '-- no tag --'}`, `Level: ${xp.getLevel(message, m.id)}\n XP: ${m.xp}`);

Auto role when someone starts streaming

I wanted to set a role for a user if he/she starts streaming and sending a message in #streaming. But I keep getting this error that TypeError: Cannot read 'add' of undefined.
client.on('presenceUpdate', (oldMember, newMember) => {
const guild = newMember.guild;
const streamingRole = guild.roles.cache.find(role => role.id === 'streamer');
if (newMember.user.bot) return;
if (newMember.user.presence.activities.streaming) { // Started playing.
let announcement = `Ebrywan ${newMember} just started streaming`;
let channelID = client.channels.cache.find(channel => channel.name.toLowerCase() === "streaming");
if (channelID) channelID.send(announcement);
newMember.roles.add(streamingRole)
console.log(`${streamingRole.name} added to ${newMember.user.tag}`)
}
});
From what i can see from the documentation newMember is a Presence and not a user you can add a role too. Try:
newMember.member.roles.add(streamingRole)
.member will give you the member and you can get the roles of a member + adding new roles should also be possible
Presence: https://discord.js.org/#/docs/main/stable/class/Presence
Member: https://discord.js.org/#/docs/main/stable/class/GuildMember

Trying to fetch a member based on their nickname/displayname. Discord.js BOT

Hi am i trying to take a user specified nickname for a member and fetch that members and get additional information about that user. Right now i have confirmed the ARGS[0] sent by the user is correct but i am getting a NULL response to the matched user. Not sure what i am missing. Thanks
This is my current code. Just trying to get the match working right now. I also need to consider if the person doesnt have a nickname to check the username. Or think would displayname property be better. Thanks
if(command === "memberinfo") {
let sentNickname = args[0];
message.channel.send(`Sent Nickname: ${sentNickname}`);
const discordserver = client.guilds.get(DragonTS); // Define server to get information from
discordserver.fetchMembers() // Fetch guild members
.then() //.then(console.log)
.catch(console.error);
}
let matchedMember = discordserver.members.find(m => m.nickname === sentNickname);
message.channel.send(`Matched Member ${matchedMember}`);
Looks like some parts of your code aren't executed. You need to put all your code into the .then():
if(command === "memberinfo") {
let sentNickname = args[0];
message.channel.send(`Sent Nickname: ${sentNickname}`);
const discordserver = client.guilds.get(DragonTS); // Define server to get information from
discordserver.fetchMembers() // Fetch guild members
.then((serverWithFetchedMembers) => {
let matchedMember = serverWithFetchedMembers.members.find(m => m.nickname === sentNickname);
message.channel.send(`Matched Member ${matchedMember}`);
}) //.then(console.log)
.catch(console.error);
}
It will wait for the fetchMembers() function and execute your code after it!

Categories