有没有一种方法可以按照“如果消息包含,请执行此操作”的方式进行操作

时间:2019-06-28 18:15:58

标签: javascript discord.js

我正在创建Discord机器人,如果有人使用前缀和未知命令,则希望答复。什么是“包含”功能?

switch(message.content){
          case '$status':
              message.reply('Online and ready to go!');
            break;
            case 'ping':
                message.reply('Pong!');
              break;
              case '$help':
                  message.reply('$status - reply \'Online and ready to go!\'');
                  message.reply('$despacito - reply with despacito');
                break;
                case '$despacito':
                    message.reply('https://pbs.twimg.com/media/D-KIbiaWsAAV1-2.jpg');
                  break;
                  default: 
                  console.log('not recognized:' + message.content); 

我想在console.log之后添加If *contains* '$', message.reply('Uh oh, looks like that's not a valid command.')函数。

2 个答案:

答案 0 :(得分:0)

我认为您正在寻找包含-message.content.includes(...)

答案 1 :(得分:0)

当前编写代码的方式,而不是使用vfp screenshot of the error(它会在字符串中的任意位置搜索前缀 ),您可以使用String.includes()来查看消息是否以前缀开头。

// const prefix = '$';

default:
  if (message.content.startsWith(prefix)) {
    return message.channel.send('Unknown command.')
      .catch(console.error);
  }