discord.js user specific snowflake id commands - javascript

so im trying to get my bot to detect a user id to allow a command so a command for only one user and im having a bit of trouble understanding what i am doing wrong
run(message) {
//member id 184191493919997952
if(message.member.id.find('id', '184191493919997952')){
return message.say('True')
}else{
return message.say('False')
}
}}
so ive tried to set it up to check whoever used the command and if their snowflake id isnt equal to the one listed then it should return false but i keep getting a TypeError am i missing something?

ID is a property of User (which is message.author).
You can directly use "==" to check if it equals to something.
run(message) {
if (message.author.id == "184191493919997952") {
message.reply("true")
} else {
message.reply("false")
};
};
https://discord.js.org/#/docs/main/stable/class/User?scrollTo=id
Also, there is no method called "say" of message.
You can check available methods here (for example, message.reply()):
https://discord.js.org/#/docs/main/stable/class/Message

Related

I cant get an item from an array for my Discord bot to use to take action

I am trying to program a bot for a server, and I am currently working on making a 'Mute' command. My idea was to make it so when the command is called, the bot must first check if the person has the role which allows them to mute other members, if that condition is met, then the bot would take the second argument, aka the Discord id of the member which must be muted, and give them a role which inhibits them from speaking in the server.
For some reason when I was testing the code, I wasn't able to get past the bot checking whether the second argument was a valid ID for a member of the server.
Here is the mute command code:
if (message.member.roles.cache.has('765334017011613717')) {
console.log('Oh oh');
const person = message.guild.member(
message.mentions.users.first() || message.guild.members.get(args[1])
);
if (!person) {
console.log('Oh oh 2');
}
} else message.channel.send('You do not have permission to use this command.');
Note: The console.log() functions were added so I could see where the problem was happening when running the code.
There are two things that you might do in order to make this work with the first being more of a suggestion.
First, ideally you always want to check if the role exists and return if it doesn't. This prevents you from having to put your entire logic to mute the person into the if statement. You do that like this:
if (!message.member.roles.cache.has('your role ID')) {
return message.channel.send('You do not have permission to use this command.');
}
The actual problem in your code stems from your way of finding the member. The right way to do that is, either checking the first mention of a member or taking a provided ID from your first argument. Your first argument being not args[1] but args[0]. If the ID is being used you need to use the cache property. Source
let muteMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
You should then check if a member has actually been found and return if not.
if (!muteMember) {
return message.channel.send("That member doesn't exist!")
}
Note: You already have this in your code but I wanted to include it for completeness.
After that you can continue with your code to mute someone.

Discord %sticky command

I'm currently trying to create a Discord bot that sends a message when a user with a specified role sends the command %sticky This is a test
I want it to always be the first message in the channel and every time another user types in the channel the bot deletes its last message and posts again. I haven't had any luck online yet even finding a bot that already does this functionality, or where to even start. Here's what I kind of have so far
var lastStickyMessage;
client.on('message', message => {
if (lastStickyMessage != null) {
message.channel.fetchMessage(lastStickyMessage)
.then(retrievedMessage => retrievedMessage.delete());
}
message.reply("This is a Sticky Message").then(sent => {
let lastStickyMessage = sent.id;
}
});
There are several errors with your variable management: One one hand you create a new let with the same name. Since a let is a scoped variable, the lastStickyMessage will have a different value inside the sent callback than it has outside of it, since those are two different variables (read more on this here).
Apart from that you should save the last sent ID in a file or somewhere since the var will be reset once you restart your bot (the built in fs module could help you with that, you can find the documentation here).
One last thing: If you initialize a variable without a value it is not null but undefined. If you only check using == it will still evaluate to true (means null == undefined) but if you compare using ===, it will evaluate to false (null !== undefined). In your case this is not really a problem but this might be good to know for other cases.

Whats the best way to check for the existence of a document which contains a field with a specific value - Firestore?

I want to check if a field nam has a specific value and return a boolen if true(or check by something else - whatever).
Here is my code which actually does check but still returns something (some autogenerated stuff from the server) and if I check for null of course it will never be null.
So, whats the easiest and best way to do this? And is it better to do it server-side? P.S. I want to inform the user that a field with this value was found!
Here is my current code:
var type_of_vendor = db().collection(this.state.vendor_type).where('nam', '==', this.state.vendor_name);
type_of_vendor.get()
.then((user) => {
if (user.exists) {
this.state.error = true;
let errorMessage = 'error message';
this.setState({ errorMessage: errorMessage });
} else {
this._signUpVendorvalidateHandler();
}
});
Your code looks fine to me. The only think I'd change is add .limit(1) to the query, so that you never retrieve more than 1 document, even if there are multiple matches.
Doing this server-side is possible with either Cloud Functions, or on a server you already control.
If you do this on the server, you will only have to return whether the document exists (a boolean), instead of the entire document. So you're saving some bandwidth there.

Is there a way to bind a bot to one specific channel?

I am making a Discord bot, and want to make it only be able to post on 1 specific channel. I can not make it have a lower role than a regular member, or it will not be able to give them roles.
I have tried an if/else statement based on the channel's ID.
case 'start':
if (message.channel === 615842616373805067) {
//somewhat unimportant command
return message.member.addRole('615166824849604619'),
message.channel.send(` ${message.author} has been given the role.)`,
message.delete);
//unimportant command ends here
} else {
console.log('Wrong channel, no role.')
}
I expect the bot to only be able to post in this one channel.
Instead of checking for the message.channel Class, try checking for the id property of that class
if(message.channel.id != "615842616373805067") return console.log('Wrong channel, no role.');
//rest of your code
Make sure to check the official documentation for detailed descriptions of each Class, Method, Property, etc.

Role exclusive commands not working // Discord.js

My code is currently:
if (message.content == ",test") {
if (message.member.roles.find("name", "images")) {
message.channel.send( {
file: "http://www.drodd.com/images14/black11.jpg" // Or replace with FileOptions object
});
}
if (!message.member.roles.find("name", "images")) { // This checks to see if they DONT have it, the "!" inverts the true/false
message.reply('You need the \`images\` role to use this command.')
.then(msg => {
msg.delete(5000)
})
}
message.delete(100); //Supposed to delete message
return; // this returns the code, so the rest doesn't run.
}
If the user has the role 'images' I want the bot to send the image when they say ",test" and I want the user's ",test" message to be deleted after some seconds. However, this doesn't seem to be working.
I have tried to send the image without the role checking and that works.
How can I fix this?
This is because message.member.roles.find("name", "images") checks if the roles EXISTS in the guild. In order to find if someone has a role, you would use message.member.roles.has(myRole). To find the ID of a role dynamically, you would use this:
let myRole = message.guild.roles.find("name", "Moderators").id;.

Categories

Resources