diff --git a/index.js b/index.js index 2f63c10..3b5faf4 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,19 @@ const authCommand = require('./lib/generate-auth') -module.exports = function(bot, botConfig) { +module.exports = function (bot, botConfig) { // Detect possible login/register failure var isCommandSended = false; var config = botConfig.AutoAuth; - if(!bot) { + if (!bot) { throw new Error('Bot object is missing, please provide valid mineflayer bot as first argument'); } - if(!config) { + if (!config) { throw new Error('Password is missing, expecting string or object as second argument'); } - if(typeof config === 'string') { + if (typeof config === 'string') { config = { password: config }; @@ -23,44 +23,52 @@ module.exports = function(bot, botConfig) { bot.addChatPattern('registerRequest', /\/register/); bot.addChatPattern('loginRequest', /\/login/); + bot.addChatPattern("loginSuccess", /Successful login!/); - bot.on('chat:registerRequest', function() { + function remove() { + bot.removeChatPattern("registerRequest") + bot.removeChatPattern("loginRequest") + bot.removeChatPattern("loginSuccess") + } + + bot.on('chat:registerRequest', () => { bot.chat(authCommand('register', config.password)); - if(config.logging) { + if (config.logging) { console.log('Got register request'); } - bot.emit('serverAuth'); - - if(isCommandSended) { + if (isCommandSended) { console.log('Register request repeated, probably failed to register'); - if(config.repeatCb) { + if (config.repeatCb) { config.repeatCb.call(); } } - if(!config.ignoreRepeat) { + if (!config.ignoreRepeat) { isCommandSended = true; } }); - bot.on('chat:loginRequest', function() { + bot.on('chat:loginRequest', () => { bot.chat(authCommand('login', config.password)); - if(config.logging) { + if (config.logging) { console.log('Got login request'); } - bot.emit('serverAuth'); - - if(isCommandSended) { + if (isCommandSended) { console.log('Login request repeated, probably failed to login'); - if(config.repeatCb) { + if (config.repeatCb) { config.repeatCb.call(); } } - if(!config.ignoreRepeat) { + if (!config.ignoreRepeat) { isCommandSended = true; } }); + + bot.on("chat:loginSuccess", async () => { + bot.emit('serverAuth'); + remove() + }) }; diff --git a/lib/generate-auth.js b/lib/generate-auth.js index 844cc16..e51e2c0 100644 --- a/lib/generate-auth.js +++ b/lib/generate-auth.js @@ -1,8 +1,8 @@ module.exports = (type, password) => { - let command = '/' + type + ' ' + password; + let command = `/${type} ${password}` if(type === 'register') { - command += ' ' + password; + command += ` ${password}` } return command;