plugins
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
const conf = require('../conf');
|
||||
const {sleep} = require('../utils');
|
||||
|
||||
@ -11,11 +13,80 @@ const {
|
||||
} = require("@google/generative-ai");
|
||||
|
||||
|
||||
|
||||
class Ai{
|
||||
constructor(prompt){
|
||||
this.prompt = prompt;
|
||||
this.start();
|
||||
constructor(args){
|
||||
this.bot = args.bot;
|
||||
this.promptName = args.promptName;
|
||||
this.prompCustom = args.prompCustom || '';
|
||||
this.intervalLength = args.intervalLength || 30;
|
||||
this.intervalStop;
|
||||
this.messageListener;
|
||||
}
|
||||
|
||||
async init(){
|
||||
await this.start();
|
||||
this.bot.on('onReady', async (argument)=>{
|
||||
try{
|
||||
await sleep(1000);
|
||||
let messages = [''];
|
||||
|
||||
this.messageListener = this.bot.on('message', (message, type)=>{
|
||||
if(type === 'game_info') return;
|
||||
if(message.toString().startsWith('<') && message.toString().split('>')[0].includes(this.bot.bot.entity.username)){
|
||||
console.log('message blocked from message array')
|
||||
return;
|
||||
}
|
||||
console.log(`Message ${type}: ${message.toString()}`)
|
||||
messages.push('>', message.toString());
|
||||
});
|
||||
|
||||
this.intervalStop = setInterval(async ()=>{
|
||||
let result;
|
||||
// if(messages.length ===0) return;
|
||||
|
||||
try{
|
||||
result = await this.chat(JSON.stringify({
|
||||
messages, currentTime:Date.now()+1}
|
||||
));
|
||||
}catch(error){
|
||||
console.log('error AI API', error, result);
|
||||
messages = [];
|
||||
return ;
|
||||
}
|
||||
|
||||
try{
|
||||
messages = [''];
|
||||
if(!result.response.text()) return;
|
||||
|
||||
for(let message of JSON.parse(result.response.text())){
|
||||
console.log('toSay', message.delay, message.text);
|
||||
if(message.text === '___') return;
|
||||
setTimeout(async (message)=>{
|
||||
await this.bot.sayAiSafe(message.text);
|
||||
}, message.delay*1000, message);
|
||||
}
|
||||
}catch(error){
|
||||
console.log('Error in AI message loop', error, result);
|
||||
if(result || result.response || result.response.text()){
|
||||
console.log(result.response.text())
|
||||
}
|
||||
}
|
||||
}, this.intervalLength*1000);
|
||||
|
||||
}catch(error){
|
||||
console.log('error in onReady', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async unload(){
|
||||
if(this.intervalStop){
|
||||
clearInterval(this.intervalStop);
|
||||
this.intervalStop = undefined;
|
||||
}
|
||||
this.messageListener();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__settings(history){
|
||||
@ -63,13 +134,26 @@ class Ai{
|
||||
}
|
||||
}
|
||||
|
||||
start(history){
|
||||
async start(history){
|
||||
const genAI = new GoogleGenerativeAI(conf.ai.key);
|
||||
|
||||
const model = genAI.getGenerativeModel({
|
||||
model: "gemini-1.5-flash",
|
||||
});
|
||||
|
||||
let bulbaItems = await axios.get('https://webstore.bulbastore.uk/api/listings');
|
||||
bulbaItems = bulbaItems.data.listings.map(i=>i.listing_name);
|
||||
|
||||
console.log('AI for prompts', conf.ai.prompts)
|
||||
|
||||
this.prompt = conf.ai.prompts[this.promptName](
|
||||
this.bot.bot.entity.username,
|
||||
conf.ai.interval,
|
||||
Object.values(this.bot.getPlayers()).map(player=>`<[${player.lvl}] ${player.username}>`).join('\n'),
|
||||
bulbaItems,
|
||||
this.prompCustom,
|
||||
);
|
||||
|
||||
this.session = model.startChat({
|
||||
...this.__settings(history),
|
||||
// systemInstruction: this.prompt,
|
||||
@ -91,7 +175,6 @@ class Ai{
|
||||
};
|
||||
await sleep(500);
|
||||
this.session.params.history.pop();
|
||||
console.log('current history', this.session.params.history);
|
||||
this.start(this.session.params.history);
|
||||
return await this.chat(message, retryCount++)
|
||||
}
|
||||
@ -103,5 +186,5 @@ class Ai{
|
||||
|
||||
|
||||
|
||||
module.exports = {Ai};
|
||||
module.exports = Ai;
|
||||
// run();
|
Reference in New Issue
Block a user