Giter VIP home page Giter VIP logo

hackmud-chat-api's Introduction

hackmud-chat-api

A client for the Hackmud Chat API.

Additional docs for the chat API

Installation

npm i -s @com1killer/hackmud-chat-api for module use

npm i -g @com1killer/hackmud-chat-api for CLI use

CLI Usage

hm-pass2token <chat pass>
Converts your chat pass to a chat token.

Usage example

var HackmudChatAPI = require("@com1killer/hackmud-chat-api");
var chat = new HackmudChatAPI("YOUR_CHAT_TOKEN_OR_CHAT_PASS");
console.log(chat.token);

chat.on("poll", function pollHandler(messages) { // When messages are polled
    let cleanMessages = messages.filter(message => message.to_user == "my_bot_user" && messages.to_user != messages.from_user); // Filter messages. Only messages sent to bot user remains, and echo is removed.

    messages.forEach(message => { // For every message
        chat.tell("my_bot_user", "my_user", message.from_user + " said:\n" + message.msg); // Relay message to another user
    })
});

Reference

new HackmudChatAPI

Creates a HackmudChatAPI instance. Converts chat pass into chat token if needed, does initial account sync, and sets up timers.

Arguments:

  • tokenOrPass: Chat token or chat pass. If chat pass, it will be converted to chat token.
  • pollInterval: Polling frequency in milliseconds. Must be over 700ms to avoid ratelimits. Recommended to be over 2s. Default: 2s
  • accountSyncInterval: Account sync (updated usernames) frequency in milliseconds. Must be over 5s to avoid ratelimits, but a much bigger frequency is enough. Default: 30m
  • url: URL of Chat API. Doesn't need to be touched. Default: "https://www.hackmud.com/mobile"

Properties

  • token: Chat token.
  • users: An object of users, where the user is the key, and an array of their joined channels is the value.
  • lastPoll: The JS timestamp of the last poll.

on

Adds an event handler.

Arguments:

  • name: Name of event. (poll, error, accountSync)
  • handler: Event handler. Argument:
    • poll: An array of messages.
    • error: An error.
    • accountSync: An object of users, where the user is the key, and an array of their joined channels is the value.

supervise

Adds an event handler for every event.

Arguments:

  • handler: Event handler. See argument above.

send

Sends a message to a channel.

Arguments:

  • from: Username to send the message from.
  • channel: Channel to send the message to.
  • message: Message to send.

Returns:

  • ok: Success?

tell

Sends a tell to a user.

Arguments:

  • from: Username to send the message from.
  • to: Username to send the message to.
  • message: Message to send.

Returns:

  • ok: Success?

destroy

Destroys HackmudChatAPI instance.

Message

A message object.

Properties:

  • id: Message ID
  • t: JS timestamp of message
  • from_user: Username of sender
  • msg: Message
  • is_join: Whether the chat message is a channel join, not present if false
  • is_leave: Whether the chat message is a channel leave, not present if false
  • channel: Channel of message, not present in tells
  • to_user: Username of recipient

hackmud-chat-api's People

Contributors

com1killer avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.