【问题标题】:How to get the reason of a ban spaced in the confirmation message如何在确认消息中获取禁令原因
【发布时间】:2021-04-09 11:17:16
【问题描述】:

因此,每当用户被我的 Discord.js 机器人的 ban 命令禁止时,用户提及背后指定的原因不会在确认消息中隔开。我已将禁止原因存储在以下代码中:const banReason = args.slice(1).join(''); 并在消息中将其用作.addFields({ name: 'Reason', value: ``${banReason}`` })。但是每当机器人返回消息时,所有作为禁止原因的文本都不会被隔开。例如,如果我要输入-ban @User No spacing,它会在确认消息中将原因显示为Nospacing。所以问题是你如何划分禁令的原因?

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    在我给你答案之前,我想给你一点关于.join()的“教训”

    .join() 方法用于将数组的元素连接在一起。

    // For example, if we take the following array and use the join method on it, we'll get:
    const fruits = ['apple', 'orange', 'watermelon'];
    fruits.join();
    // Expected Output: 'apple,orange,watermelon'
    // .join() automatically defaults the joining to have ',' between the elements if no argument was provided.
    

    虽然在某些情况下我们显然希望以不同的方式分隔元素,但这看起来会更简洁一些。在这里,我们可以传递一个特定的参数,将元素彼此分开。

    // For example:
    fruits.join('');
    // Expected Output: 'appleorangewatermelon'
    

    从这里开始,由于您的参数基本上是用户给定的元素数组,我们可以使用以下方法简单地将它们与间距连接起来:

    fruits.join(' ');
    // Expected Output: 'apple orange watermelon'
    // from this, we learn that the answer to your problem is: args.slice(1).join(' ');
    

    祝你好运!

    【讨论】:

    • 嗯,我现在肯定明白 .join() 方法了,谢谢你,感谢你的帮助!
    • 没问题!! :p
    【解决方案2】:

    你必须做

    const banReason = args.slice(1).join(' ');
    

    因为你想在单词之间有空格。你的错误只是你什么都不加入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多