Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

Discord.js: message.mentions.members.array() works fine untile the message meets the condition for a certain if-statement

$
0
0

I have the following code for a moderation bot in Discord.js.

const Discord = require("discord.js");
const client = new Discord.Client();
const gekickt = new Discord.RichEmbed()
  .setColor("#AE0202")
  .setTitle("Je bent gekickt")

client.on("message", message => {
  const guild = message.guild
  const author = message.member
  const mentions = message.mentions.members.array();
  console.log(mentions)
  console.log(message.content)
  const actualcontent = message.content.replace(/<@.+>/g, "")
  console.log(actualcontent)
  if (message.content.startsWith("!kick")) {
    if (author.hasPermission("KICK_MEMBERS")) {
      if (mentions.length > 0) {
        const reason = actualcontent.replace("!kick", "")
        for (member in mentions) {
          mentions[member].send(gekickt
            .setDescription("uit de " + message.guild.name + " server")
            .addField("Door:", message.author.name)
            .addField("Reden:", reason))
          mentions[member].kick(reason)
        }
      } else {
        message.channel.send("**Error**: no users were mentioned to kick")
      }
    } else {
      message.channel.send("**Error**: you aren't allowed to use the command!")
    }
  }
})

My !kick command requires at least one mention of one or more members to kick. To achieve this I'm using the mentions property of the message which I take the members from and put them in an array. When I send a regular message with a mention, the code works fine. But when I type the !kick command with mentions the const mentions = message.mentions.members.array(); suddenly returns null. Why is this?

I am familiar with other ways to achieve the same result like regex but before I change the code I want to know why this doesn't work may I need it in the future


Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>