user14173492
user14173492

Reputation:

Discord.js | How to get user's input and make a message for it?

So I'm trying to make a Discord bot with javascript, but I'm trying to make it so it can get the user's input and it has to be a person or like a mention, and make it as a message.

So like if I type !say @Dobie the bot will say @Dobie is a son of a cookie like that, please help me it would be really helpful.

Upvotes: 0

Views: 6149

Answers (1)

Jakye
Jakye

Reputation: 6645

That's pretty simple to achieve. You have to check if there are any mentions within the message (Message.mentions), and if that's the case, you can just send a reply back to the channel.

Here's an example:


client.on('message', (message) => {
 // Making sure that the author of the message is not a bot.
 if (message.author.bot) return false;

 // Checking if the message's content starts with "!say"
 if (message.content.toLowerCase().startsWith('!say')) {
  // Making sure that there is at least one GuildMember mention within the message.
  if (!message.mentions.members.size) return false;

  // Sending the message.
  message.channel.send(
   `${message.mentions.members.first()} is a son of a cookie`
  );
 }
});

Upvotes: 1

Related Questions