Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

plugapi

plugcubed127MIT5.1.1TypeScript support: definitely-typed

Generic API for building plug.dj bots

api, bot, plug, plug.dj

readme

plugAPI Build Status npm version npm downloads NPM David Discord Server

About

A generic NodeJS API for creating plug.dj bots.

Originally by Chris Vickery, now maintained by TAT and The plug³ Team.

How to use

Run the following:

npm install plugapi

You can choose to instantiate plugAPI with either Sync or Async:

Sync:

const PlugAPI = require('plugapi');
const bot = new PlugAPI({
    email: '',
    password: ''
});

bot.connect('roomslug'); // The part after https://plug.dj

bot.on(PlugAPI.events.ROOM_JOIN, (room) => {
    console.log(`Joined ${room}`);
});

Async:

const PlugAPI = require('plugapi');

new PlugAPI({
    email: '',
    password: ''
}, (err, bot) => {
    if (!err) {
        bot.connect('roomslug'); // The part after https://plug.dj

        bot.on(PlugAPI.events.ROOM_JOIN, (room) => {
            console.log(`Joined ${room}`);
        });
    } else {
        console.log(`Error initializing plugAPI: ${err}`);
    }
});

New features in V5.0.0

Guest login is now possible if no userdata is passed into plugAPI or guest is set to true

Guest:

const PlugAPI = require('plugapi');
const bot = new PlugAPI();
// OR
const bot = new PlugAPI({
    guest: true
});

Facebook login is now possible. Easiest way to obtain the Access Token and user ID is to login via fb on plug and view the request data.

Facebook:

const PlugAPI = require('plugapi');
const bot = new PlugAPI({
    facebook: {
        accessToken: 'xxxxxxxx',
        userID: 'xxxxxxxx'
    }
});

PlugAPI now uses tough-cookie to store cookies. Refer to the wiki for more information.


Examples

Here are some bots that are using this API.

