Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const md5 = require('md5');
- const SteamID = require('steamid');
- const Broadcaster = function(redisClient, broadcastChannel, APP_KEY) {
- const that = this;
- this.redisClient = redisClient;
- this.APP_KEY = APP_KEY || "";
- this.broadcastChannel = broadcastChannel || 'private-broadcast-channel';
- this.lastUser = false;
- };
- module.exports = Broadcaster;
- var prototype = Broadcaster.prototype;
- /**
- * Converts Steamid, accountid, etc to Broadcast channel.
- * @param user e.g STEAM_0:0:NNNNNN or ACCOUNT_ID or Secure:EncryptedChannel
- * @returns {*}
- */
- prototype.GetUserBroadcastChannel = function GetUserBroadcastChannel(user) {
- var channel = false;
- if (typeof user == "number")
- user = user.toString();
- // check if ths is a secure channel
- if (typeof user == "string" && user.indexOf('Secure:') != -1)
- channel = user;
- else
- {
- var _steam = new SteamID(user);
- if (_steam.isValid())
- {
- var steam64 = _steam.getSteamID64();
- var encrypted = md5( md5(steam64) +''+ this.APP_KEY );
- channel = 'Secure:'+encrypted;
- }
- else
- {
- var sid = new SteamID();
- sid.universe = SteamID.Universe.PUBLIC;
- sid.type = SteamID.Type.INDIVIDUAL;
- sid.instance = SteamID.Instance.DESKTOP;
- sid.accountid = user;
- if (sid.isValid()) {
- var steam64 = sid.getSteam2RenderedID();
- var _steam = new SteamID(steam64);
- steam64 = _steam.getSteamID64();
- var encrypted = md5( md5(steam64) + '' + this.APP_KEY);
- channel = 'Secure:' + encrypted;
- }
- }
- }
- return channel;
- };
- /**
- * Plays a sound effect on users browser.
- * @param user SteamID || AccountID || Secure Channel
- * @param effect Effect Name, or file.
- * @param Settings Object { Volume: 0.1, Repeat: 0, Length: N }
- * @constructor
- */
- prototype.soundfx = function soundfx(user, effect, Settings ) {
- var event = 'PlayAudioEffect';
- //if (typeof Settings == undefined)
- // var Settings = {};
- //
- var extraSettings = Settings || {};
- if (typeof payload != "object")
- payload = {Sound: effect, Settings: extraSettings };
- else
- {
- payload.Sound = effect;
- payload.Settings = extraSettings;
- }
- return this.send(user, event, payload);
- };
- /**
- * Reason we are all here, utility to broadcast.
- * @param user
- * @param event
- * @param payload
- * @returns {boolean}
- * @constructor
- */
- prototype.send = function send(user, event, payload) {
- this.lastUser = user;
- var BroadcastTo = this.GetUserBroadcastChannel(user);
- if (!BroadcastTo)
- {
- console.log("[BROADCAST ERROR] Error trying to broadcast to "+user+" - Cannot obtain Secure channel from input.");
- return false;
- }
- payload.BroadcastTo = BroadcastTo;
- if (event.indexOf('App\\Events\\') == -1)
- event = 'App\\Events\\'+event;
- var payloadC = {'event': event,
- 'data': payload };
- var payload_json = JSON.stringify(payloadC);
- this.redisClient.publish(this.broadcastChannel, payload_json );
- 205
- return this;
- };
- /**
- * Sends a sound to the last user, useful with method chaining.
- * @param effect
- * @param options
- */
- prototype.withSound = function withSound(effect, options)
- {
- return this.soundfx(this.lastUser, effect, options);
- };
- /**
- * Sends status message event
- * @param user - Steam ID or any identifable user.
- * @param message Message to send
- * @param className info|warning|error|default|success
- * @returns {boolean}
- */
- prototype.statusMsg = function status(user, message, className)
- {
- var payload = { Status: message, Type: className || 'default' };
- return this.send(user, 'Steam_User_Items_Status', payload);
- };
- prototype.bot = function(username, event, payload)
- {
- var BroadcastTo = "bot-"+username;
- if (!BroadcastTo)
- {
- console.log("[BROADCAST ERROR] Error trying to broadcast to "+user+" - Cannot obtain Secure channel from input.");
- return false;
- }
- if (event.indexOf('App\\Events\\') == -1)
- event = 'App\\Events\\'+event;
- var payloadC = {'event': event,
- 'data': payload };
- var payload_json = JSON.stringify(payloadC);
- return this.redisClient.publish(BroadcastTo, payload_json );
- };
- prototype.irc = function (event, payload)
- {
- var BroadcastTo = "bot-irc";
- if (!BroadcastTo)
- {
- console.log("[BROADCAST ERROR] Error trying to broadcast to "+user+" - Cannot obtain Secure channel from input.");
- return false;
- }
- console.log("IRC> "+event+" with payload: "+payload);
- if (event.indexOf('App\\Events\\') == -1)
- event = 'App\\Events\\'+event;
- console.log("IRC> "+event+" with payload: "+payload);
- var payloadC = {'event': event,
- 'data': payload };
- var payload_json = JSON.stringify(payloadC);
- this.redisClient.publish(BroadcastTo, payload_json );
- return this;
- };
- prototype.irc_steambotsay = function(bot, message)
- {
- if (typeof bot == "object" && typeof bot.username != "undefined") bot = bot.username;
- else if (typeof bot != "string") bot = "["+typeof bot+"] undefined";
- return this.irc("SteamBotSay", {nick:bot,
- response:message}
- );
- };
- prototype.alert = function (user, message, type, options)
- {
- if (typeof options == 'undefined')
- options = {timeout: 15000, layout: "topCenter", 'theme': 'bootstrapTheme', animation: {open: 'shake', close: 'hinge'}};
- // Alert, Success, Error, Warning, Information, Confirm
- if (type.indexOf('alert') == -1 && type.indexOf('error') == -1 && type.indexOf('warning') == -1 && type.indexOf('information') == -1) {
- type = 'information';
- }
- var payload = {
- Message: message,
- Type: type || "information",
- Options: {
- timeout: options.timeout || 15000,
- modal: false, // not implimented
- layout: options.layout || "topCenter",
- theme: options.theme || "bootstrapTheme",
- animation: {
- open: options.animation.open || "shake",
- close: options.animation.close || "hinge"
- }
- }
- };
- return this.send(user, "Alert", payload );
- };
- //BroadcastToUser('STEAM_0:0:6771510', 'Steam_User_Items_Status', {Status: 'Broadcast by SteamID [STEAM_0:0:6771510] - Testing!', Type: 'error'});
- //
- //setTimeout(() => {
- // BroadcastToUser('Secure:5a831e69aaa1ed381d4f545d0a7e4689', 'Steam_User_Items_Status', {Status: 'Broadcast via Channel [Secure:5a831e69aaa1ed381d4f545d0a7e4689]', Type: 'warning'});
- //}, 2000);
- //
- //setTimeout(() => {
- // BroadcastToUser('13543020', 'Steam_User_Items_Status', {Status: 'Broadcast by account ID [13543020]', Type: 'warning'});
- //}, 3000);
- //
- //setTimeout(() => {
- // BroadcastToUser('[U:1:13543020]', 'Steam_User_Items_Status', {Status: 'Broadcast by account ID [U:1:267645796]', Type: 'error'});
- //}, 7000);
- //var channel = 'private-broadcast-channel';
- //
- //var payload = {'event': 'App\\Events\\Steam_User_Items_Status',
- // 'data': {
- // BroadcastTo: 'Secure:5a831e69aaa1ed381d4f545d0a7e4689',
- // type: 'success', Status: 'Testing Broadcast System 1. 2. 3.' } };
- //
- //var payload = JSON.stringify(payload);
- //redisclient.publish(channel, payload);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement