【问题标题】:How to get my program to read an input escape sequence如何让我的程序读取输入转义序列
【发布时间】:2020-08-26 07:45:32
【问题描述】:

我正在制作一个不和谐的机器人,其中一个命令允许用户使用他们想要的任何文本发送和嵌入任何频道,但我希望他们也能够在嵌入的正文中开始新的一行。简单地让他们在命令中的消息中键入“\n”并不起作用,机器人将在嵌入中输出 \n 而不是换行。有没有简单的方法可以做到这一点?

嵌入:

const sayEmbed = new Discord.MessageEmbed()
    .setColor('#4d4d4d')
    .setTitle(header.join(' '))
    .setDescription(args.join(' '))

描述字段是当args 数组中有一个“\n”时发生这种情况的地方,它不会创建一个新行,而是简单地发送。

【问题讨论】:

    标签: javascript node.js discord discord.js newline


    【解决方案1】:

    您实际上不需要使用\n,您可以在发送消息时创建一个新行,discord.js 将为您完成所有解析工作。我用我的机器人对此进行了测试:

    【讨论】:

    • 谢谢,稍后再试,并在我确认它对我有用后标记为解决方案
    【解决方案2】:

    所以我做了一种简单的 DIY 之类的东西。 (大声笑,实际上我在看到问题时做了这个,花了大约 5 分钟。)
    它允许您使用运算符|(BackSpace 下方的那个)解析消息。
    我尝试在eval 命令中使用它,所以我确信它有效。

    代码是:

    // Creating array variables so it doesn't return undefined when we try to `.concat()` it.
    let sentences = [];
    let temp = [];
    // Loops every args
    for(l=0;l<args.length;l++) {
      // Adding the args as an array to the `temp` variable.
      temp = temp.concat(argss[l])
      // If we meet `|` which is a sentence separator.
      if (args[l] === "|") {
        // Join the `temp` array, making it a sentence while removing the `|` operator.
        sentences = sentences.concat(temp.join(' ').slice(0, -2));
        // Resetting `temp` to reset the saved sentence and start a new one.
        temp = [];
      }
    }
    

    使用.join(' ') 将不起作用,因为它从数组中返回一个字符串,因此不能使用连接\n
    上述方法可能更有效。他们使用如下命令:

    // Say prefix is `.` and the command is `embed`
    .embed <header> | <content> | <title1> | <sentence1> | <title2> | <sentence2> |
    

    你将分别得到sentences[0]sentences[1]sentences[2]sentences[3]sentences[4]sentences[5]。然后,您可以将其添加到您的嵌入中。
    这也将允许多字符串输入,而不是单个参数。不要忘记最后的|,因为没有它,它会忽略整个最后一句。

    const sayEmbed = new Discord.MessageEmbed()
      .setColor('#4d4d4d')
      .setTitle(sentences[0]) // <header>
      .setDescription(sentences[1]) // <content>
      .addField(sentences[2], sentences[3]) // <title1> <sentence1>
      .addField(sentences[4], sentences[5]) // <title2> <sentence2>
      // The more you add, the more it'll allow, you'll have to set it yourself.
    

    TL;DR:更简单的答案:

    sentences = args.join(" ").split(" | ");
    

    对不起,我经常做事很辛苦。

    【讨论】:

    • 似乎我确实倾向于以艰难的方式做事,@Lioness100 的答案更简单,但我的答案更灵活,任何一个都可以。
    • 您可以只使用.split() method,而不是使用for 循环。 JSfiddle example
    • 好吧,sentences = args.join(" ").split(" | ") 的效果和我猜的一样。我花了大约 10 分钟的时间没有阅读任何内容 xd。
    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多