import { createBot } from 'mineflayer'; import { createClient } from 'minecraft-protocol'; import got from 'got'; const defaultAuthServer = 'http://authserver.thealtening.com'; const defaultSessionServer = 'http://sessionserver.thealtening.com'; export interface AltData { token: string password?: string username?: string limit?: boolean skin?: string info?: any valid?: boolean } export class Alt { token: string; constructor(public data: AltData) { this.token = this.data.token; } /** * Create a `minecraft-protocol` client from its options for this alt. * @param opts The options to create the `minecraft-protocol` client with. */ createClient(opts: any) { const defaults = { username: this.token, password: 'Password', authServer: opts.proxy ?? defaultAuthServer, sessionServer: opts.proxy ?? defaultSessionServer }; return createClient({ ...defaults, ...opts } as any); } /** * Create a `mineflayer` bot from its options for this alt. * @param opts The options to create the `mineflayer` bot with. */ createBot(opts: any) { const defaults = { username: this.token, password: 'Password', authServer: opts.proxy ?? defaultAuthServer, sessionServer: opts.proxy ?? defaultSessionServer }; return createBot({ ...defaults, ...opts } as any); } } export class Client { constructor(public api: string) {} /** * Sends a `GET` request to the Altening api on a lower level. * @param url The path for your request * @param options The options of the request */ async get(url: string, options?: {}): Promise { const req = `${this.api}${url}`; return got.get(req, { responseType: 'json', ...options }).then(i => i.body); } /** * Sends at a `POST` request to the Altening api on a lower level. * @param url The path for your request * @param options The options of the request */ async post(url: string, options?: {}): Promise { return got.post(`${this.api}${url}`, { responseType: 'json', ...options }).then(i => i.body); } /** * Get an alt, optionally from a pre-existing token * @param token Optional - Create an alt object from its token. If the token isn't specified, it generates a new alt with `altening.generate()` */ async alt(token?: string) { if (token != null) { return this.generate(); } return new Alt({ token }); } /** * Generates an alt from the Altening API. */ async generate() { const alt: AltData = await this.get('/generate'); return new Alt(alt); } /** * Create a `minecraft-protocol` client from its options and the altening token. * @param opts The options to create the `minecraft-protocol` client with. * @param token The token of the bot to create the `minecraft-protocol` bot with. */ async createClient(opts: any, token?: string) { const bot = await this.alt(token); return bot.createClient(opts); } /** * Create a `mineflayer` bot from its options and the altening token. * @param opts The options to create the `mineflayer` bot with. * @param token The token of the bot to create the `mineflayer` bot with. */ async createBot(opts: any, token?: string) { const bot = await this.alt(token); return bot.createBot(opts); } } export default new Client('https://api.thealtening.com/free');