【问题标题】:How to make bot commands SPACE Insensitive如何使机器人命令空间不敏感
【发布时间】:2020-07-29 16:56:31
【问题描述】:

我想让我的机器人命令空间不敏感。每次我做类似“go rps”的事情时,即使 go rps 是一个命令,未知命令的东西仍然会输出。对于像“查看”这样的命令,未知的命令是有效的。我在下面链接了我的代码!我想出了如何使它不区分大小写但不区分空格。

client.on('message', (message) => {
    if(!message.content.startsWith(PREFIX)) return;
    let validCommands = ["go rps", "go mountains", "go hills", "go cannons", "go left", "go east", "go west", "view", "holler"];
    // Added the .toLowerCase() function vvv to make everything work both ways 
    const args = message.content.toLowerCase().substring(PREFIX.length).slice().split(/ /);;
    const command = args.shift();
    const isValid = validCommands.includes(command); 
    const unknowncommand = new Discord.MessageEmbed()
    .setColor('GREEN')
    .setTitle('SCOTT')
    .setDescription('You\'ve entered an unknown command. \nUse **!help** to see all available commands.')   
    if(!isValid){
        if(message.author.bot) return;
        return message.channel.send(unknowncommand), console.log(`[SCOTT] ${message.author.username} entered an unknown command in "${message.guild.name}".`);
    }
});

这个代码是我几个小时前在网上找到的。唯一有效的。有人可以帮我获得间距的,比如“go rps”应该可以工作。无论如何,请帮助。谢谢。

【问题讨论】:

  • 您需要将命令吐到文件中并使用正则表达式查找命令或命令别名

标签: node.js discord.js


【解决方案1】:

这应该可行:

client.on('message', (message) => {
    if(!message.content.startsWith(PREFIX)) return;
    let validCommands = ["go rps", "go mountains", "go hills", "go cannons", "go left", "go east", "go west", "view", "holler"];
    const command = message.content.toLowerCase().substring(PREFIX.length)
    console.log(command)
    const isValid = validCommands.includes(command); 
    const unknowncommand = new Discord.MessageEmbed()
    .setColor('GREEN')
    .setTitle('SCOTT')
    .setDescription('You\'ve entered an unknown command. \nUse **!help** to see all available commands.')   
    if(!isValid){
        if(message.author.bot) return;
        return message.channel.send(unknowncommand), console.log(`[SCOTT] ${message.author.username} entered an unknown command in "${message.guild.name}".`);
    }
});

【讨论】:

【解决方案2】:

要使文本空间不敏感,您可能希望删除所有空格字符。

也不要忘记将您的白名单更改为不区分空格的字符串

const text = 'some text with spaces and    tabs';
console.log(text.replace(/\s/gm, ''));

【讨论】:

    猜你喜欢
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2021-09-08
    • 2021-03-28
    相关资源
    最近更新 更多