Current section
Files
Jump to
Current section
Files
src/mineflayer_bot_ffi.mjs
import * as mineflayer from 'mineflayer'
import { Error, Ok } from './gleam.mjs'
import * as bot from './mineflayer/bot.mjs'
import { option_or_undefined, mineflayer_error_to_gleam } from './utils_js.mjs'
export function create_bot(gleamBotOpts) {
const jsBotOpts = {...gleamBotOpts}
if (gleamBotOpts.auth instanceof bot.Microsoft) {
jsBotOpts.auth = 'microsoft'
} else if (gleamBotOpts.auth instanceof bot.Mojang) {
return new Error(new bot.DeprecatedAuth(new Mojang()))
} else {
jsBotOpts.auth = 'offline'
}
jsBotOpts.password = option_or_undefined(gleamBotOpts.password)
jsBotOpts.logErrors = false
jsBotOpts.hideErrors = true
try {
const b = mineflayer.createBot(jsBotOpts)
b.on('error', (err) => {
b.emit('gleam_error_123', mineflayer_error_to_gleam(err))
})
return new Ok(b)
} catch (err) {
if (err.message.startsWith('unsupported protocol version:')) {
return new Error(new bot.UnsupportedProtocolVersion(err.message.substring(30)))
}
return new Error(new bot.UnknownError(err.message))
}
}
export function end_bot(bot, reason) {
bot.end(option_or_undefined(reason))
}
export function send_chat(bot, message) {
bot.chat(message)
return bot
}