-3

I want to block my user from using too many capital letters in the same message (i.e typing in full CAPS LOCK). I tried this, but it doesn't seem to work if they put spaces between capital letters; here is my current code:

client.on("message", async (msg) => {
  let sChannel = msg.guild.channels.find((c) => c.name === "guard-log");
  if (msg.channel.type === "dm") return;
  if (msg.author.bot) return;
  if (msg.content.length > 15) {
    if (db.fetch(`capslock_${msg.guild.id}`)) {
      let caps = msg.content.replace(" ", "").toUpperCase();
      if (msg.content == caps) {
        if (!msg.member.hasPermission("ADMINISTRATOR")) {
          if (!msg.mentions.users.first()) {
            msg.delete();
            let embed = new Discord.RichEmbed()
              .setColor(0xffa300)
              .setFooter("Retro Guard", client.user.avatarURL)
              .setTitle("Caps-lock Engel")
              .setDescription("Fazla caps lock kullanımı yakalandı!")
              .addField("Kanal Adı", msg.channel.name, true)
              .addField(
                "Kişi",
                "Kullanıcı: " + msg.author.tag + "\nID: " + msg.author.id,
                true
              )
              .addField("Engellenen mesaj", "```" + msg.content + "```", true)
              .setTimestamp();
            sChannel.send(embed);
            return msg.channel
              .send(`${msg.author} fazla caps kullanıyorsun.`)
              .then((m) => m.delete(5000));
          }
        }
      }
    }
  }
});
Lioness100
  • 8,260
  • 6
  • 18
  • 49
Eray Özden
  • 21
  • 1
  • 7

1 Answers1

0

Use .toUpperCase() on each letter and compare it to the letter capitalized, if it is the same, then it's in capital. Otherwise it's not, you don't actualy need any databases. Try following this code:

client.on("message", async msg => {
  let sChannel = msg.guild.channels.find(c => c.name === "guard-log");

  if (msg.channel.type === "dm" || msg.author.bot || msg.content.length < 15) return;
  // Use `||` (OR) to make it cleaner.

  let non_caps, caps;
  // Create the variables.

  for (x=0;x<msg.content.length;x++) {
    if (msg.content[x].toUpperCase() === msg.content[x]) caps++;
    else non_caps++;
  }
  // `caps` is the amount of capital letters, while `non_caps` is the amount of non-capital letters. This checks for each letter of the message and gets the amount of `caps` and `non_caps`.

  const textCaps = (caps / message.content.length) * 100;
  // Gets a percentage of the capital letters.

  if (textCaps >= 60 && !msg.member.permissions.has('ADMINISTRATOR')) {
  // If the capital letters is over or equals to 60% of the message,
  // and if the user isn't an ADMINISTRATOR, then...

    let embed = new Discord.RichEmbed()
      .setColor(0xffa300)
      .setFooter('Retro Guard', client.user.avatarURL)
      .setTitle("Caps-lock Engel")
      .setDescription("Fazla caps lock kullanımı yakalandı!")
      .addField("Kanal Adı", msg.channel.name, true)
      .addField('Kişi', 'Kullanıcı: '+ msg.author.tag +'\nID: '+ msg.author.id, true)
      .addField('Engellenen mesaj', "```" + msg.content + "```", true)
      .setTimestamp() 
    sChannel.send(embed);
    msg.delete(); // Deletes the capped message.
    return msg.reply(`fazla caps kullanıyorsun.`)
    // `message.reply()` mentions the user that sent the message and is cleaner.
      .then(m => m.delete(5000))
  }
})

Do be careful, since this will delete the message if they put too much caps, including them quoting someone, whether they mean it or not.

If you still don't understand, here are some reference:

Mineko Kayui
  • 761
  • 4
  • 15
  • in this situation i am getting "x is not defined" error. also getting "message is not defined" error in 3th and 10/maybe 11th lines – Eray Özden Sep 10 '20 at 13:41
  • It doesn't seem to be a problem in my code, I was using `msg` as my message constructor not `message`. You might want to change all of your `message` to `msg` or change all the `msg` in my code to `message`, `x is undefined` is because the `message`, which returned `undefinded` as well failed and the code broke. @ErayÖzden. – Mineko Kayui Sep 10 '20 at 13:46
  • but how can i define x – Eray Özden Sep 10 '20 at 13:48
  • You don't need to, but if you're a bit confused, just add `let x = 0;`. Before the `for(){}` was called. – Mineko Kayui Sep 10 '20 at 13:49