Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.98 KB | None | 0 0
  1. var Beam = require('beam-client-node');
  2. var BeamSocket = require('beam-client-node/lib/ws');
  3. var config = require('../config/config');
  4. var utils = require('./utils');
  5. var beam = new Beam();
  6.  
  7. var IDUser;
  8. var beamSockets = {};
  9. var beamIDs = {};
  10. var beamChannelNames = {};
  11. var Channel = require('mongoose').model('Channel');
  12. var Command = require('mongoose').model('Command');
  13. var User = require('mongoose').model('User');
  14. exports.connect = function() {
  15. beam.use('password', {
  16. username: config.rushbotUsername,
  17. password: config.rushbotPassword
  18. }).attempt().then(function(response) {
  19. IDUser = response.body.id;
  20. joinChannel('RushBot');
  21. connectToChannels();
  22. }).catch(function(err) {
  23. //If this is a failed request, don't log the entire request. Just log the body
  24. if (err.message !== undefined && err.message.body !== undefined) {
  25. err = err.message.body;
  26. }
  27. console.log('error joining chat:', err);
  28. });
  29. };
  30. function updateViewingPoints(channelID, channelName){
  31. console.log('Updating Viewing Points');
  32. beam.request('get', '/channels/' + channelID)
  33. .then(function(response) {
  34. if(!response.body.online){
  35. Channel.findOne({ beamID: channelID}, function(err, channel){
  36. if(!err){
  37. beam.chat.getUsers(channelID).then(function(data){
  38. for(var i = 0; i < data.body.length; i++){
  39. var userName = data.body[i].userName;
  40. User.findOne({channelID: channelID, username: userName}, function(err, user){
  41. if(!err){
  42. if(user){
  43. user.currency = user.currency + channel.viewGain;
  44. user.save();
  45.  
  46. }
  47. }
  48. });
  49. }
  50. });
  51. }
  52. });
  53. }
  54. });
  55.  
  56. }
  57. function joinChannel(channelName) {
  58. //we need the channel id.
  59. return beam.request('get', '/channels/' + channelName)
  60. .then(function(response) {
  61. beamIDs[channelName] = response.body.id;
  62. beamChannelNames[response.body.id] = channelName;
  63. return beam.chat.join(response.body.id);
  64. }).then(function(response) {
  65. var channelId = beamIDs[channelName];
  66. beamSockets[channelId] = new BeamSocket(response.body.endpoints).boot();
  67. beamSockets[channelId].on('ChatMessage', newMessage.bind(this, beamIDs[channelName]));
  68. beamSockets[channelId].on('UserJoin', userJoin.bind(this, beamIDs[channelName]));
  69. Channel.findOne({beamID: channelId}, function(err, channel){
  70. if(!err){
  71. if(channel){
  72. channel.viewTime = 1;
  73. channel.save();
  74. console.log((channel.viewTime * 60) * 1000);
  75. setInterval(updateViewingPoints, (channel.viewTime * 60) * 1000, channelId, channelName);
  76. }
  77.  
  78. }
  79. });
  80.  
  81. return beamSockets[channelId].call('auth', [beamIDs[channelName], IDUser, response.body.authkey])
  82. .then(function() {
  83. console.log("Connected to Beam channel: " + channelName);
  84. }).catch(function(err) {
  85. console.log(err);
  86. throw err;
  87. });
  88. //Move all in one to here
  89.  
  90. }).catch(function(err) {
  91. throw err;
  92. });
  93. }
  94.  
  95. function userJoin(channelId, data) {
  96. console.log('User ' + data.username + ' joined');
  97. User.findOne({
  98. username: data.username,
  99. channelID: channelId
  100. }, function(err, userObject) {
  101. if (!err) {
  102. if (!userObject) {
  103. var newUser = new User({
  104. username: data.username,
  105. channelID: channelId,
  106. beamID: data.id,
  107. });
  108. newUser.save();
  109. }
  110. else {
  111. }
  112. }
  113. else {
  114. throw err;
  115. }
  116. });
  117. }
  118.  
  119. function processCommand(message, args, username, user_roles, channelId, messageID, callback) {
  120. var shouldShout = false;
  121. if(message[0] == "!"){
  122. shouldShout = true;
  123. }else if(message[0] != "?"){
  124. beam.request('get', '/channels/' + channelId)
  125. .then(function(response) {
  126. if(response.body.online){
  127. Channel.findOne({
  128. beamID: channelId
  129. }, function(err, channel){
  130. if(!err){
  131. User.findOne({
  132. username: username,
  133. channelID: channelId
  134. }, function(err, user){
  135. if(!err){
  136. if(user){
  137. user.currency = user.currency + channel.chatGain;
  138. user.save();
  139.  
  140. }
  141. }
  142. });
  143. }
  144. });
  145.  
  146. }
  147. });
  148. return;
  149. }
  150. message = message.substr(1, message.length);
  151.  
  152. if (message.indexOf("addCommand") != -1 || message.indexOf("addCom") != -1) {
  153. if (user_roles.indexOf('Mod') != -1 || user_roles.indexOf('Owner') != -1) {
  154. if (args.length < 4) {
  155. callback({shout: shouldShout, message: 'Incorrect Usage: !addCommand <trigger> <role> <Response> ', channelID: channelId});
  156. }
  157. else {
  158. var trigger = args[1],
  159. role = args[2];
  160. args.splice(0, 3);
  161. var response = args.join(" ");
  162. Command.find({
  163. channelID: channelId,
  164. trigger: trigger
  165. }, function(err, foundCommand) {
  166. if (!err) {
  167. if (foundCommand) {
  168. var newCommand = new Command({
  169. channelID: channelId,
  170. trigger: trigger,
  171. response: response,
  172. role: role,
  173. });
  174. newCommand.save();
  175. callback({shout: shouldShout, message: 'Command ' + trigger + ' has been created', channelID: channelId});
  176. }
  177. else {
  178. callback({shout: shouldShout, message: 'A command with that trigger already exists!', channelID: channelId});
  179. }
  180. }
  181. });
  182.  
  183. }
  184.  
  185. }
  186.  
  187. }
  188. else if (message.indexOf("removeCommand") != -1 || message.indexOf("removeCom") != -1) {
  189. if (user_roles.indexOf('Mod') != -1 || user_roles.indexOf('Owner') != -1) {
  190. if (args.length < 2) {
  191. callback( {shout: shouldShout, message: 'Incorrect Usage: !removeCommand <trigger>', channelID: channelId});
  192. }
  193. else {
  194. Command.findOneAndRemove({
  195. channelID: channelId,
  196. trigger: args[1],
  197. }, function(err) {
  198. if (!err) {
  199. callback( {shout: shouldShout, message: 'Removed the command ' + args[1], channelID: channelId});
  200. }
  201. else {
  202. callback( {shout: shouldShout, message: 'No command with that trigger', channelID: channelId});
  203. }
  204. });
  205.  
  206. }
  207.  
  208. }
  209.  
  210. }
  211. else if (message == "join") {
  212.  
  213. beam.request('get', '/channels/' + username).then(function(response) {
  214. if (response.body.id in beamSockets) {
  215. callback( {shout: shouldShout, message: 'Oh no! Im already in your channel!', channelID: channelId});
  216. }
  217. else {
  218. var mewChannel = new Channel({
  219. name: username,
  220. botName: 'RushBot',
  221. beamID: response.body.id,
  222. });
  223. mewChannel.save(function(err) {
  224. if (!err) {
  225. joinChannel(username).then(function(err) {
  226. if (!err) {
  227. 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});
  228. }
  229.  
  230. });
  231.  
  232. }
  233. else {
  234. throw err;
  235. }
  236. });
  237.  
  238. }
  239. });
  240.  
  241. }
  242. else if (message == "part") {
  243. if (user_roles.indexOf('Owner') != -1) {
  244. beam.request('get', '/channels/' + username).then(function(response) {
  245. Channel.findOneAndRemove({
  246. name: username,
  247. beamID: response.body.id,
  248. }, function(err) {
  249. if (!err) {
  250. callback( {shout: shouldShout, message: 'Goodbye!', channelID: channelId});
  251. }
  252. });
  253. });
  254.  
  255. }
  256. }
  257. else if (message.indexOf("bal") != -1) {
  258. if (args.length == 1) {
  259. Channel.findOne({
  260. beamID: channelId
  261. }, function(err, channel) {
  262. if (!err) {
  263. User.findOne({
  264. username: username,
  265. channelID: channelId
  266. }, function(err, user) {
  267. if (!err) {
  268. callback( {shout: shouldShout, message: '@' + username + ' you have ' + user.currency + ' ' + channel.currenyName, channelID: channelId});
  269. }
  270. });
  271. }
  272. });
  273. }else{
  274. Channel.findOne({
  275. beamID: channelId
  276. }, function(err, channel) {
  277. if (!err) {
  278. User.findOne({
  279. username: args[1],
  280. channelID: channelId
  281. }, function(err, user) {
  282. if (!err) {
  283. callback( {shout: shouldShout, message: '@' + username + ' ' + args[1] + ' has ' + user.currency + ' ' + channel.currenyName, channelID: channelId});
  284.  
  285. }
  286. });
  287. }
  288. });
  289. }
  290. }else{
  291. Command.findOne({
  292. channelID: channelId,
  293. trigger: message
  294. }, function(err, command) {
  295. if (!err) {
  296. if (command) {
  297. if (user_roles.indexOf(command.role) != -1 || user_roles.indexOf("Owner") != -1) {
  298. callback( {shout: shouldShout, message: command.response, channelID: channelId});
  299. }
  300. }
  301. }
  302. });
  303. }
  304.  
  305. }
  306.  
  307. function newMessage(channelId, data) {
  308. var message = utils.flattenBeamMessage(data.message.message);
  309. var username = data.user_name;
  310. var user_roles = data.user_roles;
  311. var args = message.split(" ");
  312. var messageID = data.id;
  313. processCommand(message, args, username, user_roles, channelId, messageID, function(response){
  314. if(!response){
  315.  
  316. }else if(response.shout){
  317. beamSockets[response.channelID].call('msg', [response.message]);
  318. }
  319. else{
  320. beamSockets[response.channelID].call('whisper', [username, response.message]);
  321. }
  322.  
  323. });
  324.  
  325.  
  326.  
  327.  
  328.  
  329. }
  330.  
  331. function connectToChannels() {
  332.  
  333. Channel.find({}, function(err, channel) {
  334. if (!err) {
  335. for (var i = 0; i < channel.length; i++) {
  336. joinChannel(channel[i].name);
  337. }
  338. }
  339. });
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement