Advertisement
Technical_13

wping

Nov 25th, 2019
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const bot = 'DDObot';
  2. const Discord = require( 'discord.js' );
  3. const commando = require( 'discord.js-commando' );
  4. const path = require( 'path' );
  5. const fsSettings = 'settings.json';
  6. const settings = require( path.join( __dirname, '../../../' + fsSettings ) );
  7. var strSettings = JSON.stringify( settings );
  8. const objTimeString = {
  9.   year: 'numeric', month: 'long', day: 'numeric',
  10.   hour: 'numeric', minute: 'numeric', second: 'numeric',
  11.   timeZone: 'America/New_York', timeZoneName: 'short' };
  12. var strNow = ( new Date() ).toLocaleDateString( 'en-US', objTimeString );
  13. const isDebug = true;//settings[ bot ].onError.isDebugMode;
  14. const strWikiName = ( bot === 'DDObot' ? 'DDOwiki' : 'LOTROwiki' );
  15. const myWiki = settings[ bot ].wikis[ strWikiName.toLowerCase() ];
  16. const wikiArticlePath = myWiki.root + myWiki.article;
  17. const unirest = require( 'unirest' );
  18.  
  19. const arrDefinedStatusCodes = [ 400, 401, 403, 404, 500, 502, 503, 504 ]
  20. const httpStatusCodes = {
  21.   400: 'Bad Request',
  22.   401: 'Unauthorized',
  23.   403: 'Forbidden',
  24.   404: 'Page Not Found',
  25.   500: 'Internal Server Error',
  26.   502: 'Bad Gateway',
  27.   503: 'Service Unavailable',
  28.   504: 'Gateway Timeout'
  29. }
  30.  
  31. class WikiPing extends commando.Command {
  32.   constructor( client ) {
  33.     super( client, {
  34.       name: 'wping',
  35.       group: 'wiki',
  36.       memberName: 'wping',
  37.       description: 'Ping the wiki to see if it\'s up or not.'
  38.     } );
  39.   }
  40.  
  41.   async run( message, strArgs ) {
  42.     var arrArgs = strArgs.split( ' ' );
  43.     if ( arrArgs[ 0 ] !== undefined ) {
  44.       var wikiLinks = [];
  45.       var rawLinks = arrArgs.join( ' ' ).match( /(\[\[(.*?)\]\])*/g ).filter( aMatch => { if ( aMatch !== '' ) { return aMatch; }  } );
  46.       rawLinks.forEach( rawLink => { wikiLinks.push( rawLink.replace( / /g, '_' ).replace( /[\[\]]/g, '' ) ); } );
  47.       var rawTemplateLinks = arrArgs.join( ' ' ).match( /(\{\{(.*?)\}\})*/g ).filter( tMatch => { if ( tMatch !== '' ) { return tMatch; }  } );
  48.       rawTemplateLinks.forEach( rawTemplateLink => { wikiLinks.push( 'Template:' + rawTemplateLink.replace( / /g, '_' ).replace( /[\{\}]/g, '' ) ); } );
  49. //      console.log( '%o', wikiLinks );
  50.     }
  51.    
  52.     let pingPage = ( wikiLinks.length >=1 ? wikiLinks[ 0 ] : 'Ping' );
  53.    
  54.     unirest.get( wikiArticlePath + pingPage ).end( function ( objPingTest ) {
  55.       let intStatCode = objPingTest.statusCode;
  56.       let strDesc = ( arrDefinedStatusCodes.indexOf( intStatCode ) !== -1 ? '`' + intStatCode + '` '+ httpStatusCodes[ intStatCode ] : intStatCode ) + ': https://httpstatuses.com/' + intStatCode;
  57.       console.log( '%s:\n\tPinged: %s\n\tintStatCode: %o\n\tstrDesc: %s', strNow, pingPage, intStatCode, strDesc );
  58.       message.channel.send( strWikiName + ' ' + ( intStatCode === 200 ? 'is **UP**!!!' : 'returned: ' + strDesc ) ).then( () => {
  59.         message.delete( 1000 ).catch( errDel => { console.error( '%s: Failed to delete `!wping` request: %o', strNow, errDel ); } );
  60.       } );
  61.     } );    
  62.   }
  63. }
  64.  
  65. module.exports = WikiPing;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement