forked from wmantly/mc-bot-town
		
	AI
This commit is contained in:
		
							
								
								
									
										67
									
								
								nodejs/controller/ai.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								nodejs/controller/ai.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,67 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| const conf = require('../conf'); | ||||
|  | ||||
| const { | ||||
| 	GoogleGenerativeAI, | ||||
| 	HarmCategory, | ||||
| 	HarmBlockThreshold, | ||||
| } = require("@google/generative-ai"); | ||||
|  | ||||
| const genAI = new GoogleGenerativeAI(conf.ai.key); | ||||
|  | ||||
| const model = genAI.getGenerativeModel({ | ||||
| 	model: "gemini-1.5-flash", | ||||
| }); | ||||
|  | ||||
| const generationConfig = { | ||||
| 	temperature: 1, | ||||
| 	topP: 0.95, | ||||
| 	topK: 64, | ||||
| 	maxOutputTokens: 8192, | ||||
| 	responseMimeType: "application/json", | ||||
| }; | ||||
|  | ||||
| async function run(name, interval, players) { | ||||
| 	const chatSession = model.startChat({ | ||||
| 		generationConfig, | ||||
| 		safetySettings:[ | ||||
| 			// See https://ai.google.dev/gemini-api/docs/safety-settings | ||||
| 			{ | ||||
| 				category: HarmCategory.HARM_CATEGORY_HARASSMENT, | ||||
| 				threshold: HarmBlockThreshold.BLOCK_NONE, | ||||
| 			}, | ||||
| 			{ | ||||
| 				category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, | ||||
| 				threshold: HarmBlockThreshold.BLOCK_NONE, | ||||
| 			}, | ||||
| 			{ | ||||
| 				category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, | ||||
| 				threshold: HarmBlockThreshold.BLOCK_NONE, | ||||
| 			}, | ||||
| 			{ | ||||
| 				category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, | ||||
| 				threshold: HarmBlockThreshold.BLOCK_NONE, | ||||
| 			}, | ||||
| 		], | ||||
| 		history: [ | ||||
| 			{ | ||||
| 				role: "user", | ||||
| 				parts: [ | ||||
| 					{text: conf.ai.prompt(name, interval, players)}, | ||||
| 				], | ||||
| 			}, | ||||
| 			{ | ||||
| 				role: "model", | ||||
| 				parts: [ | ||||
| 					{text: "Chat stuff"}, | ||||
| 				], | ||||
| 			} | ||||
| 		], | ||||
| 	}); | ||||
|  | ||||
| 	return chatSession; | ||||
| } | ||||
|  | ||||
| module.exports = {run}; | ||||
| // run(); | ||||
| @ -7,7 +7,9 @@ const {CJbot} = require('../model/minecraft'); | ||||
| const inventoryViewer = require('mineflayer-web-inventory'); | ||||
|  | ||||
| const commands = require('./commands'); | ||||
| const {onJoin} = require('./player_list') | ||||
| const {onJoin} = require('./player_list'); | ||||
| const ai = require('./ai'); | ||||
|  | ||||
|  | ||||
| for(let name in conf.mc.bots){ | ||||
| 	if(CJbot.bots[name]) continue; | ||||
| @ -22,21 +24,72 @@ for(let name in conf.mc.bots){ | ||||
|  | ||||
| 	bot.on('onReady', async function(argument) { | ||||
| 		// inventoryViewer(bot.bot); | ||||
| 		try{ | ||||
| 			// onJoin(bot); | ||||
| 			// await sleep(1000); | ||||
| 			// bot.bot.setControlState('jump', true); | ||||
| 			// setTimeout(()=> bot.bot.setControlState('jump', false), 5000); | ||||
| 			if(bot.hasAi){ | ||||
| 				console.log(`${bot.bot.entity.username} has AI`); | ||||
| 				let messages = []; | ||||
|  | ||||
| 		onJoin(bot); | ||||
| 		await sleep(1000); | ||||
| 		bot.bot.setControlState('jump', true); | ||||
| 		setTimeout(()=> bot.bot.setControlState('jump', false), 5000) | ||||
| 				bot.bot.on('message', (message, type)=>{ | ||||
| 					if(type === 'game_info') return; | ||||
| 					if(message.toString().startsWith('<') && message.toString().split('>')[0].includes(bot.bot.entity.username)) return; | ||||
| 					console.log(`Message ${type}: ${message.toString()}`) | ||||
| 					messages.push('>', message.toString()); | ||||
| 				}); | ||||
|  | ||||
| 	}) | ||||
| 				await sleep(500); | ||||
| 				 | ||||
| 				let aiChat = await ai.run( | ||||
| 					bot.bot.entity.username, | ||||
| 					conf.ai.interval, | ||||
| 					Object.values(bot.getPlayers()).map(player=>`<[${player.lvl}] ${player.username}>`).join('\n') | ||||
| 				); | ||||
| 				 | ||||
| 				setInterval(async ()=>{ | ||||
| 					let result; | ||||
| 					if(messages.length ===0) return; | ||||
| 					 | ||||
| 					try{ | ||||
| 						result = await aiChat.sendMessage(messages); | ||||
| 					}catch(error){ | ||||
| 						console.log('error AI API', error, result); | ||||
| 						messages = []; | ||||
| 						return ; | ||||
| 					} | ||||
|  | ||||
| 	// bot.on('message', function(...args){ | ||||
| 	// 	console.log('message | ', ...args) | ||||
| 	// }) | ||||
| 					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 bot.sayAiSafe(message.text); | ||||
| 							}, message.delay*1000, message); | ||||
| 						} | ||||
| 					}catch(error){ | ||||
| 						console.log('Error in AI message loop', error, result); | ||||
| 					} | ||||
| 				}, conf.ai.interval*1000); | ||||
| 			} | ||||
|  | ||||
| 		}catch(error){ | ||||
| 			console.log('error in onReady', error); | ||||
| 		} | ||||
| 	}); | ||||
|  | ||||
| 	bot.on('error', console.log); | ||||
|  | ||||
| 	// bot.on('message', function(message, type){ | ||||
| 	// 	console.log(`${type}: ${message.toString()}`) | ||||
| 	// }); | ||||
| } | ||||
|  | ||||
| (async ()=>{ | ||||
| try{ | ||||
| (async ()=>{try{ | ||||
| 	for(let name in CJbot.bots){ | ||||
| 		let bot = CJbot.bots[name]; | ||||
| 		if(bot.autoConnect){ | ||||
|  | ||||
		Reference in New Issue
	
	Block a user