I would like to allow the link to be sent only to channels in certain categories and not to channels in other categories, but it doesn't work.
Here is what I have tried:
if (message.content.includes('discord.gg/'||'discordapp.com/invite/'||'https://'||'http://'))
{
if (message.author.guild.channels.get(c => c.name == "Link" && c.type == "category"))
return;
}
else
message.delete().then(message.reply('U Cant Send Link In Here. Go To Link category'))
You try get message.author.guild. message author its a user so he dont have any guild property. When you try find message.guild.channels or message.member.guild.channels its always will return true, if guild has this channnel. It does not depend on the channel for sending the message. The right way is check channel parrent and if parrent name !== somethink, then delete msg.
And still better replace message.channel.parent.name === 'Link' to message.channel.parent.id === 'CATEGORY ID'
if (message.content.includes('discord.gg/'||'discordapp.com/invite/'||'https://'||'http://')) {
if(message.channel.parent.name === 'Link') return
message.delete().then(msg => message.reply('U Cant Send Link In Here. Go To Link category'))
}
message.author.guild.channels.get(c => c.name == "Link" && c.type == "category")wont work, since it only takes a single key. See here: Map#get
What you would need to do it to use the find function. Since Discord.js uses Collections (basically Map's with additional functions) you have access to additional utility functions. See here: Discord.js#Collections. And via find() you can use functions just fine.
message.author.guild.channels.find(c => c.name == "Link" && c.type == "category")
Related
I'm trying to create an application where you have multiple blogs about lost, found and up for adoption pets. You can filter this blogs by:
'Categories (Lost, found, up for adoption)',
'Animal type (Dog, cat, bird, rabbit, etc)',
'Hair type (no hair, short, medium, long)',
'Gender (Female, male)',
'Eyes type (...)',
'HasCollar(yes, no)'
I've done a working filter but it was on the client side and since I'm going to handle a large amount of data, that solution won't work.
I've also tried to solve this problem using queries provided by firestore but I've come across with the limitation where I couldn't compare more than two categories.
This was the code
if (activeCategory !== 'All' && activeGender === 'All' && activeAnimal === 'All') {
q = query(
collection(db, "blogs"),
where("category", "==", activeCategory),
where("gender", "!=", activeGender),
where("animal", "!=", activeAnimal),
)
onSnapshot(
q,
async (snapshot) => {
let list = []
snapshot.docs.forEach((doc) => {
list.push({id: doc.id, ...doc.data()})
})
lastDoc = await snapshot.docs[snapshot.docs.length - 1]
firstDoc = await snapshot.docs[0]
list.sort((a, b) => (a.timestamp < b.timestamp) ? 1 : -1)
setBlogs(list)
setFilters(list)
setLoading(false)
setActive("home")
setLastDoc(lastDoc)
setFirstDoc(firstDoc)
}, (error) => {
console.log(error)
}
)
} else if{...} }
useEffect(() => {
fetchData()
}, [activeCategory, activeGender, activeAnimal])
So my only option is to use Firebase functions. I've talked to a friend and he told me that I needed to have endpoints like this, for example
https/firebase.adoptar.com/api/filter? activeCategory=adopcion
The thing is, I don't know how to do it or where to search information in order to learn about this. It has an structure resembling node js, right?
So I'm trying to come up with a solution using firebase functions. Can someone help me or tell me where to search so I can learn how to do it? Or how a query should look like in order to multifilter and update the page's info according the filter?
I've read firebase functions documentation but at best it tells you how to do a CRUD operation, nothing about filtering or creating an endpoint like the one above.
I'm trying to get my slowmode command working. Basically when I type >sm 2s it replies with "Please follow this example: ;sm 5" <-- this reply should just send if args are null.
if(args[1] == null) {
return message.channel.send("Please follow this example: ;sm 5")
}if (args[1] !== null) {
message.channel.setRateLimitPerUser(args[0])
message.channel.send(`Slowmode is now ${args[0]}s`)
}}
module.exports.config = {
name: "sm",
aliases: []
}```
In JavaScript and most other languages, you could refer to ! in functions as not.
For example, let's take message.member.hasPermission(). If we add ! at the start, giving us:
if (!message.member.hasPermission('ADMINISTRATOR') return
We're basically telling the client, if the message member does **not** have the administrator permission, return the command.
Now, let's take your if statement, saying if (!args[1] == null) {, you're basically telling the client if not args[1] equals to null do this:, which let's face it makes totally no sense.
When we want to compare a variable with a certain value, we would want to tell the client if args[1] is **not** equal to null, do this. Hence why you should fix you if statement into saying:
if (args[1] !== null) {
Sorry for the pretty long answer, felt like giving a little lesson to someone :p
I'm currently making a Discord Bot for my Discord Server, and what I'm wanting to do is make a User Information Embed, I've got the embed created, but what I'm wanting to do is like if the user has the Premium Role, then in the embed it'll say like "True" or "False" if they don't have the role.
here's my Embed Code:
if (message.content.startsWith(prefix + "info")) {
message.delete(500)
const userinfo = new Discord.RichEmbed()
.setColor('#71ff33')
.setTitle('')
.setURL('')
.setAuthor('User Information', 'http://www.stickpng.com/assets/images/585e4bf3cb11b227491c339a.png')
.setDescription("Hey " + message.author.username + " here's the information you requested.")
.setThumbnail(message.author.avatarURL)
.addBlankField()
.addField('**User:**', message.member.user, true)
.addField('**Rank:**', message.member.highestRole, true)
.addField('**Joined:**', message.member.joinedAt, true)
.addField('**Premium:**', , true) //This is where I'm stuck at.
.setImage()
.addBlankField()
.setTimestamp()
.setFooter('Discord User Information' , message.author.avatarURL);
message.channel.send(userinfo);
}
Right there at the Premium field, I want it to display 'True' or 'False' depending if they got the role or not, if I'm making sense on what I'm trying to explain here.
I'm using Node.Js and Discord.Js
You should be able to use one of the following options
This returns true/false
message.member.roles.some(r => r.name === 'Premium');
These return the role if found. Meaning additional logic is required to determine true or false based on if a role is returned or not.
message.member.roles.get(id);
message.member.roles.find(r => r.name === 'Premium');
message.member.roles.find(r => r.id === id); //the .get(id) is faster
I'm trying to find channels in a category by their names.
I've tried this method, but it didn't work.
category.channels.find(c => c.name == "Name" && c.type == "text");
If you check the CategoryChannel documentation, you can see that the attribute to get the channels of a category is actually children.
Try the following instead :
category.children.find(c => c.name == "Name" && c.type == "text");
Need some help with my code. I started coding my first Discord bot today using Node.JS and the Discord.JS library, and others soon. I am currently using a YouTube video to guide me through Node.JS. Here's my question.
Here is my current code.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('message', (message) => {
if(message.content == 'ping') {
message.reply('pong');
}
if(message.content == 'xd') {
message.reply('smh');
}
});
This code works fine. If you go over to the second message.content, it has 'xd' written inside it. When I write the word xd in my Discord server, the bot will return smh. The problem with this is that the bot will only return smh when the capitalization is exactly like it is in xd, but I want it to work for all capitalization.
Like this.
if(message.content == 'xd', 'xD', 'XD', 'Xd',) {
message.reply('pong');
}
Obviously, the above code doesn't work, but is there any easy way to do this?
Cheers.
Before I answer the question, make sure hide your bot's token when sharing your source code because then people can use your bot to do some harmful stuff if it has the right permissions.
Okay so the answer:
At the very beginning, declare a variable called msg or something that stores message.content.toLowerCase();. So no matter how the person types it, it will always be lower case.
Be aware that it will only work if message.content is "xd" only so if a person type "xD something", this wouldn't work.
Additional stuff to your code:
If you want to check if a person used a certain prefix to signify a command, you can check if message.content.startsWith("prefix"); - this will return true or false.
It's always good to break down the content of the message into separate variables allowing you to easily do what you wanna do with the message, for example, a command and args variable (convert message.content into an array first).
You can call .toLowerCase() on message.content to transform it to all lower case:
if (message.content.toLowerCase() == 'xd') {
message.reply('pong');
}
This way the message's casing is practically ignored.
You need to lowercase the message.content using method toLowerCase() method then compare it with "xd string using === operator
ex:
if(message.content && message.content.toLowerCase() === 'xd') {
message.reply("smh");
}
There are 2 other main ways to allow for multiple parameters besides the toLowerCase() function.
The first way would be to use the || operator or the or operator. This method allows for you to check for completely different words while being able to have the same outcome. For example:
if (message.content == "xd" || message.content == "xD" || message.content == "XD") {
message.reply("Why would you do that");
}
The second way would be to use regular expressions like regexes. Regexes check for words inside strings by using the .test() method. For example:
let myRegex = /xd/i;
if (myRegex.test(message.content)) { //We are assuming that message.content = "XD"
message.reply("smh");
}
This method works because the regex checks whether the word is equal to the regex it is assigned to. The i after the /xd/ means that it doesn't care about the casing so this is a shorter way to check for certain words without caring about letter case.
use toLowerCase on the content and then test it
if(message.content.toLowerCase() === 'xd') {
message.reply('pong');
}
A better way todo this is have aliases...
//defined early on.
let commands = [
{
aliases:["xd","lol",":smile:"],
run:function(msg){
msg.send(`pong`);
}
},
{
aliases:[":("],
run:function(msg){
msg.send(`why the long face?`);
}
}
]
//later
client.on(`message`,function(msg){
for(let i =0;i<commands;i++){
if(commands[i].includes(msg.content.toLowerCase())){
commands[i].run();
}
}
});
string.toLowerCase as answered above doesn't always work when you want multiple things to run the same command.
The code above allows aliases for every commands
you can use something like || to have multiple parameters.
In your case it would look like this
if(message.content == 'xd' || 'Xd' || 'xD' || 'XD') {
message.reply('idk');
}
But in your case (because it is always the two same letters in the same order) you can simply add .toLowercase() and then the message will be treated as if it had only lowercase letters and you won't have to worry.
example:
if(message.content.toLowercase() === 'xd') {
message.reply('idk');
}
However there's also a third option to have multiple parameters, you can simply use a constant (const)
example:
const 1 = [ "xd" , "lmao" , "lmfao" ]
if(message.content == 1) {
message.reply('idk');
}
You can change "1" for info
Have a nice day :)
oh and thanks to the moderator that corrected me and sorry for my mistake.