2019-03-19 12:06:46 -07:00
"use strict" ;
// Created by Dalton Lange
exports . _ _esModule = true ;
var fs = require ( 'fs' ) ;
var Discord = require ( 'discord.js' ) ;
var client = new Discord . Client ( ) ;
var info = JSON . parse ( fs . readFileSync ( "./data/info.json" ) ) ;
var races = [ "tree" , "ground" , "chipmunk" ] ;
var classes = [ "rogue" , "berserker" , "knight" , "ranger" , "huntsman" , "priest" ] ;
var lastNames = [ "Nutcrack" , "Seedsower" , "McScuiri" , "Rodentia" , "Arbora" , "Patagi" ] ;
var newPlayerTemplate = {
"name" : "" ,
"race" : "" ,
"class" : "" ,
"level" : 1 ,
"nuts" : 0 ,
"dm" : false ,
"dm_points" : 0
} ;
var processes = {
2019-03-21 12:47:06 -07:00
"help" : {
title : "Help" ,
description : "Show a description of a command" ,
run : function ( msg , data , args , file ) { return help ( msg , args ) ; }
2019-03-19 12:06:46 -07:00
} ,
2019-03-21 12:47:06 -07:00
"read" : {
title : "Read" ,
description : "Read a description of your squirrel" ,
run : function ( msg , data , args , file ) { return read ( msg , data ) ; }
} ,
"create" : {
title : "Create" ,
description : "Create a squirrel, can only be used once." ,
run : function ( msg , data , args , file ) { return msg . reply ( "You already have a squirrel silly." ) ; }
} ,
"namechange" : {
title : "Namechange" ,
description : "Change the name of your squirrel, can only be used once" ,
run : function ( msg , data , args , file ) { return nameChange ( msg , data , file ) ; }
} ,
"races" : {
title : "Races" ,
description : "List all the races" ,
run : function ( msg , data , args , file ) { return printRaces ( msg ) ; }
} ,
"classes" : {
title : "Classes" ,
description : "List all the classes" ,
run : function ( msg , data , args , file ) { return printClasses ( msg ) ; }
2019-03-19 12:06:46 -07:00
}
} ;
2019-03-21 12:47:06 -07:00
var COLOR = "#E81B47" ;
2019-03-19 12:06:46 -07:00
var authorId ;
var playerFile ;
var rawPlayerData ;
var command ;
var messageContent ;
var args = [ "" ] ;
var encounterInProgress ;
var playerData ;
client . on ( 'ready' , function ( ) {
console . log ( "Logged in as " + client . user . tag + "!" ) ;
var encounterChannel = client . channels . find ( function ( ch ) { return ch . name === 'the-wild' ; } ) ;
var preparingChannel = client . channels . find ( function ( ch ) { return ch . name === 'preparing-for-expedition' ; } ) ;
// const preparingCollector = new Discord.MessageCollector(preparingChannel);
// preparingCollector.on('collect', msg => {
// console.log(msg.content);
// });
// setInterval( function() { encounter(encounterChannel); }, 300000 );
// encounter(encounterChannel);
} ) ;
client . on ( 'message' , function ( msg ) {
if ( msg . channel . type === "dm" )
return ;
messageContent = msg . content . split ( " " ) ;
command = messageContent [ 0 ] ;
if ( messageContent . length > 1 )
args = messageContent . slice ( 1 ) ;
if ( ! command . startsWith ( info . prefix ) )
return ;
authorId = msg . author . id ;
playerFile = "./data/playerdata/" + authorId + ".json" ;
if ( fs . existsSync ( playerFile ) ) {
rawPlayerData = fs . readFileSync ( playerFile ) ;
playerData = JSON . parse ( rawPlayerData ) ;
2019-03-21 12:47:06 -07:00
Object . keys ( processes ) . forEach ( function ( process ) {
if ( "!" + processes [ process ] . title . toLowerCase ( ) === command ) {
processes [ process ] . run ( msg , playerData , args , playerFile ) ;
2019-03-19 12:06:46 -07:00
}
} ) ;
}
else if ( command === "!create" ) {
console . log ( "Attempting to make a squirrel for " + authorId ) ;
if ( create ( msg , args ) )
console . log ( "Squirrel made succesfully for " + authorId ) ;
else
console . log ( "Failed to create quirrel for " + authorId ) ;
}
} ) ;
client . login ( info . key ) ;
function encounter ( encounterChannel ) {
if ( ! encounterInProgress ) { // randomInt(1,60) === 60 &&
console . log ( "Starting Encounter" ) ;
encounterInProgress = true ;
encounterChannel . send ( "An encounter is beginning!" ) ;
encounterChannel . send ( "`\u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0`\n`\u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0`\n`\u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0`" ) . then ( function ( msg ) {
console . log ( "Encounter finished" ) ;
encounterInProgress = false ;
} ) ;
}
}
function randomInt ( low , high ) {
return Math . floor ( Math . random ( ) * ( high - low + 1 ) + low ) ;
}
function raceText ( race ) {
if ( race === races [ 2 ] )
return capitalize ( race ) ;
else if ( race === races [ 0 ] || race === races [ 1 ] )
return capitalize ( race ) + " Squirrel" ;
else {
console . log ( "Error " + race + " is not a valid race" ) ;
return "" ;
}
}
function capitalize ( toCaps ) {
return toCaps . charAt ( 0 ) . toUpperCase ( ) + toCaps . slice ( 1 ) . toLowerCase ( ) ;
}
// Commands:
2019-03-21 12:47:06 -07:00
function help ( msg , args ) {
}
2019-03-19 12:06:46 -07:00
function read ( msg , data ) {
var embed = new Discord . RichEmbed ( )
. setTitle ( msg . author . username )
. setDescription ( "Squirrel Info" )
2019-03-21 12:47:06 -07:00
. setColor ( COLOR )
2019-03-19 12:06:46 -07:00
. setThumbnail ( msg . author . avatarURL )
. addField ( "Name" , data . name )
. addField ( "Race" , raceText ( data . race ) )
. addField ( "Class" , capitalize ( data [ "class" ] ) )
. addField ( "Nuts" , data . nuts )
. addField ( "Level" , data . level ) ;
if ( data . dm === true )
embed . addField ( "DM Points" , data . dm _points ) ;
msg . channel . send ( embed ) ;
}
function create ( msg , args ) {
if ( args . length === 3 ) {
var newSquirrel _1 = msg . content . split ( ' ' ) ;
var newPlayer = newPlayerTemplate ;
var incorrect _1 = "" ;
incorrect _1 = " race" ;
races . some ( function ( type ) {
if ( newSquirrel _1 [ 2 ] . toLowerCase ( ) == type ) {
incorrect _1 = "" ;
return true ;
}
} ) ;
if ( incorrect _1 == "" )
incorrect _1 = " class" ;
else
incorrect _1 += ", and class" ;
classes . some ( function ( type ) {
if ( newSquirrel _1 [ 3 ] . toLowerCase ( ) == type ) {
if ( incorrect _1 == " race, and class" )
incorrect _1 = " race" ;
else
incorrect _1 = "" ;
return true ;
}
} ) ;
if ( incorrect _1 === "" ) {
newPlayer . name = newSquirrel _1 [ 1 ] + " " + lastNames [ randomInt ( 0 , lastNames . length - 1 ) ] ;
newPlayer . race = newSquirrel _1 [ 2 ] ;
newPlayer [ "class" ] = newSquirrel _1 [ 3 ] ;
fs . writeFileSync ( playerFile , JSON . stringify ( newPlayer ) ) ;
msg . reply ( "Squirrel " + newPlayer . name + " has been created" ) ;
return true ;
}
else {
msg . reply ( "Some parameters were entered incorrectly. Remember the format is \"create name race class\". Your name can be whatever you want, but you have to choose from !race, and !class. You incorrectly entered your:" + incorrect _1 + "." ) ;
return false ;
}
}
else {
msg . reply ( "Too many or too few parameters were entered. The format is \"create name race class\". You only get to choose your first name, don't write a last name." ) ;
return false ;
}
}
function nameChange ( msg , data , file ) {
if ( data . level == 1 ) {
if ( msg . content . split ( ' ' ) . length >= 2 ) {
var newName = msg . content . split ( ' ' ) [ 1 ] ;
data . name = capitalize ( newName ) + " " + data . name . split ( ' ' ) [ 1 ] ;
fs . writeFileSync ( file , JSON . stringify ( data ) ) ;
msg . reply ( "Name changed to " + data . name ) ;
}
else {
msg . reply ( "Please provide a name" ) ;
}
}
else {
msg . reply ( "Sorry, you have to be level 1 to change your name. You're stuck with " + data . name ) ;
}
}
function printRaces ( msg ) {
var print = "The races are: " ;
for ( var i = 0 ; i < races . length ; i ++ ) {
if ( i === races . length - 1 ) {
print += "& " + raceText ( races [ i ] ) + "s" ;
}
else {
print += raceText ( races [ i ] ) + "s, " ;
}
}
msg . reply ( print ) ;
}
function printClasses ( msg ) {
var print = "The classes are: " ;
for ( var i = 0 ; i < classes . length ; i ++ ) {
if ( i === classes . length - 1 ) {
print += "& " + capitalize ( classes [ i ] ) + "s" ;
}
else {
print += capitalize ( classes [ i ] ) + "s, " ;
}
}
msg . reply ( print ) ;
}