javascript - 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
有什么方法可以将经文末尾的信息分开,并从经文的开头开始下一条信息?
解决方案
有我的建议,需要测试,但应该有效。
// 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!
推荐阅读
- javascript - Avada WP 主题/引导模式导致页面在移动设备上滚动到顶部
- c - 嵌套的 strtok_r():连续的标记包含父分隔符
- postgresql - PostgreSQL 驱动程序在每个连接上运行 SQL
- linux - while循环如何等待进程
- python - Python kmeans包安装失败
- token - 使用 web3 / rpc 跟踪以太坊区块链中代币的流通
- excel - 改进 SUMPRODUCT 语句以返回多个条件
- applescript - AppleScript 可以通过本地网络启动 Quicktime 播放吗?
- visual-c++ - 从版本控制接收时缺少 Visual C++ 附加目录
- sql-server - 如果两者都为空,则返回两个日期或特定文本中的较低者