Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Beam = require('beam-client-node');
- var BeamSocket = require('beam-client-node/lib/ws');
- var config = require('../config/config');
- var utils = require('./utils');
- var beam = new Beam();
- var IDUser;
- var beamSockets = {};
- var beamIDs = {};
- var beamChannelNames = {};
- var Channel = require('mongoose').model('Channel');
- var Command = require('mongoose').model('Command');
- var User = require('mongoose').model('User');
- exports.connect = function() {
- beam.use('password', {
- username: config.rushbotUsername,
- password: config.rushbotPassword
- }).attempt().then(function(response) {
- IDUser = response.body.id;
- joinChannel('RushBot');
- connectToChannels();
- }).catch(function(err) {
- //If this is a failed request, don't log the entire request. Just log the body
- if (err.message !== undefined && err.message.body !== undefined) {
- err = err.message.body;
- }
- console.log('error joining chat:', err);
- });
- };
- function updateViewingPoints(channelID, channelName){
- console.log('Updating Viewing Points');
- beam.request('get', '/channels/' + channelID)
- .then(function(response) {
- if(!response.body.online){
- Channel.findOne({ beamID: channelID}, function(err, channel){
- if(!err){
- beam.chat.getUsers(channelID).then(function(data){
- for(var i = 0; i < data.body.length; i++){
- var userName = data.body[i].userName;
- User.findOne({channelID: channelID, username: userName}, function(err, user){
- if(!err){
- if(user){
- user.currency = user.currency + channel.viewGain;
- user.save();
- }
- }
- });
- }
- });
- }
- });
- }
- });
- }
- function joinChannel(channelName) {
- //we need the channel id.
- return beam.request('get', '/channels/' + channelName)
- .then(function(response) {
- beamIDs[channelName] = response.body.id;
- beamChannelNames[response.body.id] = channelName;
- return beam.chat.join(response.body.id);
- }).then(function(response) {
- var channelId = beamIDs[channelName];
- beamSockets[channelId] = new BeamSocket(response.body.endpoints).boot();
- beamSockets[channelId].on('ChatMessage', newMessage.bind(this, beamIDs[channelName]));
- beamSockets[channelId].on('UserJoin', userJoin.bind(this, beamIDs[channelName]));
- Channel.findOne({beamID: channelId}, function(err, channel){
- if(!err){
- if(channel){
- channel.viewTime = 1;
- channel.save();
- console.log((channel.viewTime * 60) * 1000);
- setInterval(updateViewingPoints, (channel.viewTime * 60) * 1000, channelId, channelName);
- }
- }
- });
- return beamSockets[channelId].call('auth', [beamIDs[channelName], IDUser, response.body.authkey])
- .then(function() {
- console.log("Connected to Beam channel: " + channelName);
- }).catch(function(err) {
- console.log(err);
- throw err;
- });
- //Move all in one to here
- }).catch(function(err) {
- throw err;
- });
- }
- function userJoin(channelId, data) {
- console.log('User ' + data.username + ' joined');
- User.findOne({
- username: data.username,
- channelID: channelId
- }, function(err, userObject) {
- if (!err) {
- if (!userObject) {
- var newUser = new User({
- username: data.username,
- channelID: channelId,
- beamID: data.id,
- });
- newUser.save();
- }
- else {
- }
- }
- else {
- throw err;
- }
- });
- }
- function processCommand(message, args, username, user_roles, channelId, messageID, callback) {
- var shouldShout = false;
- if(message[0] == "!"){
- shouldShout = true;
- }else if(message[0] != "?"){
- beam.request('get', '/channels/' + channelId)
- .then(function(response) {
- if(response.body.online){
- Channel.findOne({
- beamID: channelId
- }, function(err, channel){
- if(!err){
- User.findOne({
- username: username,
- channelID: channelId
- }, function(err, user){
- if(!err){
- if(user){
- user.currency = user.currency + channel.chatGain;
- user.save();
- }
- }
- });
- }
- });
- }
- });
- return;
- }
- message = message.substr(1, message.length);
- if (message.indexOf("addCommand") != -1 || message.indexOf("addCom") != -1) {
- if (user_roles.indexOf('Mod') != -1 || user_roles.indexOf('Owner') != -1) {
- if (args.length < 4) {
- callback({shout: shouldShout, message: 'Incorrect Usage: !addCommand <trigger> <role> <Response> ', channelID: channelId});
- }
- else {
- var trigger = args[1],
- role = args[2];
- args.splice(0, 3);
- var response = args.join(" ");
- Command.find({
- channelID: channelId,
- trigger: trigger
- }, function(err, foundCommand) {
- if (!err) {
- if (foundCommand) {
- var newCommand = new Command({
- channelID: channelId,
- trigger: trigger,
- response: response,
- role: role,
- });
- newCommand.save();
- callback({shout: shouldShout, message: 'Command ' + trigger + ' has been created', channelID: channelId});
- }
- else {
- callback({shout: shouldShout, message: 'A command with that trigger already exists!', channelID: channelId});
- }
- }
- });
- }
- }
- }
- else if (message.indexOf("removeCommand") != -1 || message.indexOf("removeCom") != -1) {
- if (user_roles.indexOf('Mod') != -1 || user_roles.indexOf('Owner') != -1) {
- if (args.length < 2) {
- callback( {shout: shouldShout, message: 'Incorrect Usage: !removeCommand <trigger>', channelID: channelId});
- }
- else {
- Command.findOneAndRemove({
- channelID: channelId,
- trigger: args[1],
- }, function(err) {
- if (!err) {
- callback( {shout: shouldShout, message: 'Removed the command ' + args[1], channelID: channelId});
- }
- else {
- callback( {shout: shouldShout, message: 'No command with that trigger', channelID: channelId});
- }
- });
- }
- }
- }
- else if (message == "join") {
- beam.request('get', '/channels/' + username).then(function(response) {
- if (response.body.id in beamSockets) {
- callback( {shout: shouldShout, message: 'Oh no! Im already in your channel!', channelID: channelId});
- }
- else {
- var mewChannel = new Channel({
- name: username,
- botName: 'RushBot',
- beamID: response.body.id,
- });
- mewChannel.save(function(err) {
- if (!err) {
- joinChannel(username).then(function(err) {
- if (!err) {
- callback( {shout: shouldShout, message: 'Hello! My name is RushBot and im here to help you out. Please make me a mod so i can do the tasks!', channelID: channelId});
- }
- });
- }
- else {
- throw err;
- }
- });
- }
- });
- }
- else if (message == "part") {
- if (user_roles.indexOf('Owner') != -1) {
- beam.request('get', '/channels/' + username).then(function(response) {
- Channel.findOneAndRemove({
- name: username,
- beamID: response.body.id,
- }, function(err) {
- if (!err) {
- callback( {shout: shouldShout, message: 'Goodbye!', channelID: channelId});
- }
- });
- });
- }
- }
- else if (message.indexOf("bal") != -1) {
- if (args.length == 1) {
- Channel.findOne({
- beamID: channelId
- }, function(err, channel) {
- if (!err) {
- User.findOne({
- username: username,
- channelID: channelId
- }, function(err, user) {
- if (!err) {
- callback( {shout: shouldShout, message: '@' + username + ' you have ' + user.currency + ' ' + channel.currenyName, channelID: channelId});
- }
- });
- }
- });
- }else{
- Channel.findOne({
- beamID: channelId
- }, function(err, channel) {
- if (!err) {
- User.findOne({
- username: args[1],
- channelID: channelId
- }, function(err, user) {
- if (!err) {
- callback( {shout: shouldShout, message: '@' + username + ' ' + args[1] + ' has ' + user.currency + ' ' + channel.currenyName, channelID: channelId});
- }
- });
- }
- });
- }
- }else{
- Command.findOne({
- channelID: channelId,
- trigger: message
- }, function(err, command) {
- if (!err) {
- if (command) {
- if (user_roles.indexOf(command.role) != -1 || user_roles.indexOf("Owner") != -1) {
- callback( {shout: shouldShout, message: command.response, channelID: channelId});
- }
- }
- }
- });
- }
- }
- function newMessage(channelId, data) {
- var message = utils.flattenBeamMessage(data.message.message);
- var username = data.user_name;
- var user_roles = data.user_roles;
- var args = message.split(" ");
- var messageID = data.id;
- processCommand(message, args, username, user_roles, channelId, messageID, function(response){
- if(!response){
- }else if(response.shout){
- beamSockets[response.channelID].call('msg', [response.message]);
- }
- else{
- beamSockets[response.channelID].call('whisper', [username, response.message]);
- }
- });
- }
- function connectToChannels() {
- Channel.find({}, function(err, channel) {
- if (!err) {
- for (var i = 0; i < channel.length; i++) {
- joinChannel(channel[i].name);
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement