diff --git a/src/inc/lib.mjs b/src/inc/lib.mjs index d976ae0..bdbe5ce 100644 --- a/src/inc/lib.mjs +++ b/src/inc/lib.mjs @@ -18,6 +18,10 @@ export default new class { return ~~(Math.random() * (max - 1) + 1); }; + sleep(ms) { + return new Promise(r => setTimeout(r, ms)); + }; + async getPlayerlist(world, clanid) { const res = await (await fetch(`https://${world}.freewar.de/freewar/dump_players.php`)).text(); return res.split("\n").map(p => { diff --git a/src/index.mjs b/src/index.mjs index 8f40c25..7af60df 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -94,32 +94,42 @@ bot.on('message', async e => { } } - conv.push({ - role: 'user', - content: `${e.user.nick} schreibt: ${e.message.trim()}` - }); - await fs.writeFile(`./data/conversations/${constr}.json`, JSON.stringify(conv)); - conversations.set(constr, conv); + let tldr = false; + if(e.message.match(/^!tldr/i)) { + conv.push({ + role: 'user', + content: `${e.user.nick} schreibt: schreibe eine kurze Zusammenfassung der letzten 50 Nachrichten.` + }); + tldr = true; + } - if(!e.message?.match(/ra+i+ne+r/i)) { + if(!tldr) { + conv.push({ + role: 'user', + content: `${e.user.nick} schreibt: ${e.message.trim()}` + }); + await fs.writeFile(`./data/conversations/${constr}.json`, JSON.stringify(conv)); + conversations.set(constr, conv); + } + + if(!e.message?.match(/\bra+i+ne+r\b/i) && +e.self.me.id !== +e.raw?.reply_to_message?.from?.id && !tldr) { if(!config.initiative.includes(constr)) return; - let probability = 20; // default, 5% + let probability = 100; - if(e.message.match(/.*\?$/)) { - probability = 5; // 20% - } + if(e.message.match(/.*\?$/)) + probability = 20; - if(lib.rand(probability) !== 1) { + if(lib.rand(probability) !== 1) return; - } } // Rainer matched - if(last) - return await e.reply(`ich antworte bereits ${last}!`); + if(typeof e.sendChatAction !== 'undefined') + await e.sendChatAction('typing'); + while(last) await lib.sleep(500); const actcon = [{ "role": "system", @@ -142,10 +152,12 @@ bot.on('message', async e => { }); } - actcon.push(...conv, { - role: 'user', - content: `${e.user.nick} schreibt: ${e.message.trim()}` - }); + if(!tldr) { + actcon.push(...conv.slice(-15)); + } + else { + actcon.push(...conv); + } const opts = { method: 'POST', @@ -189,11 +201,18 @@ bot.on('message', async e => { console.log("Rainer: " + res.choices[0].message.content.trim()); try { - if(initials[constr] && initials[constr].langmod === 'oger') - await e.reply(oger(res.choices[0].message.content.trim())); - else - await e.reply(res.choices[0].message.content.trim()); + let orig = res.choices[0].message.content.trim(); + for(let i = 0, cl = orig.length; i < cl; i += 1024) { + const msg = orig.substring(i, i + 1024); + if(initials[constr] && initials[constr].langmod === 'oger') { + await e.reply(oger(msg)); + } + else { + await e.reply(msg); + } + } } catch(err) { + console.error(err); await e.reply('ups, kann ich nicht schreiben, wäre zu viel gewesen.'); } } @@ -204,3 +223,7 @@ bot.on('message', async e => { last = false; return; }); + +bot.on('error', err => { + console.error(err); +});