首页 > 解决方案 > Discord.js 在某个点拆分消息

问题描述

我正在制作歌词命令。由于不和谐的限制,我必须将消息分成几部分。我当前的解决方案只是将消息拆分为 2000 个字符。

        for(let i = 0; i < besedilo.length; i += 2000) {

         const lyrics = new RichEmbed() 
        .setTitle(` ${serverQueue.songs[0].title} `)
        .setColor(0xFF1E24)
        .setAuthor('KSoft.Si', 'https://cdn.ksoft.si/images/Logo1024.png', 'https://ksoft.si/')
        .setDescription(`**Besedilo : **
        ${besedilo.substring(i, Math.min(besedilo.length, i + 2000))}
        `)
        .setFooter('Če besedilo ni pravo pač pride tako leto...\nZa izboljšavo pišite predloge na: Anej#0001');
        message.channel.send(lyrics).then(d_msg => { d_msg.delete(240000); });
        }

        } 

我想在诗句的结尾把它分开

这是一节经文的例子,它以 \n\n 结尾。

I wanna be a billionaire so fucking bad\n
Buy all of the things I never had\n
I wanna be on the cover of Forbes magazine\n
Smiling next to Oprah and the Queen\n\n

有什么方法可以将经文末尾的信息分开,并从经文的开头开始下一条信息?

标签: javascriptnode.jsdiscord.js

解决方案


有我的建议,需要测试,但应该有效。

// Raw lyrics text
let originalLyricsText = "Lyrics...";
// This will produce an array of all verses.
let originalLyricsSplitted = originalLyricsText.split('\n\n');

// Intitializing first element with an empty string is important here.
let lyricsTextsToSend = [""];
// This represent the index of "lyricsTextsToSend" array we will fill with verses.
let i = 0;

// For each verses we will fill our messages content.
originalLyricsSplitted.forEach(verse => {
    // Adding "\n\n" at the end of the current verse else there wouldn't be any space between verses in messages. 
    verse += "\n\n";

    // Checking that if we add the verse we don't goes upper the message limit.
    if (lyricsTextsToSend[i].length + verse.length < 2000) {
        // We don't go upper so concatenating the current verse in the current message content.
        lyricsTextsToSend[i] += verse;
    } else {
        // We go upper the limit, we switch to a new message and filling it with the current verse.
        i++;
        lyricsTextsToSend[i] = verse;
    }

    // Looping until we have added all verses.
});

// From here, lyricsTextsToSend will contain each messages strings you will have to send.
// You can now create kind of a queue of embeds to send by pushing Promises in an Array.
let messageQueue = [];
lyricsTextsToSend.forEach(messageString => {
  let lyricsEmbed = new RichEmbed() 
  .setTitle(` ${serverQueue.songs[0].title} `)
  .setColor(0xFF1E24)
  .setAuthor('KSoft.Si', 'https://cdn.ksoft.si/images/Logo1024.png', 'https://ksoft.si/')
  .setDescription(`**Besedilo : **
    messageString`)
  .setFooter('Če besedilo ni pravo pač pride tako leto...\nZa izboljšavo pišite predloge na: Anej#0001');

    messageQueue.push(message.channel.send(lyricsEmbed));
});

// All messages are build and ready to be send in order.
// To do this we will use Promise.all method that will process each Promises in order.
Promise.all(messageQueue).then(messages => {
    // All messages are sent, we now need to add the delete timeout on each messages sent.
    messages.forEach(message => {
        message.delete(240000);
    });
});

// Done!

推荐阅读