Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
};
Expand All @@ -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()
})
};
4 changes: 2 additions & 2 deletions lib/generate-auth.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down