Home > Software design >  How do I make my discord bot Detect lowercases and uppercases? JavaScript
How do I make my discord bot Detect lowercases and uppercases? JavaScript

Time:08-07

Soo I'm Having Trouble with Discord Bot Commands, Not the Slash (/) One but The Modified One like "m." I don't know how to do it I tried some like var content = message.content.toLowerCase(); but it didn't work I think I put it wrong or something? I don't know please help. Thanks :) Here My Code

require('dotenv').config()
const mySecret = process.env['token']
const express = require("express");
const app = express();
const token = 'My Token'
const PREFIX = 'm.';
//const Discord = require("discord.js");
//const client = new Discord.Client();
var prefix = "m.";
//var content = message.content.toLowerCase();
//var content = message.content.toUpperCase();

app.listen(1, () => {
  console.log("Bot Loaded!");
})

app.listen(3000, () => {
  console.log("Bot Is Running!");
})

app.get("/", (req, res) => {
  res.send("Bot Page > Activated Successfully!");
})

const Discord = require("discord.js")
const client = new Discord.Client({
  intents: ["GUILDS", "GUILD_MESSAGES"]
});

client.on("messageCreate", async message => {
  var content = message.content.toLowerCase();

  if (message.content === "m.Test") {
    message.channel.send("Example")

  }
})
client.login(process.env.token);
client.login(token);
client.on('ready', () => {
  console.log('Bot Online!')
})
client.on('ready', () => {
  client.user.setActivity('Scripts & Codes', {
    type: 'PLAYING'
  })
})

CodePudding user response:

Use String.prototype.toLowerCase().

String.Prototype.toLowerCase() makes the content of a string lowercase. Example:

const str = "String With UpperCaSE leTTErS"

console.log(str.toLowerCase()) // expected output: "string with uppercase letters"

In your case, the messageCreate code should look like this:

client.on("messageCreate", async message => {
    const content = message.content.toLowerCase();

    if (content === "m.test") {
        message.channel.send("Test successful!")
    }
})

Also, you may have noticed that in the conditions, the string is always in lower case. The reason is because if you did something like if(content === "m.TEst2"), it would always return false. This is because content is in all lower case while in the condition, we have m.TEst2 which has uppercase letters and conditions are case sensitive, so look out for that.

CodePudding user response:

You declared content but never used it inside your code.

Note that this won't work as long as your command's name contains upper case characters. You should use lower case.

Change:

if (message.content === "m.Test") {
  message.channel.send("Example")
}

To:

if(content === "m.test") {
  message.channel.send("Example");
}

You may also want to make sure that you have the MESSAGE_CONTENT privillege enabled.

  • Related