Reputation: 404
So I made a Discord bot, and I've got a lot of commands. A problem that keeps coming up is that for mobile users who have automatic capitalization the bot won't recognize the message. All the tutorials I've found on this subject are all in another version of Discord.js. How do I use .toLowerCase() to make all the commands case insensitive?
Upvotes: 0
Views: 4669
Reputation: 6645
You can use String.prototype.toLowerCase() which returns the calling string value converted to lower case.
For example, if you use String.prototype.toLowerCase() on the following strings it will return:
hello world
--> hello world
hElLo WoRlD
--> hello world
...
-> ...
You use it on message.content, since it is a string, to convert it to lowercase, and then check if the content equals to your command.
Here's an example:
client.on("message", message => {
/**
* Any message like the following will pass the if statement:
* -testcommand
* -TeStCoMMaND
* -TESTCOMMAND
* etc...
*/
if (message.content.toLowerCase() == "-testcommand") {
message.reply(`This is a test command.`);
}
});
Upvotes: 2
Reputation: 5330
'use strict';
/**
* A ping pong bot, whenever you send "ping", it replies "pong".
*/
// Import the discord.js module
const Discord = require('discord.js');
// Create an instance of a Discord client
const client = new Discord.Client();
/**
* The ready event is vital, it means that only _after_ this will your bot start reacting to information
* received from Discord
*/
client.on('ready', () => {
console.log('I am ready!');
});
// Create an event listener for messages
client.on('message', message => {
// If the message is "ping"
if (message.content.toLowerCase().startsWith("ping")) {
// Send "pong" to the same channel
message.channel.send('pong');
}
});
// Log our bot in using the token from https://discordapp.com/developers/applications/me
client.login('your token here');
Relevant line:
if (message.content.toLowerCase().startsWith("ping")) {
Upvotes: 0