hackmud-chatapi v0.11.0
Hackmud-ChatAPI
Class: Account
new Account(token, tick_rate)
Creates a new account associated with the token specified token
. For more information about tick_rate
, see account.tick_rate
Account.getToken(pass)
Fetches a new chat token from the specified pass.
Event: 'message'
Fired whenever a message is received on this account.
account.destroy()
Destroys this account. This calls destroy
on the agent
and stops the tick mechanism.
You must call this function before exiting. Otherwise, the tick mechanism will always keep the event loop busy, and the node process will never exit.
account.fetchUsers()
- Returns: <Promise<string[]>>
Fetch the userdata for this account. The data will be stored in account.userData
, the usernames in account.users
. The Promise resolves to the new value of account.users
.
account.getHistory(usernames)
usernames
<string[]> The usernames to return history for. Default:account.users
before
<Date> The latest point in time for which a message may be returned. Default:Date.now()
- Returns: <Promise<Object[]>>
Get the chat history of this account.
account.getUser(username)
Get a specific user. If the user does not exist on this account, this method returns null
.
Note: This function performs no update checks and works solely off of the value of account.users
. It is thus recommended to call account.fetchUsers()
before calling this method.
account.request(endpoint, data)
endpoint
<string> The server endpoint to send the request to.data
<Object> The data to include in the request. Default:{}
- Returns: <Promise<Object>>
Send a request on behalf of this account. The account's agent and token will be automatically used/added for/to the request. The Promise returned resolves with the body of the reply.
account.send(sender, channel, message)
sender
<string> The user to send the message fromchannel
<string> The channel to send the message tomessage
<string> The message content- Returns: <Promise<boolean>> The value of
ok
in the reply
Send a message to a channel. Acts like chats.send
ingame. The message may be looped back in a message
event(#eventmessage).
account.tell(sender, to, message)
sender
<string> The user to send the tell fromto
<string> the user to send the tell to.message
<string> The message content- Returns: <Promise<boolean>> The value of
ok
in the reply
Send a tell to a user. Acts like chats.tell
ingame. The message may be looped back in a message
event.
account.tick()
Perform a tick for this account. User code should rarely, if ever, call this function. The new tick is scheduled after this method completes.
account.agent
The agent this account uses for outgoing requests. This agent is created with {keepAlive: true}
.
account.last_tick
The last time a tick completed, but before the respective events are fired.
account.tickTimeout
The timeout of the next tick. May be stale (used up) or null
if tick_rate
is Infinity
.
account.tick_rate
The amount of milliseconds delay between ticks. Should be above 800 and never below 700 (unless you enjoy getting errors about polling too fast). Takes effect on the next tick.
account.token
The token for this account.
account.userData
The user data for this account. Filled by account.fetchUsers
.
account.users
The usernames for this account. Filled by account.fetchUsers
.
Class: User
Event: 'message'
message
<Message> The message itself
Fired whenever a message is received for this user.
user.getHistory(before)
before
<Date> The latest point in time for which a message may be returned. Default:Date.now()
- Returns: <Promise<Object[]>>
Get the chat history of this user.
user.send(channel, message)
channel
<string> The channel to send the message tomessage
<string> The message content- Returns: <Promise<boolean>> The value of
ok
in the reply
Send a message to a channel. Acts like chats.send
ingame. The message may be looped back in a message
event.
user.tell(to, message)
to
<string> the user to send the tell to.message
<string> The message content- Returns: <Promise<boolean>> The value of
ok
in the reply
Send a tell to a user. Acts like chats.tell
ingame. The message may be looped back in a message
event.
user.account
The account this user is associated with.
user.username
The name of this user.
Class: Message
Represents a message received from the server.
message.channel
If this message was received from a channel, the channel name. Otherwise, null
.
message.content
The content of this message.
message.from_me
Whether this message was sent from a user associated with this account. This indicates a possible loopback message or you sending messages ingame.
message.sender
The name of the user who sent this message.
message.to
If this is a tell, the name of the user to which this tell was sent. Otherwise, null
.
message.type
The "special type" of this message, if any. Currently, there are only two types: "join"
for channel join messages and "leave"
for channel leave messages.
cfg
An object used for various configuration. Contains the following properties:
cfg.HACKMUD_ADDRESS
- <string> Default:
"www.hackmud.com"
The address of the server this wrapper talks to. Only change if you know what you are doing.
cfg.REQUIRE_TLS_SECURE
- <boolean> Default:
true
Whether to require a secure TLS connection while talking to the server. Only set to false
if you know what you are doing.