(DiscordJS) How do I detect an emoji? - javascript

So I've just started with discord.js and I have been searching and searching but I just couldn't really find how to detect an emoji. Replying with one works for me, if I make it detect something like 'test', it'll reply with the smirk but after putting different parts of stuff I have seen so far together, I came up with about 3 solutions for detecting an emoji with this being the most "logical" to me. None of them worked... If anyone could help me with this please, that'd be awesome!
const smirk = client.emojis.cache.get("760523853583351818");
if (message.content.toLowerCase() === "test") {
message.channel.send(`${smirk}`); //this returns "undefined"
}
if (smirk) {
if (message.content.startsWith("<:smirk:" + smirk.id + ">")) {
message.channel.send("<:smirk:760523853583351818>");
}
}

Unicode emojis (the emojis that come global for everyone instead of being custom for servers) are encoded in text content like any other character ("A", "4", "&" or an emoji). They do not use Discord's special bracket notation (if they did, your provided code should probably work. Haven't tested but it looks right).
You can do what you want for this emoji like this:
client.on("message", message => {
if (message.author.bot) return;
if (message.content.startsWith('😏')) {
message.channel.send('😏');
}
});

Related

How to check if the user has mentioned everyone or here?

I am trying to make a command where the bot replies the ping of the bot with "Hello this is my prefix". But the bot also replies to #everyone and #here. How do I prevent that?
this is my code till now
if(message.mentions.has(client.user)){
message.reply('My prefix is `.`');
}
I assume you have already found a solution by now, but if someone were to come across this looking for something similar here you go:
if(message.mentions.everyone === true){
message.reply('My prefix is `.`');
}
Personally, if I were you, instead of adding this in with another if statement I would just keep it in the same if statement like this:
if(message.mentions.has(client.user) || message.mentions.everyone === true){
message.reply('My prefix is `.`');
}
Assuming you're new to JS, the || basically means "or", so I'm saying "if the mention, mentions the bot OR mentions everyone (#everyone/#here), I want to send my prefix". The || (or) is one of many logical operators in JavaScript, and are very useful, if you find the operators confusing, try reading this: https://www.w3schools.com/js/js_comparisons.asp

DiscordJS: Check if input is in a string

Hello!
I will try to do my best as possible to explain this issue i have.
A bin with some of the code mentioned, with comments:
Codebin
I am currently working on a Discord bot, where there is data stored in a .json file (As string).
Maybe this should be converted to an array instead?
A user writes a command and the bot initializes a sequence where it will go through several options depending on whats found in the string.
I've been struggling with this far too long (atleast 10 hours now & i am severely out of ideas now) on getting a check in for user response, to see if the input he does is in the array.
If i write !color 76561197458345 - it starts the process as seen below:
As you see the Available Options for found Dino (Carnotaurus) is:
[Utah1, Utah2]
Which is correct since its listed as the detailcrest options for Carnotaurus below in my json
[
{
"name": "Acrocanthosaurus",
"detailcrest": "[Utah1, Utah2]",
"underbelly": "[Utah3, Utah4]",
"body1": "[Utah5, Utah6]",
"body2": "[Utah7, Utah8]",
"body3": "[Utah9, Utah10]"
},
{
"name": "Carnotaurus",
"detailcrest": "[Utah1, Utah2]",
"underbelly": "[Utah3, Utah4]",
"body1": "[Utah5, Utah6]",
"body2": "[Utah7, Utah8]",
"body3": "[Utah9, Utah10]"
}
]
What then happens is that the user is gonna give a input based on the options found (This case Utah1, Utah2).
I want the bot to check the response from user in chat, if his answer is existing in the json file.
If respond to the bot in chat with Utah1 - it would proceed to next question (Because it exists).
If respond to the bot in chat with Pizza2 - it would respond (Not found, please select available options)
TL;DR:
I simply need a way to check if user response (word) is existing in the string
If Yes: continue, If No: error
I hope someone can give tips, or atleast push in the right direction on how to procceed with this.
I found a simple, but obviously not the most smart answer to my own solution - however it works as expected.
I've changed my .json strings to simply not include any brackets
"detailcrest": "Utah1, Utah2",
To search through my string i applied a .split() function (to seperate words with ,)
const inputCheck = color.detailcrest.toLowerCase().split(',').includes(detailcrest);
If i then do a if else statement on it, it returns true or false - for the given input, if it exists in the json file.
if (inputCheck === true) {
console.log("found")
} else {
console.log("False")
}
Obviously this wouldn't be the smartest way to proceed in a professional
But in my little bot it should work out with what is expected.
Due to limits, i will accept my own answer as solution in two days.

How do I check if a string does not include the phrase, ";say " after the first instance of, ";say"?

How would I check if a string doesn't include, ";say" after the person has already said, ";say" using discord.js.
I want to make sure the user doesn't break the bot that I am making by using the command ;say ;say ;say or something like that.
Solved, code and notes:
client.on("message", msg => { // when user says something
if (msg.content.toLowerCase().includes(";say ") && ((msg.author.id) !== ("690979386354302977"))) { // if it includes, ";say" and is not being sent by the bot
msg.reply((msg.content).slice(5)) //say everything after the first 5 characters
}
})
This bot is still pretty glitchy right now but I'll fix it sooner or later.
Plain JavaScript is pretty much enough to do the job in a nice and effortless manner.
You simply need to employ RegExp.prototype.test() method that will test whether your input string contains pattern ;say followed by some (or none) characters (.*) and then repeated again ((\1)):
!/(;say).*(\1)/.test(str)
Following is the quick demo:
console.log(!/(;say).*(\1)/.test('')) // true
console.log(!/(;say).*(\1)/.test('When you ;say something ;say')) // false
console.log(!/(;say).*(\1)/.test('When you ;say ;hey')) // true
.as-console-wrapper{min-height:100%;}
This is an XY Problem.
You want your bot to not break when you write something like ;say ;say ;say, right? But why is your bot able to trigger its own commands anyway? The first line of most sane message handlers should be this:
if(msg.author.bot) return;
In other words, if the current message was sent by a bot, stop processing immediately and don't try to run commands. This way your bot and other bots can't trigger your bot's commands and you won't even have to answer this question in the first place.

Eclipse discord bot - problem with sending DMs's

So im creating a discord bot for fun on my server, and one of the things im trying to do is send a DM to the sender of the Msg if the msg contains my name. However, i've tried various approaches, using various commands but nothing seems to work. Ive looked on the internet for the answer but I havent seen anyone using eclipse to make the bot, so their Code doesnt always work for me (Unless im just doing something wrong). Could anyone help me out?
You can use some regex to search for your name being present in the message.
client.on("message", message => {
let text = message;
const containsName = text.search(/name/i);
/* search method returns the index of the first letter of the
string you're searching for if it is found, else returns -1.
Here we are using regex to search for the name, and the 'i' makes
it case insensitive.
*/
if(!(containsName === -1)) message.author.send("whatever you want to DM");
});
Your name goes in place of the name in the regex.
More about regex, here.

Why does this error- and warning-free statement not complete?

I'm making a simple JavaScript for opening a dialog with a hypothetical recruiter. When I run the script it does not complete, but neither throws any errors or warnings. At least (!) it doesn't seem like it completes, but it might be me not thinking straight about how to use Firebug.
Here's the code:
var str = prompt("How are you today?", "I'm quite alright, thank you")
if (str.search(/\+alright\i|\+good\i|\+fine\i|\+alright\i/) != -1) {
alert("I'm very glad to hear that! Me, I'm feeling freaky today!");
} else if (str.search(/\+not/&&(/\+well|\+good/)||(/\+sad|\+down|\+blue/)) != -1) {
if (confirm("I'm sorry to hear that! Perhaps you'd like to get together and talk sometime?")) {
var sadNumber = prompt("What's your phone number? (The one entered is mine, feel free to call me!)", "072-");
if (sadNumber.search(/\D/) != -1) {
alert("Sorry, I think there's something wrong with your number. Try entering it with just numbers please!");
sadNumber = prompt("What's your phone number? (The one entered is mine, feel free to call me!)", "072-");
} else {
alert("That's fine! Let's move on with our job questions, shall we?");
}
} else if (alert("Wow! I didn't expect that answer! Truly interesting"));
}
Tried methods
This is what it looks like in Firebug:
After running, this is where the statement stops for some reason. Pressing continue breaks and steps out of the statement:
Stepping through, the statement continues to run, but skips all the (I conbsider) important parts. As you can see here, the alert is being skipped, and statements continues at the else if line:
My guess it that my regexp search (either the method, pattern, or modifiers) is wrong and that's why the statement breaks. I'm still finding it odd, though, since regexp errors usually throw errors or warnings, but this script returns none.
Anyone knows why this particular script breaks? Anyone having a good methodology for debugging errors that do not throw errors or warnings?
Your regular expressions are wrong.
This one, for instance: /\+alright\i|\+good\i|\+fine\i|\+alright\i/ searches for +alrighti (literally) or +goodi or +finei or +alrighti, because \+ means a literal + and \i means a literal i.
You probably meant /alright|good|fine/i, which searches for alright, good, or fine, case-insensitve. Or perhaps /\b(?:alright|good|fine)\b/i, which does the same but is expecting word boundaries on either side of the word.
You can test out your regex at various sites, including regex101.com.

Categories

Resources