Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Discord = require('discord.js');
- const bot = new Discord.Client();
- const mysql = require('mysql');
- const settings = require('./settings.json');
- const schedule = require('node-schedule');
- const nconf = require('nconf');
- const Moment = require('moment-timezone');
- const fs = require("fs");
- const hound = require('hound');
- const Table = require('cli-table2');
- //- Connection configuration
- var db_config = {
- host: '79.170.44.148',
- user: 'cl34-idoc',
- password: 'bD-hn.E3X',
- database: 'cl34-idoc'
- };
- //- Create the connection variable
- var con = mysql.createConnection(db_config);
- //- Establish a new connection
- con.connect(function(err){
- if(err) {
- // mysqlErrorHandling(connection, err);
- console.log("\n\t *** Cannot establish a connection with the database. ***");
- con = reconnect(con);
- }else {
- console.log("\n\t *** New connection established with the database. ***")
- bot.login(settings.token);
- }
- });
- //- Reconnection function
- function reconnect(con){
- console.log("\n New connection tentative...");
- //- Destroy the current connection variable
- if(con) con.destroy();
- //- Create a new one
- var con = mysql.createConnection(db_config);
- //- Try to reconnect
- con.connect(function(err){
- if(err) {
- //- Try to connect every 2 seconds.
- setTimeout(reconnect, 2000);
- }else {
- console.log("\n\t *** New connection established with the database. ***")
- bot.login(settings.token);
- return con;
- }
- });
- }
- //- Error listener
- con.on('error', function(err) {
- //- The server close the connection.
- if(err.code === "PROTOCOL_CONNECTION_LOST"){
- console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
- con = reconnect(con);
- }
- //- Connection in closing
- else if(err.code === "PROTOCOL_ENQUEUE_AFTER_QUIT"){
- console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
- con = reconnect(con);
- }
- //- Fatal error : connection variable must be recreated
- else if(err.code === "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR"){
- console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
- con = reconnect(con);
- }
- //- Error because a connection is already being established
- else if(err.code === "PROTOCOL_ENQUEUE_HANDSHAKE_TWICE"){
- console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
- }
- //- Anything else
- else{
- console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
- con = reconnect(con);
- }
- });
- bot.on('ready', () => {
- console.log('IDOC Bot Online');
- fs.readFile('./config.json', 'utf8', function (err,data) {
- data = JSON.parse(data);
- for(var i = 0; i < data.length; i++) {
- //var idocLocation = new idocLocation();
- Sector = data[i].Sector;
- SectorStatus = data[i].SectorStatus;
- SectorRunner = data[i].SectorRunner;
- SectorTime = Moment(data[i].SectorTime);
- SectorTimeTZ = SectorTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- //save(function (err) {});
- console.log(`Sector: ` + Sector + ` Sector Status: ` + SectorStatus + ` -- Sector Runner: ` + SectorRunner + ` -- Sector Time: ` + SectorTimeTZ);
- }
- });
- bot.user.setGame("IDOC Manager Online...");
- });
- //prefix declaration
- var prefix = "!";
- bot.on('message', (message) => {
- let args = message.content.split(' ').slice(1);
- var argresult = args.join(' ');
- if(!message.content.startsWith(prefix)) return;
- if(message.author.bot) return;
- if(message.content.startsWith(prefix + 'IDOCStart')) {
- //!IDOCSectorStart 01
- let args = message.content.split(" ").slice(1);
- //message.reply(' is running sector ' + args[0]);
- if (args > 0 && args < 24) {
- fs.readFile('./config.json',{encoding: 'utf8'},function(err,data) {
- var data = JSON.parse(data);
- var date = new Date();
- var i = args[0] - 1;
- data[i].SectorStatus = "Running";
- data[i].SectorRunner = message.author.username;
- data[i].SectorTime = date;
- sT = Moment(date);
- sTTZ = sT.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- console.log(data[i].SectorStatus + " " + data[i].SectorRunner + " " + sTTZ);
- message.channel.sendMessage(data[i].SectorRunner+ ' is running sector ' + "Sector: " + args[0] + " @ " + sTTZ );
- data.push();
- var dataJSON = JSON.stringify(data, null, 2);
- fs.writeFileSync('./config.json', dataJSON);
- });
- }
- } else
- if(message.content.startsWith(prefix + 'IDOCEnd')) {
- //!IDOCSectorEnd 1
- let args = message.content.split(" ").slice(1);
- //message.reply(' is running sector ' + args[0]);
- if (args > 0 && args < 24) {
- fs.readFile('./config.json',{encoding: 'utf8'},function(err,data) {
- var data = JSON.parse(data);
- var date = new Date();
- var i = args[0] - 1;
- data[i].SectorStatus = "Completed";
- data[i].SectorRunner = message.author.username;
- data[i].SectorTime = date;
- sT = Moment(date);
- sTTZ = sT.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- console.log(data[i].SectorStatus + " " + data[i].SectorRunner + " " + sTTZ);
- message.channel.sendMessage(data[i].SectorRunner+ ' completed running sector ' + "Sector: " + args[0] + " @ " + sTTZ );
- data.push();
- var dataJSON = JSON.stringify(data, null, 2);
- fs.writeFileSync('./config.json', dataJSON);
- });
- }
- } else
- if(message.content.startsWith(prefix + 'IDOCTimer')) {
- message.channel.sendMessage(message.author.username + " is Timing location : ");
- let args = message.content.split(" ").slice(1);
- //message.reply(' is running sector ' + args[0]);
- if (args > 0) {
- fs.readFile('./config.json',{encoding: 'utf8'},function(err,data) {
- var data = JSON.parse(data);
- var date = new Date();
- var i = args[0] - 1;
- data[i].SectorStatus = "Completed";
- data[i].SectorRunner = message.author.username;
- data[i].SectorTime = date;
- sT = Moment(date);
- sTTZ = sT.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- console.log(data[i].SectorStatus + " " + data[i].SectorRunner + " " + sTTZ);
- message.channel.sendMessage(data[i].SectorRunner+ ' completed running sector ' + "Sector: " + args[0] + " @ " + sTTZ );
- data.push();
- var dataJSON = JSON.stringify(data, null, 2);
- fs.writeFileSync('./config.json', dataJSON);
- });
- }
- } else
- if(message.content.startsWith(prefix + 'IDOC')) {
- let args = message.content.split(" ").slice(1);
- if (args == 'fairly') {
- message.channel.sendMessage('finding fairly worns....' );
- con.query('SELECT id, name, rune, cs, blind, timername, windowstart, image FROM idocs WHERE cs = 1 AND DATE(NOW()) = DATE(windowstart)',function(err,rows){
- if(err) throw err;
- if (rows.length > 0) {
- for(var i = 0; i < rows.length; i++) {
- IDOCTime = Moment(rows[i].windowstart);
- blindtext = rows[i].blind;
- if (rows[i].blind == 1) {
- blindtext = "Yes";
- } else {
- blindtext = "No";
- }
- IDOCTimeTZ = IDOCTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- message.channel.sendMessage('Fairly Status: ' + rows[i].id
- + ' - Finder:' + rows[i].name
- + ' - Rune: ' + rows[i].rune
- + ' - Timer: ' + rows[i].timername
- + ' - Change Time: ' + IDOCTimeTZ
- + ' - Blind: ' + blindtext
- + ' - ' + rows[i].image);
- }
- }
- });
- } else
- if (args == 'greatly') {
- message.channel.sendMessage('finding greatly worns....' );
- con.query('SELECT id, name, rune, cs, blind, timername, windowstart, image FROM idocs WHERE cs = 2 AND DATE(NOW()) = DATE(windowstart)',function(err,rows){
- if(err) throw err;
- if (rows.length > 0) {
- for(var i = 0; i < rows.length; i++) {
- IDOCTime = Moment(rows[i].windowstart);
- IDOCTimeTZ = IDOCTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- blindtext = rows[i].blind;
- if (rows[i].blind == 1) {
- blindtext = "Yes";
- } else {
- blindtext = "No";
- }
- message.channel.sendMessage('Greatly Status: ' + rows[i].id
- + ' - Finder:' + rows[i].name
- + ' - Rune: ' + rows[i].rune
- + ' - Timer: ' + rows[i].timername
- + ' - Change Time: ' + IDOCTimeTZ
- + ' - Blind: ' + blindtext
- + ' - ' + rows[i].image);
- }
- }
- });
- } else
- if (args == 'today') {
- message.channel.sendMessage('finding todays IDOCs....' );
- con.query('SELECT id, name, rune, cs, blind, timername, windowstart, image FROM idocs WHERE cs = 3 AND DATE(NOW()) = DATE(windowstart)',function(err,rows){
- if(err) throw err;
- if (rows.length > 0) {
- for(var i = 0; i < rows.length; i++) {
- IDOCTime = Moment(rows[i].windowstart);
- IDOCTimeTZ = IDOCTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- blindtext = rows[i].blind;
- if (rows[i].blind == 1) {
- blindtext = "Yes";
- } else {
- blindtext = "No";
- }
- message.channel.sendMessage('IDOC Info: ' + rows[i].id
- + ' - Finder:' + rows[i].name
- + ' - Rune: ' + rows[i].rune
- + ' - Timer: ' + rows[i].timername
- + ' - Change Time: ' + IDOCTimeTZ
- + ' - Blind: ' + blindtext
- + ' - ' + rows[i].image);
- }
- }
- });
- } else
- if (args == 'needtimed') {
- } else
- if (args == 'needtimed') {
- } else
- if (args == 'needtimed') {
- } else
- if (args == 'sectors') {
- message.channel.sendMessage('IDOC Bot is working!');
- console.log('IDOC Bot is working!');
- var dateStatus = new Date();
- dateStatusTime = Moment(dateStatus);
- dateStatusTimeTZ = dateStatusTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- console.log(message.author.username, message.author.id, ` -- Time: ` + dateStatusTimeTZ);
- fs.readFile('./config.json', 'utf8', function (err,data) {
- data = JSON.parse(data);
- // instantiate
- var table = new Table({
- head: ['Sectors', 'Status', 'Runner', 'Time']
- });
- for(var i = 0; i < data.length; i++) {
- //var idocLocation = new idocLocation();
- Sector = data[i].Sector;
- SectorStatus = data[i].SectorStatus;
- SectorRunner = data[i].SectorRunner;
- SectorTime = Moment(data[i].SectorTime);
- SectorTimeTZ = SectorTime.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z');
- // table is an Array, so you can `push`, `unshift`, `splice` and friends
- table.push(
- [Sector, SectorStatus, SectorRunner , SectorTimeTZ]
- );
- // message.channel.sendMessage(`Sector: ` + Sector + ` Sector Status: ` + SectorStatus + ` -- Sector Runner: ` + SectorRunner + ` -- Sector Time: ` + SectorTimeTZ + "\n");
- }
- console.log(table.toString());
- //message.channel.sendMessage("test table goes here");
- message.channel.sendMessage(table);
- });
- }else
- if (args == 'help') {
- message.channel.sendMessage("!IDOC today - Lists all In danger of collapse today with time");
- message.channel.sendMessage("!IDOC greatly - Lists all current locations with detailed info and picture (In Testing)");
- message.channel.sendMessage("!IDOC fairly - Lists all current locations with detailed info and picture (In Testing)");
- message.channel.sendMessage("!IDOCStart X where X is the sector you are running");
- message.channel.sendMessage("!IDOCEnd X where X is the sector you have finished running");
- message.channel.sendMessage("!IDOC sectors - Displays list of all Sectors current Status");
- message.channel.sendMessage("!IDOCTimer X - Where X is the id of the location you are timing if not already timed (In Testing)");
- message.channel.sendMessage("!IDOC needtimed - Lists all locations needing timed (In Testing)");
- message.channel.sendMessage("RDA [name of RDA]");// - informs it that one is up
- 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)
- 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
- message.channel.sendMessage("RDA [name of particular region]");// - signs you up to track a region
- message.channel.sendMessage("RDA down");// tells it the RDA is down
- message.channel.sendMessage("RDA found");// tells it that the RDA is found
- //We use two chests - and put the books that we've finished tracking in one chest
- //the OCR and auto-notification thing would be huge tho still
- } else
- if(message.content.startsWith(prefix + 'IDOCsetgame')) {
- bot.user.setGame(argresult);
- }
- }
- });
- //bot.login(settings.token);
- //const commando = require('discord.js-commando');
- //bot.registry.registerGroup('random', 'Random');
- //bot.registry.registerDefaults();
- //bot.registry.registerCommandsIn(__dirname + "/commands");
- //nconf.set('lastDied', 'Avian');
- //setInterval(function() {
- //var d = Date.now();
- //var test;
- //if(SpawnInMss){
- //if(d >= SpawnTest) return; // Return if actual time is not the same as spawn
- //NOTIFY_CHANNEL.sendMessage('Spawn started!!');
- //console.log("Spawnstarted " + d + " : " + test + " : " + Spawn );
- //}
- //else{
- //console.log("No recorded date of last death " + d + " : " + test + " : " + Spawn );
- //}}, 1000);
- //nconf.load();
- //var spawnHours = 5;
- //var millisecondsInAnHour = 60 * 60 * 1000;
- //var date = dojo.date.stamp.fromISOString(nconf.get('lastDied'));
- //var spawnTimeMS = (date).getTime() + ( spawnHours * millisecondsInAnHour);
- //var spawnDate = new Date(spawnTimeMS);
- // Send message to discord containing spawnDate
- //
- //schedule.scheduleJob(spawnDate, function() {
- // Send message to discord saying the boss has spawned
- //bot.on('message', (message) => {
- //message.channel.sendMessage(`Arch Daemon will be spawning at around: ` + spawnDate);
- //});
- //});
- //var conf_defaults = require('./config-defaults.json');
- //var conf_file = './config.json';
- //if( ! fs.existsSync(conf_file) ) {
- //fs.writeFileSync( conf_file, JSON.stringify(conf_defaults,null, 2) );
- //}
- //nconf.file({file: conf_file});
- // Create a directory tree watcher.
- //watcher = hound.watch('F:/UO Server/UOS/Snapshots/Default/');
- //watcher.on('create', function(file, stats) {
- //console.log(file + ' was created');
- //var idocF = Moment(new Date());
- //bot.on('ready', () => {
- //Discord.on('message', function(user, userID, channelID, message, rawEvent) {
- //var channel = bot.channels.find("name", "greatly2idoc");
- //var channel = bot.channels.find("name", "testing");
- //channel.sendMessage("Please dont spam!");
- //sendFiles(channelID, [file]);
- //console.log('268768548564762625', 'test');
- //console.log(`IDOC Finder Update: ` + idocF.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z') + ` - Server Time`);
- //});
- // bot.message.sendMessage(`IDOC Finder Update: ` + idocF.tz('America/Swift_Current').format('dddd, DD, MM, YYYY, hh:mm:ss A z') + ` - Server Time`);
- //bot.message.sendMessage('268768548564762625', 'test');
- //console.log('268768548564762625', 'test');
- //});
- //var con = initializeConnection({
- // host: "79.170.44.148",
- // user: "cl34-idoc",
- // password: "bD-hn.E3X",
- // database: "cl34-idoc"
- //});
- //function initializeConnection(config) {
- // function addDisconnectHandler(con) {
- // con.on("error", function (error) {
- // if (error instanceof Error) {
- // if (error.code === "PROTOCOL_CONNECTION_LOST") {
- // console.error(error.stack);
- // console.log("Lost connection. Reconnecting...");
- // initializeConnection(con.config);
- // } else if (error.fatal) {
- // throw error;
- // }
- // }
- // });
- // }
- // var con = mysql.createConnection(config);
- // Add handlers.
- // addDisconnectHandler(con);
- // bot.login(settings.token);
- // con.connect();
- // return con;
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement