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

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

Related

Different user IDs [discord.js]

So I'm working in a discord bot where I want to add different user Ids in a command but now it doesn't work. the code looks like this:
if (message.author.id !== '513773028606476308', '749446086493470751') return message.channel.send("no")
I'm assuming you want a command only to run ONLY for users with specific IDs. Therefore that if statement would be in some kind of function that handles that specific command.. your answer would be to change that if statement to
if (!['513773028606476308', '749446086493470751'].includes(message.author.id)){return message.channel.send("no")}
However, the best solution would be to have an array of these special IDs in the first place like
let allowed=['513773028606476308', '749446086493470751']
if (!allowed.includes(message.author.id)){return message.channel.send("no")}
There's a few things that are contributing to the problem you're seeing here.
The first is that your if statement isn't set up properly. To do what you're looking for, it'd have to look like if (message.author.id !== '513773028606476308' && message.author.id !== '749446086493470751'). While being able to just comma-seperate those values would be a nice feature, it's not currently something you can do in Javascript. You have to explicitly say "if the author ID is not equal to 513773028606476308 and the author ID is not equal to 749446086493470751" etc.
All that said, just continuously adding IDs on that if statement probably isn't a really good idea. Like commenters have pointed out, structuring this a bit differently would probably result in code that's both more readable and easier to change in the future. You'd do this by putting those user IDs in an array and then checking if the array doesn't contain those IDs, like so:
let admins = ['749446086493470751', '749446086493470751'];
if (!admins.includes(message.author.id)) {
return message.channel.send("no");
}
This makes your life a little easier down the line for a few reasons:
If you want to have other commands that only these two users can use, you don't have to copy-paste that big long if block elsewhere, it's fairly simple and easy.
If you do add another command like this, and then later want to add a third special user, you only have to change your code in one place (the admins array), rather than potentially missing a spot.

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.

What javascript command is this?

I remember a command that checks that a given condition is true, otherwise it stops the script. It was one word, starting with a, i think starting with att.. and it was just that word, the condition in parentheses, and the semicolon. I've googled everything I can think of and cannot find it!
if(!condition) return; //for a single assertion statement
Furthermore, you might mean "assert." Google it with javascript for a couple of reads on the subject.
You are looking for a function with the semantics of assert(condition), i.e., throw an exception if condition is false. javascript does not contain a native assert like function, but you could certainly implement it yourself. A bit of searching will yield some good results.
function assert(condition) {
condition || throw "assert failed"
}
You can spruce it up a bit as needed, but that's the basic idea.
I don't know if there's something built in natively to JavaScript but have you tried looking at this?
http://aymanh.com/9-javascript-tips-you-may-not-know#assertion

Is there a better way to write this mutiple or conditional?

I have the following IF statement in javascript:
if ( !(cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'JustifyFull') )
Any suggestions on how it could be written in a cleaner way?
Thanks
if(!cmd.match(/^Justify(Left|Right|Center|Full)$/))
In response to a few comments you can also mimic your strict comparison with a small edit:
if( typeof cmd != 'String' || !cmd.match(/^Justify(Left|Right|Center|Full)$/))
This will react in the exact same way as your current code, ignoring anything that's not a string.
Personally I think it is highly unlikely that you will need it.
This sounds like a good situation to use a switch. Just be aware that switches only do equality checking (==) not identity checking (===), though this should be fine.
switch (cmd) {
case "JustifyLeft" :
case "JustifyRight" :
case "JustifyCenter" :
case "JustifyFull" :
// do something
break;
case "somethingElse" :
default:
// do something else
break;
}
I would create a IsJustifyCommand(s) method or create a command abstract class that has a IsJustifyCommand() method on it. Then the code will read like a description of what it is trying to do.
Using regex may be neat, but will lead to maintenance problems if someone that is not a hard-core JavaScript programmer has to work on the code. However if you have lots of cases when regex is a good solution, then use it, as anyone looking at the code will soon pick it up.
(However I am a C# programmer not a JavaScript programmer, but like most programmer I have to look at / edit JavaScript code sometimes. I think most JavaScript is maintained by none JavaScript programmers.)
I hate when something is written like that. First I look at the code and think "if cmd is equal to JustifyLeft or JustifyRight... then invert that and... if that's true do the thing.. so that means if it's JustifyLeft...". For me it takes alot of time and I have to re-read the line to be sure I got it right.
I think it's better to write.
if ((cmd !== 'JustifyLeft') && (cmd !== 'JustifyRight') && (cmd !== 'JustifyCenter') && (cmd !== 'JustifyFull'))
It might be a little more verbose but I find it easier to follow. I read it as "cmd can't be any of the Justify-strings". Checking a long boolean expression and then inverting the whole answer is irritating.
I like the solution from scragar, just wanted to give my thoughts about inverting a long boolean expression.

Categories

Resources