Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const _ = require('lodash');
- const EventEmitter = require('events').EventEmitter;
- const inherits = require('util').inherits;
- const request = require('co-request');
- const socketio = require('socket.io-client');
- const debug = require('debug')('instant:client');
- const debugtick = require('debug')('verbose:tick');
- const debugchat = require('debug')('instant:chat');
- const https = require('https');
- https.globalAgent.options.rejectUnauthorized = false;
- module.exports = JdClient;
- function JdClient(username, password) {
- EventEmitter.call(this);
- let self = this;
- // Save configuration and stuff.
- async () => {
- self.username = null;
- self.name = null;
- self.balance = null;
- self.csrf = null;
- self.inits = null;
- self.uid = null;
- self.invested = null;
- self.balance = null;
- self.max_profit = null;
- self.cookie = null;
- self.version = '0.1.5'
- self.url = 'https://just-dice.com'
- self.credentials = {
- hash: '',
- username: username,
- password: password,
- code: ''
- }
- self.cookie = yield login(self.credentials, self.url)
- console.log('JD - Received cookie: ' + self.cookie);
- console.log('JD - Setting up connection to %s', self.url);
- self.socket = socketio(self.url, {
- multiplex: false,
- agent: https.globalAgent,
- extraHeaders: {
- origin: self.url,
- cookie: self.cookie
- }
- });
- self.socket.on('error', self.onError.bind(self));
- self.socket.on('login_error', self.onError.bind(self));
- self.socket.on('invest_error', self.onError.bind(self));
- self.socket.on('divest_error', self.onError.bind(self));
- self.socket.on('getver', self.onGetVer.bind(self));
- self.socket.on('tip', self.onTip.bind(self));
- self.socket.on('init', self.onInit.bind(self));
- self.socket.on('set_hash', self.onSetHash.bind(self));
- self.socket.on('balance', self.onBalance.bind(self));
- self.socket.on('chat', self.onMsg.bind(self));
- });
- }
- inherits(JdClient, EventEmitter);
- JdClient.prototype.onBalance = function(data) {
- console.log('JD - onBalance: ', data);
- this.balance = data;
- this.emit('balance', this.balance)
- };
- JdClient.prototype.onGetVer = function(key) {
- socket.emit('version', this.csrf, key, "jdbot:" + this.version);
- console.log('JD - onGetVer: ', key);
- };
- JdClient.prototype.onSetHash = function(hash) {
- console.log('JD - onSetHash: ', hash);
- };
- JdClient.prototype.onTip = function(from, name, amount, comment, ignored) {
- console.log('JD - onTip: ', from, name, amount, comment, ignored);
- };
- JdClient.prototype.onInit = function(data) {
- this.invested = data.investment;
- this.uid = data.uid;
- this.name = data.name;
- this.username = data.username;
- if (!this.inits++) {
- this.csrf = data.csrf;
- this.balance = data.balance;
- this.emit('balance', this.balance)
- console.log('JD - connected as (' + this.uid + ') <' + this.name + '>');
- } else {
- console.log('JD - reconnected');
- this.csrf = data.csrf;
- }
- };
- JdClient.prototype.onError = function(err) {
- console.log('JD - onError: ', err);
- };
- JdClient.prototype.onErr = function(err) {
- console.log('JD - onErr: ', err);
- };
- JdClient.prototype.onDisconnect = function(data) {
- console.log('JD - Client disconnected |', data, '|', typeof data);
- this.emit('disconnect');
- };
- JdClient.prototype.onMsg = function(message) {
- let msg = {}
- msg.site = 'JD'
- //setup message format for Instant and determine if message if private or public to
- // determine how to respond to the user
- if (message.match(/\[ \((.*?)\) <(.*?)> → \(1626506\) <rowan> \] (.*)/)) {
- let isPrivateRegMatch = message.match(/\[ \((.*?)\) <(.*?)> → \(1626506\) <rowan> \] (.*)/)
- msg.type = 'private'
- msg.uid = Number(isPrivateRegMatch[1])
- msg.name = isPrivateRegMatch[2]
- msg.message = isPrivateRegMatch[3].trim()
- } else {
- if (!message.match(/\((.*?)\) <(.*?)> (.*)/))
- return
- let isPublicRegMatch = message.match(/\((.*?)\) <(.*?)> (.*)/)
- msg.type = 'public'
- msg.uid = Number(isPublicRegMatch[1])
- msg.name = isPublicRegMatch[2]
- msg.message = isPublicRegMatch[3].trim()
- }
- this.emit('msg', msg);
- };
- JdClient.prototype.say = function(uid, msg) {
- if(msg.type === "private")
- this.sayPrivate(msg.uid, msg)
- else
- this.sayPublic(msg)
- };
- JdClient.prototype.sayPrivate = function(uid, msg) {
- this.socket.emit('chat', this.csrf, '/msg ' + uid + ' ' + msg.message);
- };
- JdClient.prototype.sayPublic = function(msg) {
- this.socket.emit("chat", this.csrf, msg.message)
- };
- async function login(credentials, url) {
- let jar = request.jar();
- let req = {
- url: url,
- jar: jar,
- form: {}
- }
- if (credentials.username) req.form.username = credentials.username;
- if (credentials.password) req.form.password = credentials.password;
- if (credentials.code) req.form.code = credentials.code;
- let res = yield request.post(req);
- let cookie = jar.getCookieString(url);
- return cookie
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement