【问题标题】:say message Only send me the first word of a phrase, JavaScript - Discord.js说消息只给我一个短语的第一个单词,JavaScript - Discord.js
【发布时间】:2021-04-28 05:10:28
【问题描述】:

这是代码:

const args = msg.content.trim().split(/ +/);
const cmd = args.shift().toLowerCase();

if (cmd === 'dm') {
  (await client.users.fetch(args[0]))
  .send(args[1])
    .catch(() => {
      console.log('Error while sending message.')
    })
}

我想要什么:dm

我做什么:dm test testing test 123 我收到了什么:测试

谁能解释我如何解决这个问题?机器人只发送我信息的第一个字:/ 欢迎任何帮助!

【问题讨论】:

    标签: javascript arrays discord discord.js


    【解决方案1】:

    这应该可以完成工作。

    const args = msg.content.trim().split(" ");
    const cmd = args.shift().toLowerCase();
    
    if (cmd === 'dm') {
      // Clone the args array in case you need it later
      const argsClone = args.slice()
      // Fetch user while removing their id from the list
      const user = await client.users.fetch(argsClone.shift())
      // Send the remaining of the list as a DM.
      user.send(argsClone.join(" "))
        .catch(() => {
          console.log('Error while sending message.')
        })
    }
    

    【讨论】:

    • 很高兴看到我的帮助。您能否检查答案是否有效,如果您喜欢,请点赞?谢谢。
    猜你喜欢
    • 2021-10-18
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多