added say command and comments
This commit is contained in:
parent
1916cb8725
commit
f71899e9c0
@ -12,5 +12,14 @@ module.exports = {
|
|||||||
`${command} -- ${this.commands[command].desc || ''}`
|
`${command} -- ${this.commands[command].desc || ''}`
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
'say': {
|
||||||
|
desc: `Make the bot say stuff in chat`,
|
||||||
|
allowed: ['wmantly', 'useless666', 'tux4242'],
|
||||||
|
ignoreLock: true,
|
||||||
|
async function(from, ...messages){
|
||||||
|
await this.say((messages || []).join(' '));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -35,7 +35,7 @@ let sites = {
|
|||||||
foend: {
|
foend: {
|
||||||
bot: 'ez',
|
bot: 'ez',
|
||||||
desc: `Get an invite to the Farming outpost in the end.`,
|
desc: `Get an invite to the Farming outpost in the end.`,
|
||||||
allowed: ['wmantly', 'useless666', 'tux4242', 'pi_chef', 'pi_chef', '1_cut',],
|
allowed: ['wmantly', 'useless666', 'tux4242', 'pi_chef', '1_cut',],
|
||||||
},
|
},
|
||||||
sb: {
|
sb: {
|
||||||
bot: 'owen',
|
bot: 'owen',
|
||||||
@ -47,11 +47,11 @@ let sites = {
|
|||||||
desc: `Get an invite to the Core.`,
|
desc: `Get an invite to the Core.`,
|
||||||
allowed: ['wmantly', 'useless666', 'tux4242', 'pi_chef', 'pi_chef', '1_cut', 'Ethan63020', 'Ethan63021', 'VinceNL', 'nootbot'],
|
allowed: ['wmantly', 'useless666', 'tux4242', 'pi_chef', 'pi_chef', '1_cut', 'Ethan63020', 'Ethan63021', 'VinceNL', 'nootbot'],
|
||||||
},
|
},
|
||||||
// city: {
|
art: {
|
||||||
// bot: 'art',
|
bot: 'art',
|
||||||
// desc: 'Invite to the city',
|
desc: 'Invite to art',
|
||||||
// allowed: ['wmantly', 'useless666', 'tux4242', 'owenshorts', 'VinceNL', 'Ethan63020', 'Ethan63021']
|
allowed: ['wmantly', 'useless666', 'tux4242']
|
||||||
// },
|
},
|
||||||
german: {
|
german: {
|
||||||
bot: 'linda',
|
bot: 'linda',
|
||||||
desc: `Get an invite you Germans area.`,
|
desc: `Get an invite you Germans area.`,
|
||||||
@ -71,6 +71,7 @@ module.exports = {
|
|||||||
'.invite': {
|
'.invite': {
|
||||||
desc: `The bot will /accept an /invite from you.`,
|
desc: `The bot will /accept an /invite from you.`,
|
||||||
allowed: ['wmantly', 'useless666', 'tux4242', 'owenshorts', 'pi_chef'],
|
allowed: ['wmantly', 'useless666', 'tux4242', 'owenshorts', 'pi_chef'],
|
||||||
|
ignoreLock: true,
|
||||||
async function(from){
|
async function(from){
|
||||||
await this.whisper('Coming');
|
await this.whisper('Coming');
|
||||||
await this.say(`/accept`);
|
await this.say(`/accept`);
|
||||||
@ -78,7 +79,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
'inv': {
|
'inv': {
|
||||||
desc: `Have bot.\n Site -- one'`,
|
desc: `Have bot.\n Site -- one'`,
|
||||||
// allowed: townMemebers,
|
ignoreLock: true,
|
||||||
async function(from, site){
|
async function(from, site){
|
||||||
this.__unLockCommand();
|
this.__unLockCommand();
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const botController = require('./controller/mc-bot');
|
const botController = require('./controller/mc-bot');
|
||||||
// const chatController = require('./controller/mc-chatbot');
|
|
||||||
// const actionController = require('./controller/mc-actionbot');
|
|
@ -8,33 +8,54 @@ class CJbot{
|
|||||||
isReady = false;
|
isReady = false;
|
||||||
listeners = {};
|
listeners = {};
|
||||||
|
|
||||||
|
// Holds the minimum cool down time for chat messages
|
||||||
nextChatTime = 1500;
|
nextChatTime = 1500;
|
||||||
|
|
||||||
|
// Prevents executing commands while one is running
|
||||||
commandLock = false;
|
commandLock = false;
|
||||||
commandCollDownTime = 1500;
|
commandCollDownTime = 1500;
|
||||||
|
|
||||||
|
// Holds the list of commands this bot can execute
|
||||||
commands = {};
|
commands = {};
|
||||||
|
|
||||||
|
|
||||||
combatTag = false;
|
combatTag = false;
|
||||||
doLogOut = false;
|
doLogOut = false;
|
||||||
|
|
||||||
|
// Map of all the bots currently in use
|
||||||
static bots = {};
|
static bots = {};
|
||||||
|
|
||||||
|
// Forces the code to wait while until the bot is loaded before moving on to
|
||||||
|
// the next
|
||||||
static __addLock = false;
|
static __addLock = false;
|
||||||
|
|
||||||
|
// Holds a list of bots that still needs to be connect
|
||||||
static __toConnect = [];
|
static __toConnect = [];
|
||||||
|
|
||||||
|
|
||||||
constructor(args){
|
constructor(args){
|
||||||
this.name = args.name;
|
|
||||||
|
// Friendly name to access the bot in `CJbot.bots`
|
||||||
|
this.name = args.name || args.username;
|
||||||
this.username = args.username;
|
this.username = args.username;
|
||||||
this.password = args.password;
|
this.password = args.password;
|
||||||
this.host = args.host;
|
this.host = args.host;
|
||||||
this.auth = args.auth || 'microsoft';
|
this.auth = args.auth || 'microsoft';
|
||||||
this.version = args.version || '1.19.2';
|
this.version = args.version || '1.19.2';
|
||||||
|
|
||||||
|
// States if the bot should connect when its loaded
|
||||||
this.autoReConnect = 'autoConnect' in args ? args.autoReConnect : true;
|
this.autoReConnect = 'autoConnect' in args ? args.autoReConnect : true;
|
||||||
this.autoConnect = 'autoConnect' in args ? args.autoConnect : true;
|
this.autoConnect = 'autoConnect' in args ? args.autoConnect : true;
|
||||||
|
|
||||||
|
// If we want the be always connected, kick off the function to auto
|
||||||
|
// reconnect
|
||||||
if(this.autoReConnect) this.__autoReConnect()
|
if(this.autoReConnect) this.__autoReConnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(){
|
connect(){
|
||||||
return new Promise((resolve, reject) =>{
|
return new Promise((resolve, reject) =>{
|
||||||
|
|
||||||
|
// Create the mineflayer instance
|
||||||
this.bot = mineflayer.createBot({
|
this.bot = mineflayer.createBot({
|
||||||
host: this.host,
|
host: this.host,
|
||||||
username: this.username,
|
username: this.username,
|
||||||
@ -43,43 +64,51 @@ class CJbot{
|
|||||||
auth: this.auth,
|
auth: this.auth,
|
||||||
});
|
});
|
||||||
|
|
||||||
// this.bot.on('message', (m)=> console.log(m.toString()))
|
// If an error happens before the login event, toss an error back to
|
||||||
this.bot.on('error', (m)=>{
|
// the caller of the function
|
||||||
|
let onError = this.bot.on('error', (m)=>{
|
||||||
console.log(this.bot.version, m.toString())
|
console.log(this.bot.version, m.toString())
|
||||||
reject(m)
|
reject(m)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// If the connection ends before the login event, toss an error back
|
||||||
|
// to the caller of the function
|
||||||
this.bot.on('end', (m)=>{
|
this.bot.on('end', (m)=>{
|
||||||
console.log(this.name, 'Connection ended:', m);
|
console.log(this.name, 'Connection ended:', m);
|
||||||
this.isReady = false;
|
this.isReady = false;
|
||||||
reject(m);
|
reject(m);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// When the bot is ready, return to the caller success
|
||||||
this.bot.on('login', ()=>{
|
this.bot.on('login', ()=>{
|
||||||
this.__onReady()
|
this.__onReady()
|
||||||
resolve()
|
resolve()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Set a timer to try to connect again in 30 seconds if the bot is
|
||||||
|
// not connected
|
||||||
setTimeout(async ()=>{try{
|
setTimeout(async ()=>{try{
|
||||||
if(this.autoReConnect && !this.isReady) await this.connect();
|
if(this.autoReConnect && !this.isReady) await this.connect();
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error('minecraft.js/connect/setTimeout/', this.name, ' ', error)
|
console.error('minecraft.js | connect | setTimeout |', this.name, ' ', error)
|
||||||
}}, 30000);
|
}}, 30000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
// Wrap the once method so it works correctly with await
|
||||||
|
|
||||||
once(event){
|
once(event){
|
||||||
return new Promise((resolve, reject)=> this.bot.once(event, resolve));
|
return new Promise((resolve, reject)=> this.bot.once(event, resolve));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Internal method to kick off the functionality after its loaded in the
|
||||||
|
// server
|
||||||
async __onReady(){try{
|
async __onReady(){try{
|
||||||
|
|
||||||
|
// Add the listeners to the bot. We do this so if the bot loses
|
||||||
|
// connection, the mineflayer instance will also be lost.
|
||||||
this.__startListeners();
|
this.__startListeners();
|
||||||
|
|
||||||
// console.log('listeners', this.listeners.onReady)
|
// Call the internal listeners when the bot is ready
|
||||||
|
|
||||||
for(let callback of this.listeners.onReady || []){
|
for(let callback of this.listeners.onReady || []){
|
||||||
console.log('calling listener', callback)
|
console.log('calling listener', callback)
|
||||||
await callback(this)
|
await callback(this)
|
||||||
@ -93,10 +122,9 @@ class CJbot{
|
|||||||
this.__listen();
|
this.__listen();
|
||||||
|
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error('minecraft.js/__onReady/', this.name, ' ', error)
|
console.error('minecraft.js | __onReady | ', this.name, ' ', error)
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
||||||
__startListeners(){
|
__startListeners(){
|
||||||
for(let event in this.listeners){
|
for(let event in this.listeners){
|
||||||
for(let callback of this.listeners[event]){
|
for(let callback of this.listeners[event]){
|
||||||
@ -105,11 +133,14 @@ class CJbot{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrap the .on method so we can hold the listeners internally
|
||||||
on(event, callback){
|
on(event, callback){
|
||||||
if(!this.listeners[event]) this.listeners[event] = [];
|
if(!this.listeners[event]) this.listeners[event] = [];
|
||||||
|
|
||||||
this.listeners[event].push(callback);
|
this.listeners[event].push(callback);
|
||||||
|
|
||||||
|
// If the bot is already loaded, add the passed listener to the
|
||||||
|
// mineflayer instance
|
||||||
if(this.isReady){
|
if(this.isReady){
|
||||||
if(event === 'onReady') callback(this);
|
if(event === 'onReady') callback(this);
|
||||||
else this.bot.on(event, callback);
|
else this.bot.on(event, callback);
|
||||||
@ -118,13 +149,16 @@ class CJbot{
|
|||||||
return event === 'onReady' ? true : ()=> this.bot.off(listener, callback);
|
return event === 'onReady' ? true : ()=> this.bot.off(listener, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo; add .off wrapper
|
||||||
|
|
||||||
|
// Listen for ending events and call connect again
|
||||||
__autoReConnect(){
|
__autoReConnect(){
|
||||||
try{
|
try{
|
||||||
console.log('auto connect function')
|
console.log('auto connect function')
|
||||||
this.on('end', async (reason)=>{
|
this.on('end', async (reason)=>{
|
||||||
console.error('_autorestart MC on end', reason)
|
console.error('_autorestart MC on end', reason)
|
||||||
|
|
||||||
await sleep('30000')
|
await sleep(30000)
|
||||||
this.connect()
|
this.connect()
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -137,7 +171,6 @@ class CJbot{
|
|||||||
});
|
});
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error('error in __autoReConnect', error);
|
console.error('error in __autoReConnect', error);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,6 +181,15 @@ class CJbot{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
quit(force){
|
||||||
|
if(!this.combatTag || force){
|
||||||
|
this.bot.quit();
|
||||||
|
}else{
|
||||||
|
console.log('Logout prevented due to combatTag');
|
||||||
|
this.doLogOut = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Chat and messaging*/
|
/* Chat and messaging*/
|
||||||
|
|
||||||
__listen(){
|
__listen(){
|
||||||
@ -176,8 +218,6 @@ class CJbot{
|
|||||||
if(message.toString().includes(' invited you to teleport to him.')){
|
if(message.toString().includes(' invited you to teleport to him.')){
|
||||||
// teleport invite
|
// teleport invite
|
||||||
|
|
||||||
// console.log('ChatBot on message', message.toString())
|
|
||||||
|
|
||||||
console.log('found teleport', message.toString().split(' ')[0])
|
console.log('found teleport', message.toString().split(' ')[0])
|
||||||
this.__doCommand(message.toString().split(' ')[0], '.invite');
|
this.__doCommand(message.toString().split(' ')[0], '.invite');
|
||||||
}
|
}
|
||||||
@ -206,15 +246,6 @@ class CJbot{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
quit(force){
|
|
||||||
if(!this.combatTag || force){
|
|
||||||
this.bot.quit();
|
|
||||||
}else{
|
|
||||||
console.log('Logout prevented due to combatTag');
|
|
||||||
this.doLogOut = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
__chatCoolDown(){
|
__chatCoolDown(){
|
||||||
return Math.floor(Math.random() * (3000 - 1500) + 1500);
|
return Math.floor(Math.random() * (3000 - 1500) + 1500);
|
||||||
}
|
}
|
||||||
@ -269,7 +300,7 @@ class CJbot{
|
|||||||
this.whisper(from, `I dont know anything about ${cmd}`);
|
this.whisper(from, `I dont know anything about ${cmd}`);
|
||||||
}
|
}
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error('minecraft.js/__doCommand/', this.name, ' ', error)
|
console.error('minecraft.js | __doCommand |', this.name, ' ', error)
|
||||||
}}
|
}}
|
||||||
|
|
||||||
addCommand(name, obj){
|
addCommand(name, obj){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user