Eclipse discord bot - problem with sending DMs's - javascript

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.

Related

How to detect how many pings are in a message DISCORD JS?

How can I detect how many pings are in a message?
I'm not sure where to start. I'm trying to create a anti-raid bot and I want to detect if someone pings 5 people in one message
MessageMentions#user returns a discord.js collection, you can get the length of the collection by simply using message.mentions.users.size, this works with any mentions like
.roles
.members
.channels
and you can just detect using
if(message.mentions.members.size > n) {
message.delete();
}
Simple code that counts unique user mentions in the message.
let count = 0;
for(const user of msg.mentions.users) {
count++;
}
if(count>4) {
msg.delete();
}
You can see how many people were pinged using message.mentions.users Like the following:
Array.from(message.mentions.users).length
It may be 1 over what you need, because Javascript strings start at 0. If so, just add a -1.
Array.from(message.mentions.users).length-1
And please, search this stuff up. I was able to find this on the Discord.js documentation in a matter of seconds.
Me being stupid again, use size attribute on message.mentions.users collection per the Keyed Collection standard.

(DiscordJS) How do I detect an emoji?

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('😏');
}
});

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.

Google Apps Script - Retrieving username

This will hopefully be an easy one for you experienced developers. I'm trying to use the Google Apps Script service to get the username of the logged in user for my Google Site. I can get the email to display fine, however when I try to use the substring method to shorten to the username I get the follow error.
TypeError: Cannot find function substring in object email#email.com. (line 4, file "getusername")
I understand why I'm getting this error but not sure how to fix it. Below is my code
function doGet() {
var userEmail = Session.getEffectiveUser();
var username = userEmail.substring(0, userEmail.indexOf("#"));
var HTMLString= "<body> <h2> Logged in as " + userEmail +",</h2></body>";
HTMLOutput = HtmlService.createHtmlOutput(HTMLString);
return HTMLOutput
}
I'm guessing that because the variable userEmail is dynamic depending on the logged in user, the variable username is unable to see the email address until it has executed ?
Any help would be greatly appreciated.
Complete Javascript newbie here.
Thanks
Jack
Change:
var userEmail = Session.getEffectiveUser();
to:
var userEmail = Session.getEffectiveUser().getEmail();
If you look at the documentation for Session.getEffectiveUser(), you'll see what the "return" is. I never paid much attention to that for a long time. But, you need to always know what is returned. Some return types are: string, or an object, or an integer, or a Class. Depending upon what is returned, that will determine what you can further do. Lots of beginners notice that there is a period in between things, and are able to figure out that they need to patch things together with a period. But they don't really know what that period is for. That period is called a dot operator, and every place there is a "dot operator" is basically another step in a process. You're "chaining" multiple operations together. In the code: Session.getEffectiveUser().getEmail() there are two periods (dot operators) and two separate things happening, each with a different returned value. The getEmail() method returns a string. The documentation states that getEffectiveUser() returns a "User" But if you go to the documentation for "User" you'll see it described as a "Class". To understand what a "Class" is, you'll need to do some research.

DiscordAPIError Invalid Form Body when Trying to Purge Messages

So I was pretty sure that this code worked correctly at one point, but recently it was brought to my attention that it sometimes works but not always (read: It will always purge my messages by n amount, but it might not purge another members).
Because I was teaching myself how to use multiple files on this project, the code is actually split between two files. I'm not sure if that has anything to do with the issue or not. Given the size of the two files, I've uploaded them both to pastebin.
The command code.
The Function code.
It purges just fine when I try and purge n where n is any number within the acceptable range, but when I try and purge n by username I get an error message stating
DiscordAPIError: Invalid Form Body
limit: Value "" is not int.
(note that is two double quotes, the formatting is a little unclear)
I'm confused as to where it is getting a value that is not an int, as I'm running the amount through praseInt. I've snipped out the code segment that checks if a user is defined and posted it below as I believe it to be the problem:
if (user) { // If User is Provided
debug.run(`Filtering messages by ${user.username}`);
const filterBy = user ? user.id : client.user.id;
messages = messages.filter(m => m.author.id === filterBy).array().slice(0, amount);
}
Any assistance would be greatly appreciated. I'm more or less about to scrap the user feature if I can't figure it out.
Also I apologize for the formatting. I can never get the hang of this sites formatting.
Try getting rid of
.array().slice(0, amount).
The array of fetched messages doesn't need to be cut by the amount variable if the message.channel.fetchMessages({ limit: amount }).then((messages) => { line already limits it.

Categories

Resources