P
P
Prorokius2019-05-29 22:06:45
JavaScript
Prorokius, 2019-05-29 22:06:45

How to make the bot respond only to a specific person?

I'm trying to make the bot respond only to messages from a certain person.
I know that you can do it by ID, but I can't even redeem it like
var prefix = "^";
client.on("message", msg => {
if(!msg.guild) return;
if(!msg.content.startsWith("^")) return;
const args = msg.content.slice(prefix.length) .trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === "print"){
let text = args.join(" ");
msg. delete().catch(err => console.log(err));
msg.channel.send(text);
}
});
Inserted
if(!msg.author.id === "my_id") return;
Instead
of if(!
But the bot did not care that I set a limit for it and print it from everyone.
Can anyone help how to implement this correctly?
PS Life has not prepared me for this yet, but is it possible to somehow make me write this message to him in PM, and he to the server in a certain channel?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2019-05-29
@Sanasol

So look at what your code is doing, with parentheses, it’s clear in what order the operations are performed: i.e. you don’t have a check, but who knows what, that’s why the bot didn’t care about her. https://learn.javascript.ru/ifelse

G
goblin3564, 2019-06-22
@goblin3564

In my opinion, I did not understand what your code is doing, do you need the bot to check all messages, and respond only if a certain user writes, or do you need the bot to respond to the user when entering a command?
If it's the first option, then it's done like this:

client.on("message", msg => {
  if(msg.author.bot) return;
  if(msg.author.id === "ваш_id") { msg.author.send("Ваш ответ пользователю."); }
 }
});

If it's the second option, then:
client.on("message", msg => {
  if(msg.author.bot) return;
  if(msg.content === "ваша команда/сообщение")
  { if(msg.author.id === "ваш_id") { msg.author.send("Ваш ответ пользователю."); } }
 }
});

If you need something else, describe your problem in more detail.

S
Sergey c0re, 2019-07-20
@erge

Prorokius , Sanasol is hinting at the wrong code, there should be a NOT equality condition, not an equality condition on .... ! msg.author.id
what is it anyway? I suspect that the result ! msg.author.id will always be false (if there is ANY Id)
and false is certainly not equal (And the condition is equal) to any value on the right side of the condition, therefore if does not work!
see Comparison Operators

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question