An easy-to-use hackmud Chat API module.
Latest NPM release (recommended): npm @skiilaa/install hackmud-chat-api
Latest GitHub version: npm install moriczgergo/hackmud-chat-api
var Hackmud = require('hackmud-chat-api');
var chat = new Hackmud("token or chat_pass");
console.log("Logged in! Token: " + chat.token);
console.log("Users: " + chat.user.join(", "));
chat.subscribe(messages => {
messages.forEach(message => {
console.log("From: " + message.from_user);
console.log("To: " + message.to_user);
if (message.channel) { // If sent in a channel
console.log("Sent in " + message.channel);
chat.send(message.to_user, message.channel, message.msg); // Reply with the same message in the same channel with the user that received the message.
} else { // If sent in private
chat.tell(message.to_user, message.from_user, message.msg) // Reply with the same message in private with the user that received that message.
console.log("Sent in private");
}
console.log(message.msg);
});
});
Unique message ID provided by the API.
Unix timestamp in milliseconds.
The user who sent the message.
The user who received the message.
The message's text. (with color codes)
Optional, only present if the message was sent in a channel, if the message wasn't a tell.
Parameters:
auth
: A token or a chat_pass.
Initializes the client.
Example:
var chat = new Client("ghwef");
Params:
handler
: A callback that receives messages.- 1st param: Array of messages.
users
: Array of users to listen with. Optional.
Returns: Index of listener. You'll need this if you want to unsubscribe.
Example:
var handlerIndex = chat.subscribe(messages => {
console.log("Messages received:");
messages.forEach(message => {
console.log(message.msg);
});
});
Params:
index
: Index of handler to unsubscribe.
Example:
chat.unsubscribe(handlerIndex);
Params:
username
: Username to send message with.channel
: Channel to send message to.msg
: Message to send.
Returns: null or error, if there was one.
Example:
var err = chat.send("my_bot", "0000", "my_bot.totally_not_a_scam is the best script ever.");
if (err) console.error("An error occurred: " + err);
Params:
username
: Username to send message with.tell
˙: Username to send message to.msg
: Message to send.
Returns: null or error, if there was one.
Example:
var err = chat.tell("my_bot", "me", "Started running.");
if (err) console.error("An error occurred: " + err);
Array of users.
An object. The keys are usernames, and the values are arrays of channel names that the user has joined.
Example:
{
me: ["0000", "town", "CHOICE_EPSILON_2"],
my_bot: ["0000"]
}
The token, either the one passed to the constructor, or the one generated from chat_pass.