Get balance of other people - javascript

I'm basically trying to making a balance command (with quick.db) that checks for yourself and for other people as well, suppose -bal
(my balance pop up) but then when I ping someone with -bal #(user) it shows my current balance as well, here's an image:
I've also tried lots of ways but it seems to not work for me :/
Here's the code:
let bal =
db.fetch(`money_${message.guild.id}_${message.author.id}`) ||
db.fetch(`money_${message.guild.id}_${message.mentions.users.first()}`);
if (bal === null) bal = 0;
let user = message.mentions.users.first().username || message.author.username;
const embed = new Discord.MessageEmbed()
.setTitle('[ ' + user + "'s balance ]")
.setDescription(bal);
message.channel.send(embed);

You need to check the order of values when you declare the bal variable. You're basically saying that if there is a message.author then bal's value will be their balance. As there is always a message author, you will never check the mentioned user. So, you need to change the order.
Or, you could also simplify it like I did below. You can create a new user variable and if there is a mentioned user, assign its value, if not, assign the author. Later, as both the mentioned user and the author has an id and a username, you can simply use user.id and user.username.
You can also simplify the if (bal === null) bal = 0 part, by simply using a logical OR (||) after the db.fetch.
let user = message.mentions.users.first() || message.author;
let balance = db.fetch(`money_${message.guild.id}_${user.id}`) || 0;
const embed = new Discord.MessageEmbed()
.setTitle('[ ' + user.username + "'s balance ]")
.setDescription(balance);
message.channel.send(embed);

Related

How to Subtract a different amount from User if certain role is present Discord.JS

Discord.JS bot issue; Hello! I'm looking to subtract a lower amount of points from a user if they have a certain role, the Issue I'm having is that the bot subtracts the regular amount of points and then the role-specific amount, where I just want it to subtract one value or the other.
Here's the code before I tried creating the different values, any help would be appreciated, thank you!
let EmbedError = new Discord.MessageEmbed()
.setColor('#FFFFFF')
.setDescription(`You already own ${message.guild.roles.cache.get('733373020491481219')} !`);
return message.channel.send(EmbedError)
}else if (author < 20000) return message.channel.send(Embed)
db.subtract(`money_${message.guild.id}_${user.id}`, 20000)
db.fetch(`hot_rod_red${message.guild.id}_${user.id}`);
db.set(`hot_rod_red_${message.guild.id}_${user.id}`, true)
let Embed2 = new Discord.MessageEmbed()
.setColor("#FFFFFF")
.setDescription(`:white_check_mark: You bought ${message.guild.roles.cache.get('733373020491481219')} for 20,000 credits!`);
message.guild.members.cache.get(user.id).roles.add("733373020491481219")
db.subtract(`money_${message.guild.id}_${user.id}`, 20000)
db.push(message.author.id, `${message.guild.roles.cache.get('733373020491481219')}`);
message.channel.send(Embed2)```
Try something like
if (message.member.roles.find(r => r.name === "Role Name")) {
//extract less points
} else {
//extract normal amount of points
}

How to get text from individual users

I'm trying to make a notes command for specific users, but my Discord bot makes the notes any note that a user public has said. How do I fix this? I already know the problem with my code, but I don't know how to fix it.
if (command == "Note") {
const notes = db.fetch('userInfo');
while (!args[0]) return message.channel.send("Here are your notes: " + notes);
db.set('userInfo', args.join(' '));
message.channel.send((args.join(' ')) + (" successfully noted!"));
}
You have a row called userInfo, this row gets updated whenever someone executes the command. If you want it to link with a user, you're better of using the id of the user because everyone has a different ID. I suggest using something like:
const notes = db.fetch(`userInfo.${message.author.id}`)
while (!args[0]) return message.channel.send("Here are your notes: " + notes )
db.set('userInfo.${message.author.id}', args.join(' '))
message.channel.send(args.join(' ') + " successfully noted!")

Sometimes bot won't add role and set the nickname

I set up a bot so whenever someone types t!accept #user [nickname] it will add a role to the user and changes the user's nickname. This is my code :
mention = message.mentions.members.first();
if (msg.startsWith (prefix + "accept")){
if(!message.member.hasPermission("MANAGE_ROLES")) return message.reply("You have no permission!");
if (mention == null) { return; }
let args = message.content.split(" ").slice(2);
mention.roles.add('123456789')
mention.setNickname(mention.displayName+" "+args.join(' '))
mention.send("Congratulations");
try{
await message.author.send("Sent.")
}catch(e){
message.channel.send("An error has occured.")
}
}
However, only most of the time it will fully work, sometimes it won't do both things, and sometimes it will only do one of the task (Add role, but not nickname and vice versa)
Is there anything wrong with my code?
Thanks in advance!
It's possible the nickname you're providing at times is too long and exceeds discords nickname length limit of 32 characters. You should add a check of:
if ((mention.displayName + " " + args.join(" ")).length > 32) return message.channel.send("The nickname exceeds 32 characters")
However that could not be it of course, I would've posted a comment beforehand but I unfortunately do not have enough reputation.

How can I split a message and resend the second part into a text channel?

I am trying to split a message when someone uses this tip command and get the second part of the message.
In other words, after someone types &tip <#607942707620610087> 10 satoshi I want to be able to get the 10 satoshi part and put it into the general.send command.
var general = client.channels.find(channel => channel.id === "605819397524946972");
if(message.content.startsWith("&tip <#607942707620610087>")) {
message.author.send("Thanks for the donation!");
general.send("<#" + message.author.id + "> just donated ")
};
var general = client.channels.find(channel => channel.id === "605819397524946972");
if(message.content.startsWith("&tip <#607942707620610087>")) {
let args = message.content.slice("&").split(' ');
message.author.send("Thanks for the donation!");
general.send(`${message.author} just donated ${args[2]`)
};
message.content.slice("&").split(' ')This will slice the prefix and split the message like that ("&tip <#607942707620610087> 10 satoshi" Where tip will be our (args[0]), <#607942707620610087> (args[1]) and finally 10 satoshi will be ours (args[2]))

Discord.js mention players on welcome

a few days ago i made a code to mention users when they join my discord server, this is the code i currently have:
bot.on( "guildMemberAdd", member => {
let textChannel = member.guild.channels.find(channel => channel.id === '569582472812298240');
if (textChannel){
var messages = [
`Brace yourselves. <#${member.user.id}> just joined the server.`,
`Challenger approaching - <#${member.user.id}> has appeared`,
`Welcome <#${member.user.id}>. Leave your weapon by the door.`,
`Big <#${member.user.id}> showed up!`,
`<#${member.user.id}> just joined... or did they?`,
`Ready player <#${member.user.id}>`,
`<#${member.user.id}> hopped into the server. Kangaroo!!`,
`<#${member.user.id}> joined. You must construct additional pylons.`,
`Hello. Is it <#${member.user.id}> you're looking for?`,
`Where's <#${member.user.id}> in the server!`,
`It's dangerous to go alone, take <#${member.user.id}>`
]
textChannel.send({embed: {
color: 3447003,
description: messages[ Math.floor( Math.random() * 11 ) ],
timestamp: new Date(),
}
});
}
});
The problem? Sometimes i get the correct nickname of the person as you can see in this picture, but other times i get just numbers and i can't even click on that 'link'...
Correct way:
They are clickable and i can see their profile.
Incorrect way:
I can't click their id, nothing can be done with it.
Not sure why this happens randomly, any help would be extremely appreciated.
If someone joins the server and then leaves, and you have no other mutual servers with them and are not their friend, it'll just show their user id. This is a client side thing and there isn't really any real fix for mentions. You can however opt to just send their username instead of a mention.
You could try running toString on the member object, as it will automatically generate the right mention (as sometimes it can be prepended with <#!), but the likely reason it still won't work is because your Discord client did not cache the user that just joined.
It can be either because you're in a log channel, the server has too many members or the member left before your Discord client cached the user. You can either try to not use an embed, or (probably the best solution) provide the full username as well as the mention in the embed.
Try this:
bot.on('guildMemberAdd', member => {
var ran = randomRange(0,2);
var greeting;
switch (ran) {
case 0:
greeting = "Brace yourselves. " + member.user + " just joined the server.";
break;
case 1:
greeting = "Challenger approaching - " + member.user + " has appeared";
break;
case 2:
greeting = "Welcome " + member.user + ". Leave your weapon by the door.";
break;
// add more if you want (but dont forget to also change the values in line 2
}
member.guild.channels.get('CHANNEL ID HERE').send(greeting);
});
And add this function:
function randomRange(min, max) { // returns an int >= min and <= max
return Math.floor(Math.random() * (max - min + 1)) + min;
}

Categories