【问题标题】:Can't send a message when the first word of a sentence is specified指定句子的第一个单词时无法发送消息
【发布时间】:2017-10-12 19:45:24
【问题描述】:

我正在尝试制作一个不和谐的机器人。当机器人回答 pong 时,我完成了一个 ping 功能。这是一个正常工作。但现在我正在尝试制作一个音乐机器人,所以命令是 ~play url
我正在使用命令的第一个字以确保它是~play。但是我分析句子的方式显然不正确。

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
  console.log('I am ready!');
});

client.on('message', message => {
  if (message.content === '~ping') {  
    message.reply('pong');
  }  
  var msg = message.content;
  var play = msg.split(" ", 1);
  if (play === '~play') {
    console.log('jioejfaoi');
  }
});


client.login('mytoken');

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    split 函数返回一个数组,即使它只有一个值。

    var play = msg.split(" ", 1)[0]
    

    【讨论】:

    • "~play music".split(" ", 1) 将返回一个数组:["~play"][0] 表示您只需要此数组的第一个元素。
    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2022-01-01
    • 2011-05-05
    • 2014-07-31
    相关资源
    最近更新 更多