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!")
Related
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.
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;
}
Actually I think there is an even better way to get the prices using this npm (you can open it to see better how it works):
https://github.com/jaggedsoft/node-binance-api than using the "coin "variable I am trying to use but I don't know how it could be possible, if you guys could help me to figure out with a better idea or the best way to pull prices with this package using discord it would be awesome, I've been already some days sticked to it :/
What I need:
To be able to replace "ticker.XRPBTC" (code below).
For example if I write ETH it should be changed from ticker.XRPBTC to ticker.ETHBTC
var coin = (message.content.toUpperCase()).slice(2) + "BTC";
binance.prices((error, ticker) => {
console.log("Price of " + coin + ":", ticker.XRPBTC);
});
for that I made the variable coin, I thought I could just write ticker.coin but it does not work...
I have tried this:
As an example:
ticker.XRPBTC - This code works, output: the actual price of the currency.
What I am trying:
var coin = XRPBTC
console.log(ticker.coin) - output: undefined
btw. I am writting console.log to test it in the console but I also have an error if I write:
if (msg.startsWith ("eth")) {
message.reply ("Price of " + coin + ":", ticker.TRXBTC);
}
an explanation of what ticker does:
The ticker has the function to get the last price of each currency, it can be for example ETHBTC, XRPBTC, TRXBTC etc. in this case eth, xrp and trx are the currencies. So in the ticker I just need to write it that way so that I can get the price for that pair.
As you can see in the code I sent you above, I am getting the text exactly how the ticker would "need it to look".
So if I write eth, the variable "coin" is transforming it into ETHBTC
Best!
I think you're looking for property access. You might want to check if the coin exists first, e.g.
var coin = message.content.toUpperCase().slice(2) + "BTC";
console.log(coin); // logs e.g. "ETHBTC"
binance.prices((error, ticker) => {
if (coin in ticker) {
var price = ticker[coin]; // <- property access
message.reply("Price of " + coin + ": " + price);
} else {
// handle error, e.g.
message.reply("Coin " + coin + " doesn't exist");
}
});
I want to mention the user that sent the command. I know my code style is weird, but I like the JavaScript like functions.
So this is what I've tried, it mentions the user using the arguments sent.
So for example, if you did !mention <user_id> it would mention the user. I want to auto-detect this from the user sent.
function pingFunction(arguments, receivedMessage) {
if (arguments > 0) {
receivedMessage.channel.send("<#" + arguments + ">");
} else {
receivedMessage.channel.send("<#" + arguments + ">");
}
}
Thanks in advance!
The way to get id of the person who sent the message is message.author.id, or in your case, receivedMessage.author.id.
From there, you can replace receivedMessage.channel.send("<#" + arguments + ">") receivedMessage.channel.send("<#" + message.author.id + ">");
I'm writing my first Discord bot. I want him to read a users message, and then send the message back, with words separated by emojis. So the effect would be something like this: https://i.stack.imgur.com/ysg4f.png
So this is theoretically working - I say theoretically, because I used completely ridiculous solution just to show what I'm aiming for:
if (message.substring(0, 2) == "m.") {
var args = message.substring(2).trim().split(/ +/g);
var cmd = args.shift().toLowerCase();
switch (cmd) {
// m.clap
case "clap":
bot.sendMessage({
to: channelID,
message:
":clap: " + args[0] + " :clap: " + args[1] + " :clap: " + args[2],
});
break;
}
}
So, obviously, this only works for max. three words sentences, and I need it to work for any length of the sentence.
I tried wrapping the "sendMessage" function in a for loop, iterating from 0 to args.length, but that makes the bot send each word in a separate message, obviously. And it won't let me put a for loop inside of the sendMessage function.
Any help would be greatly appreciated. Thank you!!!
You can use the join() method.
message: ':clap: ' + args.join(':clap: ')