Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.03 KB | None | 0 0
  1. const Discord = require('discord.js');
  2. const bot = new Discord.Client();
  3. const mysql = require('mysql');
  4. const settings = require('./settings.json');
  5. const schedule = require('node-schedule');
  6. const nconf = require('nconf');
  7. const Moment = require('moment-timezone');
  8. const fs = require("fs");
  9. const hound = require('hound');
  10. const Table = require('cli-table2');
  11.  
  12. //- Connection configuration
  13. var db_config = {
  14. host: '79.170.44.148',
  15. user: 'cl34-idoc',
  16. password: 'bD-hn.E3X',
  17. database: 'cl34-idoc'
  18. };
  19.  
  20. //- Create the connection variable
  21. var con = mysql.createConnection(db_config);
  22.  
  23. //- Establish a new connection
  24. con.connect(function(err){
  25. if(err) {
  26. // mysqlErrorHandling(connection, err);
  27. console.log("\n\t *** Cannot establish a connection with the database. ***");
  28. con = reconnect(con);
  29. }else {
  30. console.log("\n\t *** New connection established with the database. ***")
  31. bot.login(settings.token);
  32. }
  33. });
  34.  
  35. //- Reconnection function
  36. function reconnect(con){
  37. console.log("\n New connection tentative...");
  38. //- Destroy the current connection variable
  39. if(con) con.destroy();
  40. //- Create a new one
  41. var con = mysql.createConnection(db_config);
  42. //- Try to reconnect
  43. con.connect(function(err){
  44. if(err) {
  45. //- Try to connect every 2 seconds.
  46. setTimeout(reconnect, 2000);
  47. }else {
  48. console.log("\n\t *** New connection established with the database. ***")
  49. bot.login(settings.token);
  50. return con;
  51. }
  52. });
  53. }
  54.  
  55. //- Error listener
  56. con.on('error', function(err) {
  57. //- The server close the connection.
  58. if(err.code === "PROTOCOL_CONNECTION_LOST"){
  59. console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
  60. con = reconnect(con);
  61. }
  62. //- Connection in closing
  63. else if(err.code === "PROTOCOL_ENQUEUE_AFTER_QUIT"){
  64. console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
  65. con = reconnect(con);
  66. }
  67. //- Fatal error : connection variable must be recreated
  68. else if(err.code === "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR"){
  69. console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
  70. con = reconnect(con);
  71. }
  72. //- Error because a connection is already being established
  73. else if(err.code === "PROTOCOL_ENQUEUE_HANDSHAKE_TWICE"){
  74. console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
  75. }
  76. //- Anything else
  77. else{
  78. console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
  79. con = reconnect(con);
  80. }
  81. });
  82.  
  83. bot.on('ready', () => {
  84. console.log('IDOC Bot Online');
  85. fs.readFile('./config.json', 'utf8', function (err,data) {
  86. data = JSON.parse(data);
  87. for(var i = 0; i < data.length; i++) {
  88. //var idocLocation = new idocLocation();
  89. Sector = data[i].Sector;
  90. SectorStatus = data[i].SectorStatus;
  91. SectorRunner = data[i].SectorRunner;
  92. SectorTime = Moment(data[i].SectorTime);
  93. SectorTimeTZ = SectorTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  94. //save(function (err) {});
  95. console.log(`Sector: ` + Sector + ` Sector Status: ` + SectorStatus + ` -- Sector Runner: ` + SectorRunner + ` -- Sector Time: ` + SectorTimeTZ);
  96. }
  97. });
  98. bot.user.setGame("IDOC Manager Online...");
  99. });
  100.  
  101. //prefix declaration
  102. var prefix = "!";
  103.  
  104. bot.on('message', (message) => {
  105. let args = message.content.split(' ').slice(1);
  106. var argresult = args.join(' ');
  107. if(!message.content.startsWith(prefix)) return;
  108. if(message.author.bot) return;
  109. if(message.content.startsWith(prefix + 'IDOCStart')) {
  110. //!IDOCSectorStart 01
  111. let args = message.content.split(" ").slice(1);
  112. //message.reply(' is running sector ' + args[0]);
  113. if (args > 0 && args < 24) {
  114. fs.readFile('./config.json',{encoding: 'utf8'},function(err,data) {
  115. var data = JSON.parse(data);
  116. var date = new Date();
  117. var i = args[0] - 1;
  118. data[i].SectorStatus = "Running";
  119. data[i].SectorRunner = message.author.username;
  120. data[i].SectorTime = date;
  121. sT = Moment(date);
  122. sTTZ = sT.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  123. console.log(data[i].SectorStatus + " " + data[i].SectorRunner + " " + sTTZ);
  124. message.channel.sendMessage(data[i].SectorRunner+ ' is running sector ' + "Sector: " + args[0] + " @ " + sTTZ );
  125. data.push();
  126. var dataJSON = JSON.stringify(data, null, 2);
  127. fs.writeFileSync('./config.json', dataJSON);
  128. });
  129. }
  130.  
  131. } else
  132.  
  133. if(message.content.startsWith(prefix + 'IDOCEnd')) {
  134. //!IDOCSectorEnd 1
  135. let args = message.content.split(" ").slice(1);
  136. //message.reply(' is running sector ' + args[0]);
  137. if (args > 0 && args < 24) {
  138.  
  139. fs.readFile('./config.json',{encoding: 'utf8'},function(err,data) {
  140. var data = JSON.parse(data);
  141. var date = new Date();
  142. var i = args[0] - 1;
  143. data[i].SectorStatus = "Completed";
  144. data[i].SectorRunner = message.author.username;
  145. data[i].SectorTime = date;
  146. sT = Moment(date);
  147. sTTZ = sT.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  148. console.log(data[i].SectorStatus + " " + data[i].SectorRunner + " " + sTTZ);
  149. message.channel.sendMessage(data[i].SectorRunner+ ' completed running sector ' + "Sector: " + args[0] + " @ " + sTTZ );
  150. data.push();
  151. var dataJSON = JSON.stringify(data, null, 2);
  152. fs.writeFileSync('./config.json', dataJSON);
  153.  
  154. });
  155. }
  156.  
  157. } else
  158.  
  159. if(message.content.startsWith(prefix + 'IDOCTimer')) {
  160.  
  161. message.channel.sendMessage(message.author.username + " is Timing location : ");
  162. let args = message.content.split(" ").slice(1);
  163. //message.reply(' is running sector ' + args[0]);
  164. if (args > 0) {
  165.  
  166. fs.readFile('./config.json',{encoding: 'utf8'},function(err,data) {
  167. var data = JSON.parse(data);
  168. var date = new Date();
  169. var i = args[0] - 1;
  170. data[i].SectorStatus = "Completed";
  171. data[i].SectorRunner = message.author.username;
  172. data[i].SectorTime = date;
  173. sT = Moment(date);
  174. sTTZ = sT.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  175. console.log(data[i].SectorStatus + " " + data[i].SectorRunner + " " + sTTZ);
  176. message.channel.sendMessage(data[i].SectorRunner+ ' completed running sector ' + "Sector: " + args[0] + " @ " + sTTZ );
  177. data.push();
  178. var dataJSON = JSON.stringify(data, null, 2);
  179. fs.writeFileSync('./config.json', dataJSON);
  180.  
  181. });
  182. }
  183.  
  184. } else
  185.  
  186. if(message.content.startsWith(prefix + 'IDOC')) {
  187.  
  188. let args = message.content.split(" ").slice(1);
  189.  
  190. if (args == 'fairly') {
  191. message.channel.sendMessage('finding fairly worns....' );
  192. con.query('SELECT id, name, rune, cs, blind, timername, windowstart, image FROM idocs WHERE cs = 1 AND DATE(NOW()) = DATE(windowstart)',function(err,rows){
  193. if(err) throw err;
  194. if (rows.length > 0) {
  195. for(var i = 0; i < rows.length; i++) {
  196. IDOCTime = Moment(rows[i].windowstart);
  197. blindtext = rows[i].blind;
  198.  
  199. if (rows[i].blind == 1) {
  200. blindtext = "Yes";
  201. } else {
  202. blindtext = "No";
  203. }
  204.  
  205. IDOCTimeTZ = IDOCTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  206.  
  207. message.channel.sendMessage('Fairly Status: ' + rows[i].id
  208. + ' - Finder:' + rows[i].name
  209. + ' - Rune: ' + rows[i].rune
  210. + ' - Timer: ' + rows[i].timername
  211. + ' - Change Time: ' + IDOCTimeTZ
  212. + ' - Blind: ' + blindtext
  213. + ' - ' + rows[i].image);
  214. }
  215. }
  216. });
  217.  
  218. } else
  219.  
  220. if (args == 'greatly') {
  221.  
  222. message.channel.sendMessage('finding greatly worns....' );
  223. con.query('SELECT id, name, rune, cs, blind, timername, windowstart, image FROM idocs WHERE cs = 2 AND DATE(NOW()) = DATE(windowstart)',function(err,rows){
  224. if(err) throw err;
  225. if (rows.length > 0) {
  226. for(var i = 0; i < rows.length; i++) {
  227. IDOCTime = Moment(rows[i].windowstart);
  228. IDOCTimeTZ = IDOCTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  229. blindtext = rows[i].blind;
  230.  
  231. if (rows[i].blind == 1) {
  232. blindtext = "Yes";
  233. } else {
  234. blindtext = "No";
  235. }
  236.  
  237. message.channel.sendMessage('Greatly Status: ' + rows[i].id
  238. + ' - Finder:' + rows[i].name
  239. + ' - Rune: ' + rows[i].rune
  240. + ' - Timer: ' + rows[i].timername
  241. + ' - Change Time: ' + IDOCTimeTZ
  242. + ' - Blind: ' + blindtext
  243. + ' - ' + rows[i].image);
  244. }
  245. }
  246. });
  247. } else
  248.  
  249. if (args == 'today') {
  250.  
  251. message.channel.sendMessage('finding todays IDOCs....' );
  252. con.query('SELECT id, name, rune, cs, blind, timername, windowstart, image FROM idocs WHERE cs = 3 AND DATE(NOW()) = DATE(windowstart)',function(err,rows){
  253. if(err) throw err;
  254. if (rows.length > 0) {
  255. for(var i = 0; i < rows.length; i++) {
  256. IDOCTime = Moment(rows[i].windowstart);
  257. IDOCTimeTZ = IDOCTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  258. blindtext = rows[i].blind;
  259.  
  260. if (rows[i].blind == 1) {
  261. blindtext = "Yes";
  262. } else {
  263. blindtext = "No";
  264. }
  265.  
  266. message.channel.sendMessage('IDOC Info: ' + rows[i].id
  267. + ' - Finder:' + rows[i].name
  268. + ' - Rune: ' + rows[i].rune
  269. + ' - Timer: ' + rows[i].timername
  270. + ' - Change Time: ' + IDOCTimeTZ
  271. + ' - Blind: ' + blindtext
  272. + ' - ' + rows[i].image);
  273. }
  274. }
  275. });
  276.  
  277. } else
  278.  
  279. if (args == 'needtimed') {
  280.  
  281. } else
  282.  
  283. if (args == 'needtimed') {
  284.  
  285. } else
  286.  
  287. if (args == 'needtimed') {
  288.  
  289. } else
  290.  
  291. if (args == 'sectors') {
  292.  
  293. message.channel.sendMessage('IDOC Bot is working!');
  294. console.log('IDOC Bot is working!');
  295. var dateStatus = new Date();
  296. dateStatusTime = Moment(dateStatus);
  297. dateStatusTimeTZ = dateStatusTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  298. console.log(message.author.username, message.author.id, ` -- Time: ` + dateStatusTimeTZ);
  299. fs.readFile('./config.json', 'utf8', function (err,data) {
  300. data = JSON.parse(data);
  301.  
  302. // instantiate
  303. var table = new Table({
  304. head: ['Sectors', 'Status', 'Runner', 'Time']
  305. });
  306.  
  307. for(var i = 0; i < data.length; i++) {
  308. //var idocLocation = new idocLocation();
  309. Sector = data[i].Sector;
  310. SectorStatus = data[i].SectorStatus;
  311. SectorRunner = data[i].SectorRunner;
  312. SectorTime = Moment(data[i].SectorTime);
  313. SectorTimeTZ = SectorTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
  314. // table is an Array, so you can `push`, `unshift`, `splice` and friends
  315.  
  316. table.push(
  317. [Sector, SectorStatus, SectorRunner , SectorTimeTZ]
  318. );
  319. // message.channel.sendMessage(`Sector: ` + Sector + ` Sector Status: ` + SectorStatus + ` -- Sector Runner: ` + SectorRunner + ` -- Sector Time: ` + SectorTimeTZ + "\n");
  320. }
  321. console.log(table.toString());
  322. //message.channel.sendMessage("test table goes here");
  323. message.channel.sendMessage(table);
  324. });
  325.  
  326. }else
  327.  
  328. if (args == 'help') {
  329. message.channel.sendMessage("!IDOC today - Lists all In danger of collapse today with time");
  330. message.channel.sendMessage("!IDOC greatly - Lists all current locations with detailed info and picture (In Testing)");
  331. message.channel.sendMessage("!IDOC fairly - Lists all current locations with detailed info and picture (In Testing)");
  332. message.channel.sendMessage("!IDOCStart X where X is the sector you are running");
  333. message.channel.sendMessage("!IDOCEnd X where X is the sector you have finished running");
  334. message.channel.sendMessage("!IDOC sectors - Displays list of all Sectors current Status");
  335. message.channel.sendMessage("!IDOCTimer X - Where X is the id of the location you are timing if not already timed (In Testing)");
  336. message.channel.sendMessage("!IDOC needtimed - Lists all locations needing timed (In Testing)");
  337. message.channel.sendMessage("RDA [name of RDA]");// - informs it that one is up
  338. message.channel.sendMessage("RDA status");// - asks it tell you whether one is up, which one is up, how long it's been up, or how long since the last one went down, and how many ppl, whether it's been found, and if any ppl are tracking(edited)
  339. message.channel.sendMessage("RDA regions");// - asks it to PM you a table (in the SS) showing is what tracking what region and when they started
  340. message.channel.sendMessage("RDA [name of particular region]");// - signs you up to track a region
  341. message.channel.sendMessage("RDA down");// tells it the RDA is down
  342. message.channel.sendMessage("RDA found");// tells it that the RDA is found
  343. //We use two chests - and put the books that we've finished tracking in one chest
  344. //the OCR and auto-notification thing would be huge tho still
  345. } else
  346.  
  347. if(message.content.startsWith(prefix + 'IDOCsetgame')) {
  348. bot.user.setGame(argresult);
  349. }
  350. }
  351. });
  352.  
  353. //bot.login(settings.token);
  354.  
  355.  
  356. //const commando = require('discord.js-commando');
  357. //bot.registry.registerGroup('random', 'Random');
  358. //bot.registry.registerDefaults();
  359. //bot.registry.registerCommandsIn(__dirname + "/commands");
  360. //nconf.set('lastDied', 'Avian');
  361.  
  362. //setInterval(function() {
  363. //var d = Date.now();
  364. //var test;
  365. //if(SpawnInMss){
  366. //if(d >= SpawnTest) return; // Return if actual time is not the same as spawn
  367. //NOTIFY_CHANNEL.sendMessage('Spawn started!!');
  368. //console.log("Spawnstarted " + d + " : " + test + " : " + Spawn );
  369. //}
  370. //else{
  371. //console.log("No recorded date of last death " + d + " : " + test + " : " + Spawn );
  372. //}}, 1000);
  373.  
  374. //nconf.load();
  375.  
  376. //var spawnHours = 5;
  377. //var millisecondsInAnHour = 60 * 60 * 1000;
  378. //var date = dojo.date.stamp.fromISOString(nconf.get('lastDied'));
  379. //var spawnTimeMS = (date).getTime() + ( spawnHours * millisecondsInAnHour);
  380. //var spawnDate = new Date(spawnTimeMS);
  381.  
  382. // Send message to discord containing spawnDate
  383. //
  384. //schedule.scheduleJob(spawnDate, function() {
  385. // Send message to discord saying the boss has spawned
  386. //bot.on('message', (message) => {
  387. //message.channel.sendMessage(`Arch Daemon will be spawning at around: ` + spawnDate);
  388. //});
  389. //});
  390.  
  391. //var conf_defaults = require('./config-defaults.json');
  392. //var conf_file = './config.json';
  393. //if( ! fs.existsSync(conf_file) ) {
  394. //fs.writeFileSync( conf_file, JSON.stringify(conf_defaults,null, 2) );
  395. //}
  396. //nconf.file({file: conf_file});
  397.  
  398. // Create a directory tree watcher.
  399. //watcher = hound.watch('F:/UO Server/UOS/Snapshots/Default/');
  400.  
  401. //watcher.on('create', function(file, stats) {
  402. //console.log(file + ' was created');
  403. //var idocF = Moment(new Date());
  404. //bot.on('ready', () => {
  405. //Discord.on('message', function(user, userID, channelID, message, rawEvent) {
  406. //var channel = bot.channels.find("name", "greatly2idoc");
  407. //var channel = bot.channels.find("name", "testing");
  408. //channel.sendMessage("Please dont spam!");
  409. //sendFiles(channelID, [file]);
  410. //console.log('268768548564762625', 'test');
  411. //console.log(`IDOC Finder Update: ` + idocF.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z') + ` - Server Time`);
  412. //});
  413.  
  414.  
  415. // bot.message.sendMessage(`IDOC Finder Update: ` + idocF.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z') + ` - Server Time`);
  416. //bot.message.sendMessage('268768548564762625', 'test');
  417. //console.log('268768548564762625', 'test');
  418. //});
  419.  
  420. //var con = initializeConnection({
  421. // host: "79.170.44.148",
  422. // user: "cl34-idoc",
  423. // password: "bD-hn.E3X",
  424. // database: "cl34-idoc"
  425. //});
  426.  
  427. //function initializeConnection(config) {
  428. // function addDisconnectHandler(con) {
  429. // con.on("error", function (error) {
  430. // if (error instanceof Error) {
  431. // if (error.code === "PROTOCOL_CONNECTION_LOST") {
  432. // console.error(error.stack);
  433. // console.log("Lost connection. Reconnecting...");
  434. // initializeConnection(con.config);
  435. // } else if (error.fatal) {
  436. // throw error;
  437. // }
  438. // }
  439. // });
  440. // }
  441.  
  442. // var con = mysql.createConnection(config);
  443. // Add handlers.
  444. // addDisconnectHandler(con);
  445. // bot.login(settings.token);
  446. // con.connect();
  447. // return con;
  448. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement