transformice.js
Version:
Node.js client for Transformice with full Typescript support.
257 lines (256 loc) • 6.74 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from "events";
import { ByteArray, Connection, ValueOf } from "../utils";
import { Room, RoomPlayer } from "../structures";
import { languages } from "../enums";
import ClientEvents from "./Events";
interface ClientOptions {
/**
* Will try to auto reconnect when disconnected if set to true (Default: `true`)
*/
autoReconnect?: boolean;
/**
* Which community to log in to ([language enum](/docs/api/globals#languages))
*/
language?: ValueOf<typeof languages>;
}
/**
* Client interface for event intellisense support
*/
declare interface Client {
/**
* Listens to a Client Event
*/
on<T extends keyof ClientEvents>(event: T, listener: ClientEvents[T]): this;
/** @hidden */
emit<T extends keyof ClientEvents>(event: T, ...args: Parameters<ClientEvents[T]>): boolean;
}
/**
* Represents a client that connects to Transformice.
*
* @example
* ```js
* const { Client, enums } = require("transformice.js");
*
* const client = new Client("username", "password", {
* language: enums.languages.en
* });
*
* client.on("roomMessage", (message) => {
* if (client.name === message.author.name) return;
* client.sendRoomMessage(message.author.look);
* });
*
* client.run("tfm_id", "token");
* ```
*
* @noInheritDoc
*/
declare class Client extends EventEmitter {
private version;
private connectionKey;
private authClient;
protected authServer: number;
protected ports: number[];
private host;
private tfmId;
private token;
protected identificationKeys: number[];
protected messageKeys: number[];
private main;
protected bulle: Connection;
private loops;
private tribulleId;
private password;
protected autoReconnect: boolean;
protected whoList: Record<number, string>;
/**
* The online players when the bot log.
*/
onlinePlayers: number;
/**
* The client's room.
*/
room: Room;
/**
* The client's joined channels.
*/
channels: string[];
/**
* The client's player.
*/
player: RoomPlayer;
/**
* The client's Id.
*/
playerId: number;
/**
* The client's name.
*/
name: string;
/**
* The client's playing time.
*/
playingTime: number;
/**
* The connection time.
*/
connectionTime: number;
/**
* The client's community code.
*/
community: number;
/**
* The language suggested by the server.
*/
language: ValueOf<typeof languages>;
/**
* The client's temporary code.
*/
pcode: number;
constructor(name: string, password: string, options?: ClientOptions);
/**
* Wait for specific event to be emitted
*/
private waitFor;
/**
* Handles the old packets and emits events.
*/
protected handleOldPacket(conn: Connection, ccc: number, data: string[]): void;
/**
* Handles the known packets and emits events.
*/
protected handlePacket(conn: Connection, packet: ByteArray): void;
/**
* Handles the community platform packets and emits events.
*/
protected handleTribulle(code: number, packet: ByteArray): void;
/**
* Sends a packet to the community platform (tribulle).
*/
private sendTribullePacket;
/**
* Sends a packet every 15 seconds to stay connected to the game.
*/
protected startHeartbeat(): void;
private sendHandshake;
protected setLanguage(code?: ValueOf<typeof languages>): void;
protected setSystemInfo(langue: string, sys: string, version: string): void;
/**
* Get Transformice API keys
*/
private fetchKeys;
/**
* Log in to the game.
*/
private login;
/**
* Connects and do handshake
*/
private connect;
/**
* Disconnects the client.
*/
disconnect(): void;
/**
* Starts the client.
*/
run(tfmid: string, token: string): Promise<void>;
/**
* Restart the client
*/
restart(): Promise<void>;
/**
* Sends a message to tribe
*/
sendTribeMessage(message: string): void;
/**
* Joins the tribe house.
*/
enterTribeHouse(): void;
/**
* Load a lua script in the room.
*/
loadLua(script: string): void;
/**
* Join to a chat channel
*/
joinChannel(channelName: string, permanent?: boolean): void;
/**
* Leave a chat channel
*/
leaveChannel(channelName: string): void;
/**
* Request /who to a chat channel
*/
requestWho(channelName: string): number;
/**
* Get player list inside a chat channel
*/
getChannelPlayers(channelName: string): Promise<import("../structures").Player[]>;
/**
* Sends a message to a chat channel
*/
sendChannelMessage(channelName: string, message: string): void;
/**
* Sends a message to the client's room.
*/
sendRoomMessage(message: string): void;
/**
* Sends a server command.
*
* @param message - The command message (without the `/`).
* @example
* ```js
* client.sendCommand('mod');
* ```
*/
sendCommand(message: string): void;
/**
* Sends a request to the server to join a room with specific name.
*/
enterRoom(name: string, options: {
auto?: boolean;
community?: number;
password?: string;
}): void;
/**
* Sends a whisper message to a player.
*/
sendWhisper(name: string, message: string): void;
/**
* Request friend list.
*/
requestFriendList(): void;
/**
* Get friend list
*/
getFriendList(): Promise<import("../structures").Friend[]>;
/**
* Add a player to friend list
*/
addFriend(name: string): void;
/**
* Add a player to friend list
*/
removeFriend(name: string): void;
/**
* Request tribe data
*/
requestTribe(includeDisconnectedMember?: boolean): void;
/**
* Get tribe data
*/
getTribe(includeDisconnectedMember?: boolean): Promise<import("../structures").Tribe | null>;
/**
* Request profile
*
* Alias for: `client.sendCommand("profile")`
*/
requestProfile(username: string): void;
/**
* Get user profile
*/
getProfile(username: string): Promise<import("../structures").Profile>;
}
export default Client;