Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Discord.js在某个点拆分消息_Javascript_Node.js_Discord.js - Fatal编程技术网

Javascript Discord.js在某个点拆分消息

Javascript Discord.js在某个点拆分消息,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我在做歌词命令。由于不和谐的限制,我不得不把消息分成几个部分。我当前的解决方案只是将消息拆分为2000个字符 for(let i = 0; i < besedilo.length; i += 2000) { const lyrics = new RichEmbed() .setTitle(`I know this has already been answered but I'm going to put my solution in

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

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

         const lyrics = new RichEmbed() 
        .setTitle(`I know this has already been answered but I'm going to put my solution in anyway in case you find it helpful:

const Discord = require('discord.js');
const testbot = new Discord.Client();

const token = 'NzA2NTkyNzgwMTAwNjk4MTYz.XsUZmQ.am52L1QKlP_wNwneyClRUmr_uWA'; //put token here

var msg = 'helloo\nthere\n\n\nmy fine friend' //<-lyrics go there. separate individual lines with \n and verses with \n\n\n.
var foo = '';
var limit = 2000;
var size = 0;
var max = 0;



testbot.on('ready', () =>{
    console.log('bot is online');
})

testbot.on('message', message=>{  //obviously you can add your own event here
    if(message.author.bot) return 0; //you only need this if message is your event
    let verse = msg.split('\n\n'); //this also removes the two \ns from the string so another is required.
    console.log(verse.length);
    for(var i = 0; i<verse.length; i++){
        size = verse[i].length;
        max = limit - size;
        if(foo.length < max){ //do 1400 to ensure that next verse does not break the limit. Change this as you please.
            foo = foo + '\n' + verse[i]; //to put a space between verses.
        }
        else{
            message.channel.send(foo); //send if message is too big
            foo = verse[i];
        }
    }
    message.channel.send(foo);

})

testbot.login(token);
for(设i=0;i.setTitle(`我知道这个问题已经得到了回答,但我还是要把我的解决方案放进去,以防你觉得它有用:

const Discord=require('Discord.js');
const testbot=new Discord.Client();
const token='NzA2NTkyNzgwMTAwNjk4MTYz.XsUZmQ.am52L1QKlP_wnowneyclumr_uWA';//将令牌放在这里
var msg='helloo\n这里\n\n\n我的好朋友'//{
log('bot处于联机状态');
})
testbot.on('message',message=>{//显然,您可以在这里添加自己的事件
if(message.author.bot)返回0;//仅当消息是您的事件时才需要此消息
让verse=msg.split('\n\n');//这也会从字符串中删除这两个\n,因此需要另一个。
控制台日志(诗节长度);

对于(var i=0;i有我的建议,有待测试,但应该有效

//原始歌词文本
let originallyrictext=“歌词…”;
//这将产生一系列的所有诗句。
让originallyricsplited=originalLyricsText.split('\n\n');
//在这里,用空字符串初始化第一个元素很重要。
让lyricsTextsToSend=[“”];
//这表示我们将用韵文填充的“lyricsTextsToSend”数组的索引。
设i=0;
//对于每一节,我们将填入我们的信息内容。
最初的YricsSplitted.forEach(韵文=>{
//在当前诗句末尾添加“\n\n”,否则消息中的诗句之间不会有任何空格。
韵文+=“\n\n”;
//检查如果我们添加了经文,我们不会超过消息限制。
如果(歌词文本发送[i].length+verse.length<2000){
//我们不会上移,所以在当前消息内容中连接当前的韵文。
歌词文本发送[i]+=诗句;
}否则{
//我们达到上限,我们切换到一个新的信息,并用当前的诗句填充它。
i++;
抒情诗;
}
//循环直到我们添加了所有的诗句。
});
//从这里开始,lyricsTextsToSend将包含您必须发送的每个消息字符串。
//现在可以通过在数组中推送承诺来创建要发送的嵌入队列。
让messageQueue=[];
lyricsTextsToSend.forEach(messageString=>{
让lyricsEmbed=new RichEmbed()

.setTitle(`一首诗是否总是以双换行符结尾(
\n\n
)?是的,它总是以双换行符结尾。当它达到极限时,这不只是一个错误吗?而且我不尝试每个嵌入发送一首诗,我尝试发送尽可能少的内容。而且我仍然使用旧版本。你可以这样做'message.channel.send(韵文[0])“然后做”消息。频道。发送(第[1]节)或者你想做的任何事情。而且,不,我不认为这会触发限制。有多首诗,超过2首,所以要发送整个歌词,我会发送很多嵌入。我最多要发送2或3首嵌入。我只是试图在限制处拆分消息,保留嵌入中的最后一首诗,然后在be开始新的嵌入我想我有一个解决方案,让我看看它是否有效编辑了我的答案,我忘了在嵌入描述中加入实际的
messageString
。酷,这和我要说的大致相同