Botname Room
AuntJackie Mix-N-Mash
BotX NightCore Fanclub
BeavisBot I <3 the 80's and 90's
brainbot 5M4R7
Charlotte Youtunes
-DnB- Drum & Bass
DRPG Discord Dungeons
Ekko EDT
F!shtank SWaQ Hanger
FlavorBar Flavorz
FoxBot Approaching Nirvana
Holly Refbots Connect The Songs (Read Info!)
KawaiiBot AnimeMusic
prehibicja [PL] Prohibicja.xyz ANY GENRE
QBot EDM Qluster
Skynet Cubed PlugCubed
TFLBot The F**k Off Lounge | TFL
Toaster-chan ☆ ♥ Nightcore-331 ♥ ☆
Have a bot that uses the API? Let us know!
---
## EventListener
You can listen on essentially any event that plug emits.
`javascript
// basic chat handler to show incoming chats formatted nicely
bot.on(PlugAPI.events.CHAT, (data) => {
if (data.type === 'emote') {
console.log(data.from + data.message);
} else {`
console.log(${data.from} > ${ data.message});
}
});
`

Here's an example for automatic reconnecting on errors / close events

const reconnect = () => { bot.connect(ROOM); };

bot.on('close', reconnect);
bot.on('error', reconnect);

API Documentation

For V4 documentation, the Wiki is the best resource.

For V5 documentation, please refer to the Docs for documentation on methods and events. The documentation is written in JSDoc in the respective files found in the lib/ folder. If there are changes to be made, edit the JSDoc and run the followng command:

npm run docs

Submit a pull request and wait for review


Contribute

  1. Clone repository to an empty folder.
  2. CD to the folder containing the repository.
  3. Run npm install to set up the environment.
  4. Edit your changes in the code, and make sure it follows our codestyle.
  5. Run npm test to make sure all tests pass.
  6. After it's bug free, you may submit it as a Pull Request to this repository.

Misc Options

Multi line chat

Since plug.dj cuts off chat messages at 250 characters, you can choose to have your bot split up chat messages into multiple lines.

Delete Message Blocks

With how plug currently works, deleting messages deletes the entire group of messages from the same user. Set this option to disallow that.

Delete All Chat

PlugAPI mimics plug's behavior in disallowing deletion of chat of users above the bot's rank. Setting this option to true will bypass that check.

var bot = new PlugAPI(auth);
bot.deleteMessageBlocks = false; //set to true if you want the bot to not delete grouped messages. Default is false.
bot.deleteAllChat = false; // Set to true to enable deletion of chat regardless of role . Default is false
bot.multiLine = true; // Set to true to enable multi line chat. Default is false
bot.multiLineLimit = 5; // Set to the maximum number of lines the bot should split messages up into. Any text beyond this number will just be omitted. Default is 5.

changelog

plugAPI Changelog

5.1.1

  • UPDATED: Dependencies to latest versions. (Commit 18f9c01)
  • ADDED/CHANGED: Added in ability to change both long description and short description (Commit 7f53b32)

5.1.0

  • ADDED: Added methods to set / get badges (Commit 1ff7f73)
  • CHANGED: Remove logging for some plugdj events and ignore instead (Will still be emitted) (Commit 8e8474c)
  • CHANGED: Parse strings as numbers in API methods that require it (Commit 8e8474c)
  • CHANGED: Also accept 0 for sendChat delete timeout (Commit c7a28ef)
  • CHANGED: Websocket URL has been changed to ws-prod.plug.dj (Commit 3138c43)
  • CHANGED: Get History before setting ADVANCE object on startup (Commit 349f478)
  • FIXED: HTTP Status checks for API routes (Commit 8e8474c)
  • FIXED: Return empty array if currentDJ or waitingDJs is null for getWaitList & getDJs (Commit 8e63576)
  • FIXED: Set grabs properly (Commit 484fdb2)
  • FIXED: Error swallowing with API requests. (Commit 6b44907)
  • FIXED: Permissions for global roles (Commit 4c2c545)
  • FIXED: endpoint for waitlist unban (Commit cb6825c])
  • FIXED: revert bot.mutedTriggerNormalEvents conditional change in chat event (Commit 00af49c)
  • FIXED: Do not assume we were given a valid array for room.usersToArray (Commit d99576b)
  • FIXED: Command args should now convert properly for mentioned users. Also filter out empty strings and null/undefined. (Commit a1b8bc0)
  • UPDATED: Message splitting & escaping now provided by plug-message-split (Commit 54f7077)
  • UPDATED: Throw on joining room if user is banned, room is over capacity or other error. (Commit bead8c8)
  • UPDATED: Add URL on 403 error (Commit dd9d4c1)

5.0.0

  • ADDED: Added support for FORCE_NAME_CHANGE. (Commit 878ed6e)
  • ADDED: Experimental auto reconnect (Commit dbc113a)
  • BREAKING: humanized / jsdoc more events (Commit af3b9d7)
  • BREAKING: more humanized event types & more JSDoc (Commit b095179])
  • DOCS: Upload first iteration of auto generated docs. (Commit b6909f7)
  • UPDATED: Moved constants to a JSON & updated for new roles (Commit ae022ab)
  • UPDATED: Role update for plug.dj. * 1000 roles (Commit 6968cf5)
  • UPDATED: Added new gRoles Part 2 (#137) (Commit 273e39b)
  • UPDATED: Role Update for 9/19/17 (#136) (Commit 0ac160a)
  • UPDATED: Added Socket Class for Auto Reconnect. WaitList Ban event & methods added. Updated dependencies. (Commit ba08530)
  • UPDATED: got to latest & chalk. Fixed breaking changes. (Commit 7f327bf)

5.0.0-beta.3

  • CHANGED: Authentication now uses endpoints instead of parsing HTML (Commit d7fefcf)
  • CHANGED / FIXED: Guests now don't need to parse HTML page to login, and set cookies correctly for guest mode (Commit 8023591)
  • CHANGED: PlugAPI is now ES2015 class based. Begin working on JSDoc (Commit 4ce3a43)
  • ADDED: Facebook Login is now possible (Commit ff7ddee)
  • UPDATED: WS to 2.1.0 (Commit ca772be)
  • FIXED: updateUser should now also update bot's user data as well. (Bug since 4.2.3) (Commit 8982a70)
  • CHANGED: Readme is now modernized to ES2015 with new code examples (Commit 7d2daef)

5.0.0-beta.2

  • FIXED: Check if ws exists and is open before sending socket events. (Commit e07cdc1)
  • UPDATED: Chat Handler performance should be better. (Commit e07cdc1)
  • CHANGED: Replace request for got. Installing PlugAPI should take less time now. (Commit 27b56e8)
  • FIXED: Guest mode checks for functions it cant use. Make getPlaylists callback optional. Improved error message for logging in. (Commit 3083a43)

5.0.0-beta.1

  • ADDED: data.rawun to the user object, decode HTML entities for username in data.username, added silver property (Commit 1fd534d)
  • FIXED: data.type should now be set properly for /em messages (Commit 6795d3b)
  • FIXED: Playlist management functionality (Thanks @au2001) (Commit 10e06ed)
  • FIXED: Callback of advance should now be binded properly to avoid errors (Commit acc9bdf)
  • FIXED: Guest users should no longer say the bot has left the room (Commit c111c3c)
  • UPDATED: Events & Endpoints are now in separate JSON files for easy maintenance (Commit afe3f73)
  • UPDATED: Added constants to bot prototype. Now it is possible to do bot.ROOM_ROLE.RESIDENTDJ, etc. (Commit 51dd1cb)
  • ADDED: Guest Mode (Thanks @Dazzuh) (Commit cee7c4e)
  • FIXED: getHistory should now be the correct data, Fixed empty strings in messageData.args for command event. (Commit b29e55b)
  • BREAKING: PlugAPI now does not support node versions < 4 (Commit 2979234)
  • BREAKING: Dropped PlugAPI logger for Jethro (Commit 0094238)
  • DEPRECATE: CreateLogger is no longer usable (Commit 0094238)

  • FIXED: Playlist counts should now be correct for playlist functions (Commitca3f8b8)

  • FIXED: Logger to be less strict on type checks and dropped cookie handler for request.jar (Commit30a65da)

4.2.3

  • UPDATED: Eventemitter3 to 1.2.0 and mocha-eslint to 2.0.2 (Commit 2f1d6b4)
  • FIXED: Moved deleteAllChat to correct location (Commit 58fe475)

4.2.2

  • UPDATED: All dependencies to latest versions. (Commit a4615d8)
  • FIXED: moderateDeleteChat should check for bouncer permissions (Commit c0d0e33)

4.2.1

  • ADDED: Option to delete all chat regardless of role (Commit 3b6ccc4)
  • UPDATED: All dependencies to latest versions. (Commit 7aed7be)
  • UPDATED: Codestyle to match newest ESLint config.(Commit fc41705)
  • ADDED: Linting for tests. (Commit fc41705)

4.2.0

  • FIXED: Commands should now be able to be deleted. (Commit 3aacf8c)
  • FIXED: parseInt if getUser uid is String (Commit 3aacf8c)
  • ADDED: MAINT_MODE and MAINT_MODE_ALERT events (Commit 3aacf8c)
  • IMPROVE: JSDoc to be more consistent (Commit 3aacf8c)
  • IMPROVE: Typo in utils comment (Commit 322bcbb) 4.1.2

  • ADDED: Object.assign Ponyfill (Commit 48d3c92)
  • UPDATED: Request to 2.61.0 , ws to 0.8.0 (Commit 48d3c92)
  • CHANGED: Use Array.isArray instead of util.isArray due to being deprecated (Commit ac2bdbc)
  • CHANGED: Use Object.assign ponyfill instead of util._extend (Commit 1927037)
  • FIXED: Null / Undefined messages should not break sendChat (Commit ac2bdbc)

4.1.1

  • FIXED: Null issue with ws (Commit 73d9409)
  • FIXED: A few broken links in readme (Commit d635862)

4.1.0

  • ADDED: Starting work on mocha test. Linting added for now. (Commit 33375f3)
  • CHANGED: Linted and fixed many issues with code. (Commit 33375f3)
  • FIXED: Forgot to rename function in switch case. (Commit 33375f3)
  • ADDED: Default case for mutes (sets to null if time is not defined) (Commit 33375f3)
  • CHANGED: JSDoc to be valid and descriptions where needed. (Commit 33375f3)
  • CHANGED: Use Strict mode. (Commit 33375f3)

4.0.1

  • FIXED: Room.js was returning Internal Users Array and not the local variable. (Commit 07793b8)

4.0.0

  • CHANGED: Room.js object checks to find both null and undefined (Commit c54e34b)
  • REMOVED: cookies saving / cookies.tmp file. Was causing too many issues. (Commit 10059f7) (Commit 30b2a13)
  • CHANGED: Most of plugAPI is now compliant to the node.js style of callback(err, data) (Commit 10059f7)
  • CHANGED: Use IsFinite ES6 polyfill for a more accurate check of numbers. (Commit 10059f7)
  • ADDED: Use Handle-Errors to throw if no callback is defined, else return the error. (Commit 10059f7)
  • FIXED: Empty Arguments getting interpreted as 0. (Commit 10059f7)
  • FIXED: moderateForceSkip skipping other users. (Commit 77baafb)
  • CHANGED: From Eventemitter 2 to Eventemitter 3. (Commit bea2829)

BREAKING CHANGES:

  • OnAny and OffAny are now removed.
  • Async now requires end users to handle errors by plugAPI.

3.5.0

  • REMOVED: Removed Bin and Minification build. Not neccesary for a node.js library. (Commit 7ddf40)
  • ADDED: selfSkip method to skip self (Commit 44c6b78).
  • CHANGED: moderateForceSkip will skip self if person djing is the bot (Commit 44c6b78).
  • CHANGED: Codestyle to be multiple var declarations. Fixed a duplicate var in room.js as well (Commit 44c6b78).
  • CHANGED: Added in a baseURL to request (Commit 44c6b78).
  • UPDATED: Dependencies: chalk@1.1.0, request to 2.60.0 (Commit 44c6b78).
  • CHANGED: Removed outdated node-html-encoder for he (Commit 44c6b78).
  • CHANGED: Src is now Lib to fit the pattern of node.js modules. (Commit 44c6b78).
  • REMOVED: Grunt and ClosureCompiler. Will add testing later on. (Commit 7ddf40)

3.4.0

  • CHANGED: Removed getStatus and setStatus and updated plugAPI's status object. (Commit e2e331e).
  • ADDED: Added friendRequest, gifted, and modBan to ignore list. (Commit f9c703d).
  • ADDED: Added djListCycle and djListLocked event objects, Added getBoothMeta function. (Commit 0010d0b).
  • CHANGED: Changed djListUpdate to emit user objects instead of ids. (Commit 0010d0b).
  • UPDATED: Updated user object properties and defaults.(Commit b5abec1).
  • ADDED: Added room description, name, and welcome update events. Added getRoomMeta function. Don't add guests to users listing. (Commit aab71a9).
  • ADDED: Added 2 more bots to readme info. QBot and Holly Refbots (Commit 9bfd810). (Commit f9aa9a8).

3.3.0

3.2.1

  • CHANGED: Npmignore apparently is case sensitive... actually ignore Gruntfile.js
  • FIXED: Markdown in Changelog was wrong in previous entries.

3.2.0

  • REMOVED: REPL Example. Not very used anymore.
  • ADDED: Npmignore file to ignore files when publishing.
  • FIXED: Latest WS was breaking connection due to removal of Origin Header.
  • CHANGED: Pinned dependencies to avoid issues with breaking changes.
  • CHANGED: Changed TAT to contributor and Thedark1337 to Author
  • ADDED: Bugs URL in package json to link to Github issues.
  • ADDED: David Badge to check if dependencies are at latest version.

3.1.4

  • ADDED: Chat Level Update Object and notificaton when it changes.
  • IMPROVE: Logging in to plug.dj via plugAPI should show more descriptive errors instead of stacktraces that were unreadable.
  • CHANGED: Code change for cookies.js, should be typeof === 'undefined'
  • FIXED: CreatePlaylist typeof check was String instead of string. Fix provided by @purechaose

3.1.3

  • FIXED: Grab was always false, and users were not being removed from staff properly. Fix provided by @anjanms.
  • FIXED: ModerateBanUser was not returning true if successful.Fix provided by @chrishayesmu.

3.1.2

  • FIXED: Added type back to chat messages.

3.1.1

  • FIXED: Update for Go DJ web socket

3.1.0

  • ADDED: Added utils
  • FIXED: Fixed being unable to lock and unlock the booth without reloading the bot. (#57)

3.0.0

  • FIXED: Working with plug.dj > v1.0.0
    • New connection method for socket server (Chat server and socket server combined)
  • ADDED: Manage a history of chat, limited to 512 messages (Same limit as browser clients)
  • ADDED: Documentation in JSDoc format (If you're using an IDE, this should give you in-editor documentation)
  • CHANGED: Uses e-mail and password as authentication
  • CHANGED: bot.ROLE split up into bot.ROOM_ROLE and bot.GLOBAL_ROLE
  • REMOVED: All deprecated functions are removed
  • REMOVED: bot.isUsernameAvailable(name, callback) and bot.changeUsername(name, callback) removed
  • A lot more, can't remember everything I have done...

2.4.1

  • FIXED: Compatibility with pre-2.4.0 command eventlisteners using chatID (Commit b9afcba)

2.4.0

  • ADDED: Added deleteCommands (Commit bf8acd8)
  • IMPROVE: Improvements for plug.dj v0.9.833 (Commit 5fa24ad)

2.3.2

  • FIXED: Temporary fix for plug.dj v0.9.833 (Commit 69e2f51)

2.3.1

  • FIXED: Fix detecting numbers (Commit 0319ad7)

2.3.0

  • ADDED: Set status via setStatus (Commit 7bd4390)
  • IMPROVE: Convert numbers in args to actual numbers (Commit 629a655)
  • FIXED: Fix error with commandPrefix longer than 1 character (Commit c259fe8)

2.2.8

  • IMPROVE: Include all user data on USER_LEAVE (Commit b198ddc)

2.2.7

  • FIXED: Gateway error requesting captcha (Commit f5e4590)

2.2.6

  • FIXED: Fix dependency for sockjs-client-node (Commit a1650eb)

2.2.5

  • FIXED: Be sure the user exists while handling MODERATE_STAFF messages - Fixes #14 (Commit 7883859)

2.2.4

  • IMPROVE: Allow command prefixes longer than 1 character (Commit 56374ed)
  • IMPROVE: Added sourcemap for easier tracking of issues and bugs (Commit 56374ed)

2.2.3

  • IMPROVE: Improve invalid roomname detection (Commit 3d0dbee)

2.2.2

  • IMPROVE: Allow banning people not in the room (Commit 78e7945)

2.2.1

  • FIXED: Fixed coding error (Commit ba52159)

2.2.0

  • ADDED: Added havePermission (Commit a323aa8)

2.1.0

  • ADDED: Added close (Commit ffa6f65)
  • ADDED: Ability to set other command prefix (Commit a122f47)
  • ADDED: Ability to have bot's own messages processed (Commit ea226c8)
  • ADDED: Add function to move a user in waitlist through user object (Commit 303d5fc)
  • IMPROVE: Improve code for joining room (Commit 47335b2)
  • IMPROVE: Added checks to prevent unnecessary requests (Commit c1691ca)
  • FIXED: Connect to p3's socket server via HTTPS (Commit d5b32d3)
  • DEPRECATE: Deprecate setLogObject (Commit 3e8c690)
  • DEPRECATE: Deprecate old parameters for getTwitterAuth (Commit b7e2579)
  • DEPRECATE: Deprecate moderateUnBanUser (Commit 5bb2fda)
  • DEPRECATE: Deprecate addToWaitlist and removeFromWaitlist on user objects (Commit b1cf712)

2.0.3

  • FIXED: Fix broken getHistory (Commit 645f03a)

2.0.2

  • FIXED: More inteligent way of running the queueTicker (Commit 80e314c)

2.0.1

  • FIXED: Changed required node version from 0.6 to 0.10 (Commit 78ec90f)

2.0.0

  • ADDED: Settings files for formatting in .jsbeautifyrc and .editorconfig format (Commit 5123fbc)
  • ADDED: Prevent spamming plug.dj's servers (Commit e52a3ef)
  • ADDED: Support to use plugCubed's socket server for PMs (Commit e52a3ef)
  • FIXED: plug.dj backend changes (Commit e52a3ef)
  • FIXED: plug-dj-login is now an optional dependency as it was supposed to be (Commit 9003025)
  • Using Grunt to run Closure Compiler on the api (Commit 7719cdc)
  • Fix order of readme (Commit b7b66a6)
  • Facepalm, bin folder needs to be included (Commit 7719cdc)

1.1.4

  • FIXED: Ensure gateway requests sends an array (Commit d000f9d)

1.1.3

  • FIXED: Uncompress the updatecode from the server (Commit 963235a)
  • REMOVE: Removed cheerio as a dependency as it wasn't used (Commit 52731a6)

1.1.2

  • CHANGED: Now getting the updateCode from server (Commit b32bc48)

1.1.1

  • FIXED: Hopefully give an error when gateway returns invalid JSON (Commit dfb9c6c)
  • FIXED: Actually remove the user when the user leaves the room (Commit dfb9c6c)

1.1.0

  • ADDED: Added optional autodelete of message (Commit af6f879)
    1. Added respondTimeout for commands
    2. Added timeout parameter for intChat, chat and sendChat
  • CHANGED: Now using MIT license (Commit 93da87d)

1.0.0

  • Initial Release
  • TAT is added as maintainer of the official npm package