Discord.js bot is there a way to check mention? - node.js

Attached below is a part of my code. What it does is that if I type "pain.throw #user" it will throw out an embed and some text. However, if I don't mention a user, my code doesn't work. Is there a way someone can show me to let the bot spit out a message and say "You have to mention someone for this to work" or something. Thank you. Any help is appreciated.

Simply add a
if (!usernamegedUser) return message.channel.send("Try again, except mention someone");
Under the
const usernamegedUser = message.mentions.users.first();

Related

i want my twitch chat bot to respond to a specific user with a specific reply

Im building a twitch chat bot using nodejs and i am bit stuck.
So i want the bot to reply with a specific message only if a specific user write something.
i already set up some replies to basic stuff but i cannot figure out how to do it.
so im guessing i need an if statement .... if x says whatever - return this.
but im not sure how to set it up to reply with the same thing no matter what that specific user does.
ps. its my first time using nodejs or anything coding related.
thank you.
if you want to reply to specific user every time he wrote sth and no matter what (according to your question I think this is what you are looking for):
if (context.username == "YourSpecificUserName") {
client.say(channel, `Responding to ${context.username}: Hello my friend.`)
}
anyway, if you want to reply to specific message from specific user:
if (message == "whateverMsg" && context.username == "YourSpecificUsername") {
...
}

Ways to include custom error messages in shorthand if-else sanity checks Discord JS

I am a beginner and probably, this is a stupid question. I am writing a command handler for a discord.js bot. Every time, a user sends a message starting with the correct command prefix, I check whether the invoke is in an Enmap of possible commands.
Currently, it looks like this:
const command = client.commands.get(invoke);
if(!command) return;
...
I would like to keep this shorthand way of writing those sanity checks, but I would like to inform the user that there is no command with this name.
Is there an elegant way of doing this or does anyone know where I can find more information about good ways to solve this?
Thanks in advance :D
I wouldn't recommend telling the user if a command is invalid, but here's how you can do it:
const command = client.commands.get(command);
if (!command) return message.channel.send("Invalid Command.");

DiscordAPIError: Invalid Form Body when trying to send embeds

This is one of the first commands I wrote whilst I've been learning, and recently it stopped working. I fiddled with it a little, but can't see where the problem comes from. When I run ~userinfo the following error comes up in the console:
Unhandled Rejection at: DiscordAPIError: Invalid Form Body
embed.footer.icon_url: Not a well formed URL.
This is the code for userinfo:
if (command === 'userinfo') {
var embed = new Discord.RichEmbed()
.setTitle('User Info')
.addField('Username', message.author.tag)
.addField('Server', message.guild.name)
.setColor(0xFF8AFF)
.setThumbnail(message.author.avatarURL)
.setFooter('Akasuki', version, client.user.avatarURL);
message.channel.send(embed);
}
Changing message.channel.send() to message.channel.sendEmbed() brings up errors, and there's been nothing wrong with using send() so far.
Also, this isn't necessary but if anyone knows how to add when the users account was created, to this embed it would be very helpful. Or even a resource or couple? Thanks for reading.
Used Cursed's solution (in comments):
"Change .setFooter('Akasuki', version, client.user.avatarURL); to .setFooter(`Akasuki ${version}`, client.user.avatarURL);"
Works perfectly! Thank you so much!

How to check if AXIOS POST request is sent?

I can't seem to figure out why my console.log(); isn't going off. It seems that everything after the await axios.post(); method doesn't go off? Even my handleClearData(); function isn't going off.
I'm using React with Axios for my contact form to send data to a NodeJS server to then email the data to my email.
Could anyone please explain to me or point me in the right direction on solving this problem? Also, the console.log(res); obviously doesn't return or log anything as well.
Thanks a ton!
UPDATED:
Here's more info. I even took out my function thinking it may have been the problem. It wasn't...
SECOND UPDATE:
Another update!
You'll want to put the console log under handleClearData(); within the (first) try block.
I managed to fix it because await method was waiting for a response from the server. So, on my backend I put this simple line of code, res.send() which then allowed the await function to get out of its infinite loop; therefore, executing the rest of the code that came after it.
Thanks to all that tried to help me! =)
&& happy coding!

How to mention the author of a message in another message

I am using repl.it to develop a bot. I am trying to make a command that makes the bot behave like this:
Someone: !slap #someoneelse
Bot: #Someone slapped #someoneelse
How can I get the bot to mention #someone without using ID? Multiple people will use the command and I can't just use ID since it will only work with one person. I haven't found anything that helped me, and the documentation was no help either. Hopefully, I can get help! Thank you.
Users and members have a .toString() method that is automatically called every time they are concatenated with a string: that means that if you type "Hey " + message.author you will get "Hey #author"
That's how I would do the command:
// First store the mentioned user (it will be undefiend if there's none, check that)
let mentionedUser = message.mentions.users.first();
// Reply by directly putting the User objects in the string:
message.channel.send(`${message.author} slapped ${mentionedUser}`);

Resources