Guest User

Untitled

a guest
Jul 23rd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.05 KB | None | 0 0
  1. /// <reference path="typings/tsd.d.ts" />
  2. var jsonfile = require('jsonfile');
  3. var file = "players.json";
  4. var jsonObj = jsonfile.readFileSync(file);
  5.  
  6. console.log(jsonObj.users[0].discordid);
  7.  
  8. var steam = require("steam"),
  9. util = require("util"),
  10. fs = require("fs"),
  11. crypto = require("crypto"),
  12. dota2 = require("./"),
  13. steamClient = new steam.SteamClient(),
  14. steamUser = new steam.SteamUser(steamClient),
  15. steamFriends = new steam.SteamFriends(steamClient),
  16. Dota2 = new dota2.Dota2Client(steamClient, true);
  17.  
  18. var Discord = require("discord.js");
  19. global.config = require("./config");
  20. var discordbot = new Discord.Client();
  21. discordbot.login(global.config.discord_email, global.config.discord_pass);
  22.  
  23. //Original user Input
  24. var input = "";
  25.  
  26. //The command that the user enters
  27. var command = "";
  28.  
  29. //An argument that goes with the command
  30. var argument = "";
  31.  
  32. //Boolean for if a player is registered or not
  33. var isRegistered = false;
  34.  
  35. //Whether a group is currently running
  36. var isGroupRunning = false;
  37.  
  38. //Array of the current players in the group
  39. var Group = [];
  40.  
  41. //The current hoster of the lobby
  42. var currentHoster = "";
  43.  
  44. //Max number of people in a given lobby
  45. var LobbySize = 10;
  46.  
  47. //Whether the game is in drafting stage or not
  48. var isDrafting = false;
  49.  
  50. //If the group is full or not
  51. var groupFull = false;
  52.  
  53. var version = "v1.3";
  54.  
  55. discordbot.on("message", function(message) {
  56. if(message.content.charAt(0) == "!"){
  57. input = message.content.substring(1,message.content.length).split(" ");
  58. command = input[0];
  59. argument = input[1];
  60.  
  61. //Uncomment these to test what has been entered
  62. //WriteToChannel(input);
  63. //WriteToChannel(command + " = command");
  64. //WriteToChannel(argumnt + " = argument");
  65.  
  66. //Lowers the command to lowercase so that !TEST is converted to !test
  67. command = command.toString().toLowerCase();
  68. //WriteToChannel(command + " = lowered command");
  69.  
  70. switch(command){
  71. case "register":
  72. if(isNaN(argument)){
  73. writeToChannel("Error your steam ID cannot be undefined. You can get your steamid at http://steamid.io/lookup");
  74. break;
  75. } else {
  76. register(argument, message);
  77. break;
  78. }
  79.  
  80. case "creategame":
  81. createGame(message);
  82. break;
  83.  
  84. case "join":
  85. join(message);
  86. break;
  87.  
  88. case "leave":
  89. leave(message);
  90. break;
  91.  
  92. case "listplayers":
  93. listPlayers();
  94. break;
  95.  
  96. case "pingplayers":
  97. pingPlayers(message);
  98. break;
  99.  
  100. case "help":
  101. showHelp(message, version);
  102. break;
  103.  
  104. case "disband":
  105. disband(message);
  106. break;
  107.  
  108. case "start":
  109. start(message);
  110. break;
  111.  
  112. default:
  113. unknownInput(message);
  114. break;
  115. }
  116. }
  117. });
  118.  
  119. //Writes messages to channel
  120. function writeToChannel(msg){
  121. discordbot.sendMessage(discordbot.channels.get("name", "inhouseorganising"), msg);
  122. }
  123.  
  124. //Default message when someone sends an unknown comamnd
  125. function unknownInput(){
  126. writeToChannel("Unknown command, private message me with !help for a full list.");
  127. }
  128.  
  129.  
  130. //Checks if the user is already registered up
  131. function checkSignup(id){
  132. var isSignedUp = false;
  133. for(var i=0, len = jsonObj.users.length; i<len;i++) {
  134. if(jsonObj.users[i].discordid == id) {
  135. isSignedUp = true;
  136. }
  137. }
  138. return isSignedUp;
  139. }
  140.  
  141. //Resets the global variables
  142. function resetGlobals(){
  143. isGroupRunning = false;
  144. currentHoster = "";
  145. Group = [];
  146. }
  147.  
  148. //Checks to see if the user is already in the group
  149. function checkGroup(id){
  150. var isGrouped = false;
  151. for(var i=0, len = Group.length; i<len;i++){
  152. if(Group[i] == id) {
  153. isGrouped = true;
  154. }
  155. }
  156. return isGrouped;
  157. }
  158.  
  159. //Creates an array of users called 'Group', contains only the leader for now
  160. function createGroup(user, id){
  161. isGroupRunning = true;
  162. Group = [];
  163. writeToChannel(user + " has created a new inhouse game! Do !join to get a slot");
  164. currentHoster = id;
  165. Group.push(id);
  166. }
  167.  
  168. //Function for registering new users
  169. function register(argument, message){
  170. if(argument.length == 17 && message.length == 2){
  171. isRegistered = false;
  172.  
  173. //Checks if the user is already registered, returns if they are
  174. for(var i=0, len = jsonObj.users.length; i<len;i++) {
  175. if(message.author.id == jsonObj.users[i].discordid){
  176. isRegistered = true;
  177. discordbot.reply(message, "you are already registered.");
  178. return;
  179. }
  180. }
  181.  
  182. //Registers them if not
  183. if(isRegistered == false){
  184. discordbot.reply(message, "Welcome! You have now been registered.");
  185. var discordid = message.author.id;
  186. var steamid = argument;
  187. var newuser = {discordid, steamid};
  188. jsonObj.users.push(newuser);
  189. jsonfile.writeFileSync(file, jsonObj);
  190. return;
  191. }
  192. } else {
  193. //Standard reply if they enter the wrong argument
  194. discordbot.reply(message, "Incorrect use, command should be !register 'steamid64'. You can get your steamid at http://steamid.io/lookup");
  195. return;
  196. }
  197. }
  198.  
  199. //Function for creating a virtual lobby that people join
  200. function createGame(message){
  201. if(checkSignup(message.author.id) == true){
  202. if(!isGroupRunning){
  203. createGroup(message.author.username, message.author.id);
  204. } else {
  205. writeToChannel("You can't make a game, there is already one running, try !join.");
  206. }
  207. } else {
  208. discordbot.reply(message, "you are not registered.");
  209. }
  210. }
  211.  
  212. //Function for joining the group
  213. function join(message){
  214. if(checkGroup(message.author.id)){
  215. writeToChannel(message.author.username + " you're already in the group mate.");
  216. return;
  217. }
  218. //Checks if a group is already running
  219. if(!isGroupRunning){
  220. writeToChannel(message.author.username + " there is no queue game currently running. Use !creategame to create one, or use !help to get a full list of commands.");
  221. return;
  222. } else {
  223. // if not, checks if they are signed up
  224. if(checkSignup(message.author.id) == true){
  225. //Checks to see if Group[] is less than the number of people in the lobby currently
  226. if(Group.length < LobbySize){
  227. //Adds them to group
  228. Group.push(message.author.id);
  229. writeToChannel(message.author.username + " joins the group! " + Group.length + "/" + LobbySize + " slots taken.");
  230. //Once the lobby is full, drafting begins
  231. if(Group.length == LobbySize){
  232. draftPhase();
  233. }
  234. } else {
  235. writeToChannel(message.author.username + " the group is currently full. Please try again later.");
  236. }
  237. } else {
  238. discordbot.reply(message, "you are not registered. Use !register 'steamid64'.");
  239. }
  240. return;
  241. }
  242. }
  243.  
  244. //Function which allows people to leave the group
  245. function leave(message){
  246. //Checks to see if a group is running
  247. if(!isGroupRunning){
  248. writeToChannel("No group is running at the moment, use !creategame to make one.");
  249. return;
  250. }
  251. //Checks to see if the person wanting to leave is the host
  252. if(message.author.id == currentHoster){
  253. writeToChannel("Lobby leader has left, lobby disbanding.");
  254. isGroupRunning = false;
  255. Group = [];
  256. return;
  257. } else {
  258. //Checks if the drafting phase is running, stops them if it is
  259. if(isDrafting == true){
  260. writeToChannel(message.author.username + " you cannot leave, the draft has begun.");
  261. return;
  262. }
  263. //If drafting hasn't begun, removes them from the group
  264. var index = Group.indexOf(message.author.id);
  265. if (index > -1) {
  266. discordbot.reply(message, "has left the lobby");
  267. Group.splice(index, 1);
  268. writeToChannel("Lobby is now: " + Group.length + "/" + LobbySize);
  269. }
  270. return;
  271. }
  272. }
  273.  
  274. //Function to loop through the current group and print everyone out, leader gets an extra icon
  275. function listPlayers(){
  276. if(!isGroupRunning){
  277. writeToChannel("No group is running at the moment, use !creategame to make one.");
  278. return;
  279. }
  280. var playerString = "";
  281. for(var i=0, len=Group.length; i<len;i++){
  282. if(Group[i] == currentHoster){
  283. playerString = playerString + (discordbot.users.get("id", Group[i]).username + " :trident: ") + "\n";
  284. } else {
  285. playerString = playerString + (discordbot.users.get("id", Group[i]).username) + "\n";
  286. }
  287. }
  288. writeToChannel(playerString);
  289. playerString = "";
  290. return;
  291. }
  292.  
  293. //Pings all current players in the group with an @ message
  294. function pingPlayers(message){
  295. //Will stop the user if they aren't the host
  296. if(isGroupRunning && message.author.id != currentHoster){
  297. writeToChannel("Only the party leader can ping the current players.");
  298. return;
  299. }
  300. if(!isGroupRunning){
  301. writeToChannel("No group is running at the moment, use !creategame to make one.");
  302. return;
  303. }
  304. var playerString = "";
  305. for(var i=0, len=Group.length; i<len;i++){
  306. if(Group[i] == currentHoster){
  307. playerString = playerString + (discordbot.users.get("id", Group[i]) + " :trident: ") + "\n";
  308. } else {
  309. playerString = playerString + (discordbot.users.get("id", Group[i])) + "\n";
  310. }
  311. }
  312. writeToChannel(playerString);
  313. playerString = "";
  314. return;
  315. }
  316.  
  317. //PM's a list of all commands to the user
  318. function showHelp(message, version){
  319. discordbot.sendMessage(message.author,
  320. " !register - Register your steamid\n" +
  321. " !creategame - Start a Inhouse Game \n" +
  322. " !join - Summon a magical unicorn from rainbow land not to join inhouses or anything like that\n" +
  323. " !leave - Leaves a lobby \n" +
  324. " !invite - Request a new lobby invite from the bot \n" +
  325. " !listplayers - Lists all the players who are currently in a game. :trident: means they created the lobby \n" +
  326. " !captain - Slots you as the captain of Team B \n" +
  327. " !pick - Picks a user for you team, only available to captains. Usage: !pick @USERNAME \n" +
  328. "----------------------------------------------------------------------------\n" +
  329. "--------------------Group leader specific commands-------------------\n" +
  330. "----------------------------------------------------------------------------\n" +
  331. " !pingplayers - Pings all current players in the pending lobby\n" +
  332. " !start - Starts the game\n" +
  333. " !disband - Pull a virtus.pro" +
  334. " \n\nCurrent version is: " + version);
  335. return;
  336. }
  337.  
  338. //Disbands the lobby if it is the leader
  339. function disband(message){
  340. if(message.author.id == currentHoster){
  341. writeToChannel(message.author.username + " has disbanded the inhouse party.");
  342. resetGlobals();
  343. return;
  344. } else {
  345. writeToChannel(message.author.username + " you are not the leader, you cannot disband.");
  346. return;
  347. }
  348. }
  349.  
  350. //Need to continue this
  351. function draftPhase(){
  352. isDrafting = true;
  353. writeToChannel("Group is now full, team drafting will begin soon.");
  354. return;
  355. }
  356.  
  357. //Starts the game if you are the lobby leader &
  358. function start(message){
  359. if(message.author.id == currentHoster && isDrafting == false){
  360. Dota2.launchPracticeLobby();
  361. Dota2.leavePracticeLobby();
  362. resetGlobals();
  363. writeToChannel("GLHF, bot is now ready for new group!");
  364. } else {
  365.  
  366. }
  367. }
  368. //function whatever the fuck comes next
  369.  
  370.  
  371. var onSteamLogOn = function onSteamLogOn(logonResp) {
  372. if (logonResp.eresult == steam.EResult.OK) {
  373. steamFriends.setPersonaState(steam.EPersonaState.Busy); // to display your steamClient's status as "Online"
  374. steamFriends.setPersonaName("EMSBot"); // to change its nickname
  375. util.log("Logged on.");
  376. Dota2.launch();
  377. Dota2.on("ready", function() {
  378. console.log("Node-dota2 ready.");
  379. steamFriends.on('message', function(source, message, type, chatter) {
  380. console.log('Received message: ' + message);
  381. var command = message.split(" ");
  382. switch (command[0]) {
  383. case "invite":
  384. Dota2.inviteToLobby(command[1]);
  385. break;
  386. case "leave":
  387. Dota2.leavePracticeLobby();
  388. break;
  389. }
  390. });
  391.  
  392. Dota2.on("chatMessage", function(channel, personaName, message) {
  393. Dota2.sendMessage(channel, "");});
  394. });
  395. Dota2.on("unready", function onUnready() {
  396. console.log("Node-dota2 unready.");
  397. });
  398. }
  399. },
  400. onSteamServers = function onSteamServers(servers) {
  401. util.log("Received servers.");
  402. fs.writeFile('servers', JSON.stringify(servers));
  403. },
  404. onSteamLogOff = function onSteamLogOff(eresult) {
  405. util.log("Logged off from Steam.");
  406. },
  407. onSteamError = function onSteamError(error) {
  408. util.log("Connection closed by server.");
  409. };
  410.  
  411. steamUser.on('updateMachineAuth', function(sentry, callback) {
  412. var hashedSentry = crypto.createHash('sha1').update(sentry.bytes).digest();
  413. fs.writeFileSync('sentry', hashedSentry);
  414. util.log("sentryfile saved");
  415. callback({
  416. sha_file: hashedSentry
  417. });
  418. });
  419.  
  420. // Login, only passing authCode if it exists
  421. var logOnDetails = {
  422. "account_name": global.config.steam_user,
  423. "password": global.config.steam_pass,
  424. };
  425. if (global.config.steam_guard_code) logOnDetails.auth_code = global.config.steam_guard_code;
  426.  
  427. try {
  428. var sentry = fs.readFileSync('sentry');
  429. if (sentry.length) logOnDetails.sha_sentryfile = sentry;
  430. } catch (beef) {
  431. util.log("Cannae load the sentry. " + beef);
  432. }
  433.  
  434. steamClient.connect();
  435. steamClient.on('connected', function() {
  436. steamUser.logOn(logOnDetails);
  437. });
  438. steamClient.on('logOnResponse', onSteamLogOn);
  439. steamClient.on('loggedOff', onSteamLogOff);
  440. steamClient.on('error', onSteamError);
  441. steamClient.on('servers', onSteamServers);
Add Comment
Please, Sign In to add comment