提问者:小点点

使discord.js命令不区分大小写


所以我做了一个不和机器人,我有很多命令。 一个不断出现的问题是,对于拥有自动大写功能的移动用户,机器人无法识别消息。 我找到的关于这个主题的所有教程都在discord.js的另一个版本中。 如何使用。ToLowerCase()使所有命令不区分大小写?


共2个答案

匿名用户

您可以使用string.prototype.TolowerCase(),它返回转换为小写的调用字符串值。

例如,如果对以下字符串使用String.Prototype.ToLowerCase(),它将返回:

Hello World-->; Hello World

Hello World-->; Hello World

...->; ...

在message.content上使用它,因为它是一个字符串,所以将它转换为小写,然后检查内容是否等于您的命令。

下面是一个例子:

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.`);
    }
});

匿名用户

'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');

相关行:

if (message.content.toLowerCase().startsWith("ping")) {