2019-03-19 12:06:46 -07:00
// Created by Julia Lange
2019-05-01 14:20:11 -07:00
//===Requirements===
2019-03-19 12:06:46 -07:00
const fs = require ( 'fs' ) ;
const Discord = require ( 'discord.js' ) ;
const client = new Discord . Client ( ) ;
const info = JSON . parse ( fs . readFileSync ( ` ./data/info.json ` ) ) ;
2019-11-10 21:11:34 -08:00
import { player } from "./enums" ;
2019-03-19 12:06:46 -07:00
2019-05-01 14:20:11 -07:00
//===Data stored for use, such as class, race, and last names===
2019-03-19 12:06:46 -07:00
const races : Array < string > = [ "tree" , "ground" , "chipmunk" ] ;
2019-11-10 21:11:34 -08:00
const classes : Array < string > = [ "rogue" , "berserker" , "knight" , "ranger" , "priest" ] ;
const lastNames : Array < string > = [ "Nutcrack" , "Nutmeg" , "Seedsower" , "McScuiri" , "Rodentia" , "Arbora" , "Patagi" ] ;
2019-05-01 14:20:11 -07:00
const COLOR : string = "#E81B47" ;
2019-03-19 12:06:46 -07:00
const newPlayerTemplate : player = {
2019-11-10 21:11:34 -08:00
"name" : "" ,
"race" : "" ,
2019-03-19 12:06:46 -07:00
"class" : "" ,
2019-11-10 21:11:34 -08:00
"weapon" : {
"weaponName" : "" ,
"attackStat" : 0 ,
"defenseStat" : 0 ,
"secondStat" : 0
} ,
"level" : 1 ,
"nuts" : 0 ,
"staminaMax" : 0 ,
"stamina" : 0 ,
"dm" : false ,
"dm_points" : 0
2019-03-19 12:06:46 -07:00
}
2019-05-01 14:20:11 -07:00
//===Commands===
2019-03-19 12:06:46 -07:00
const processes : any = {
2019-03-21 12:47:06 -07:00
"help" : {
title : "Help" ,
description : "Show a description of a command" ,
2019-05-01 14:20:11 -07:00
args : "**command** - the command you'd like to know more about" ,
2019-03-21 12:47:06 -07:00
run : ( msg : any , data : player , args : string [ ] , file : any ) = > help ( msg , args )
} ,
2019-03-22 14:19:23 -07:00
"list" : {
title : "List" ,
description : "List all commands" ,
args : "" ,
run : ( msg : any , data : player , args : string [ ] , file : any ) = > list ( msg )
} ,
2019-03-21 12:27:39 -07:00
"read" : {
title : "Read" ,
description : "Read a description of your squirrel" ,
2019-03-22 14:02:36 -07:00
args : "" ,
2019-03-21 12:27:39 -07:00
run : ( msg : any , data : player , args : string [ ] , file : any ) = > read ( msg , data )
2019-03-19 12:06:46 -07:00
} ,
2019-03-21 12:27:39 -07:00
"namechange" : {
title : "Namechange" ,
2019-03-22 14:19:23 -07:00
description : "Change the name of your squirrel, can only be used at level 1" ,
2019-05-01 14:20:11 -07:00
args : "**name** - the new name for your squirrel" ,
2019-03-22 14:19:23 -07:00
run : ( msg : any , data : player , args : string [ ] , file : any ) = > nameChange ( msg , data , args , file )
2019-03-21 12:27:39 -07:00
} ,
"races" : {
title : "Races" ,
description : "List all the races" ,
2019-03-22 14:02:36 -07:00
args : "" ,
2019-03-21 12:27:39 -07:00
run : ( msg : any , data : player , args : string [ ] , file : any ) = > printRaces ( msg )
} ,
"classes" : {
title : "Classes" ,
description : "List all the classes" ,
2019-03-22 14:02:36 -07:00
args : "" ,
2019-03-21 12:27:39 -07:00
run : ( msg : any , data : player , args : string [ ] , file : any ) = > printClasses ( msg )
2019-11-10 21:11:34 -08:00
} ,
"create" : {
title : "Create" ,
description : "Create a squirrel, can only be used once" ,
args : "**name** - the name of the created squirrel\n**race** - The race of the created squirrel, type !races for race options\n**class** - The class of the created squirrel, type !classes for class options" ,
run : ( msg : any , data : player , args : string [ ] , file : any ) = > msg . reply ( ` You already have a squirrel silly. ` )
} ,
2019-03-19 12:06:46 -07:00
}
2019-05-01 14:20:11 -07:00
//===Global editable variables===
let encounterChannel ;
let preparingChannel ;
let generalChannel ;
let barChannel ;
2019-03-22 14:02:36 -07:00
// let encounterInProgress: boolean;
2019-03-19 12:06:46 -07:00
2019-05-01 14:20:11 -07:00
//===Initalization===
2019-03-19 12:06:46 -07:00
client . on ( 'ready' , ( ) = > {
console . log ( ` Logged in as ${ client . user . tag } ! ` ) ;
2019-05-01 14:20:11 -07:00
encounterChannel = client . channels . find ( ch = > ch . name === 'the-wild' ) ;
preparingChannel = client . channels . find ( ch = > ch . name === 'preparing-for-expedition' ) ;
generalChannel = client . channels . find ( ch = > ch . name === 'general-table' ) ;
barChannel = client . channels . find ( ch = > ch . name === 'the-bar' ) ;
2019-03-19 12:06:46 -07:00
// const preparingCollector = new Discord.MessageCollector(preparingChannel);
// preparingCollector.on('collect', msg => {
// console.log(msg.content);
// });
// setInterval( function() { encounter(encounterChannel); }, 300000 );
// encounter(encounterChannel);
} ) ;
2019-05-01 14:20:11 -07:00
//===When receiving messages
2019-03-19 12:06:46 -07:00
client . on ( 'message' , msg = > {
2019-05-01 14:20:11 -07:00
//Reasons to exit
2019-11-10 21:11:34 -08:00
if ( ! msg . content . split ( " " ) [ 0 ] . startsWith ( info . prefix ) ) return ; // if the message doesn't start with the prefix
2019-03-19 12:06:46 -07:00
2019-11-10 21:11:34 -08:00
let messageContent : Array < string > = msg . content . split ( " " ) ; // split message into an array on spaces
2019-05-01 14:20:11 -07:00
let command : string = messageContent [ 0 ] ; // This is the command they are using
2019-03-19 12:06:46 -07:00
2019-05-01 14:20:11 -07:00
let args : Array < string > = [ "" ] ; // arguments of the message
2019-03-22 14:02:36 -07:00
if ( messageContent . length > 1 ) args = messageContent . slice ( 1 ) ;
2019-05-01 14:20:11 -07:00
let authorId : number = msg . author . id ; // Message Author
2019-03-22 14:02:36 -07:00
let playerFile : string = ` ./data/playerdata/ ${ authorId } .json ` ;
2019-03-19 12:06:46 -07:00
2019-05-01 14:20:11 -07:00
if ( fs . existsSync ( playerFile ) ) { // If they're file exists, they can use commands, otherwise direct them to the create command
2019-11-10 21:11:34 -08:00
let playerData : player = JSON . parse ( fs . readFileSync ( playerFile ) ) ;
2019-03-19 12:06:46 -07:00
2019-05-01 14:20:11 -07:00
Object . keys ( processes ) . forEach ( process = > { // See if the command they typed is a command
2019-03-22 14:02:36 -07:00
if ( ` ${ info . prefix } ${ processes [ process ] . title . toLowerCase ( ) } ` === command ) {
2019-03-21 12:47:06 -07:00
processes [ process ] . run ( msg , playerData , args , playerFile ) ;
2019-03-19 12:06:46 -07:00
}
} ) ;
2019-05-01 14:20:11 -07:00
} else {
2019-11-10 21:11:34 -08:00
if ( command === ` ${ info . prefix } create ` ) { // if they are using the create command
2019-05-01 14:20:11 -07:00
console . log ( ` Attempting to make a squirrel for ${ authorId } ` ) ;
if ( create ( msg , args , playerFile ) )
console . log ( ` Squirrel made succesfully for ${ authorId } ` ) ;
else
console . log ( ` Failed to create a squirrel for ${ authorId } ` ) ;
} else {
msg . reply ( ` Please make a squirrel before interacting with the bot. To do so please use the create command. For more information on creation type "!help create" ` ) ;
}
2019-03-19 12:06:46 -07:00
}
} ) ;
2019-05-01 14:20:11 -07:00
// Log the bot in
2019-03-19 12:06:46 -07:00
client . login ( info . key ) ;
2019-05-01 14:20:11 -07:00
//===FUNCTIONS===
2019-03-22 14:02:36 -07:00
/ *
2019-03-19 12:06:46 -07:00
function encounter ( encounterChannel : any ) : void {
if ( ! encounterInProgress ) { // randomInt(1,60) === 60 &&
console . log ( "Starting Encounter" ) ;
encounterInProgress = true ;
encounterChannel . send ( "An encounter is beginning!" ) ;
encounterChannel . send ( ` \` ■■■ ■■■ ■■■ ■■■ \` \ n \` ■■■ ■■■ ■■■ ■■■ \` \ n \` ■■■ ■■■ ■■■ ■■■ \` ` ) . then ( msg = > {
console . log ( "Encounter finished" ) ;
encounterInProgress = false ;
} ) ;
}
}
2019-03-22 14:02:36 -07:00
* /
2019-03-19 12:06:46 -07:00
function randomInt ( low : number , high : number ) : number {
return Math . floor ( Math . random ( ) * ( high - low + 1 ) + low )
}
function raceText ( race : string ) : string {
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 : string ) : string {
return toCaps . charAt ( 0 ) . toUpperCase ( ) + toCaps . slice ( 1 ) . toLowerCase ( ) ;
}
2019-05-01 14:20:11 -07:00
//===COMMAND FUNCTIONS==
2019-03-19 12:06:46 -07:00
2019-03-22 14:02:36 -07:00
function help ( msg : any , args : string [ ] ) : void {
let argument : string = args [ 0 ] . toLowerCase ( ) ;
if ( argument === "" ) {
2019-05-01 14:20:11 -07:00
msg . reply ( ` Please provide a command, like "!help help", or "!help races" \ nAlternatively type "!list" for a list of commands ` ) ;
2019-03-22 14:02:36 -07:00
} else if ( processes . hasOwnProperty ( argument ) ) {
let embed = new Discord . RichEmbed ( )
. setTitle ( processes [ argument ] . title )
2019-03-22 14:19:23 -07:00
. setColor ( COLOR )
. setDescription ( processes [ argument ] . description ) ;
2019-03-22 14:02:36 -07:00
if ( processes [ argument ] . args === "" ) {
embed . addField ( "Arguments:" , ` This command has no arguments ` ) ;
} else {
embed . addField ( "Arguments:" , processes [ argument ] . args ) ;
}
msg . channel . send ( embed ) ;
} else {
msg . reply ( ` That command does not exist ` ) ;
}
2019-03-21 12:47:06 -07:00
}
2019-03-22 14:19:23 -07:00
function list ( msg : any ) : void {
let embed = new Discord . RichEmbed ( )
. setTitle ( "List of Commands" )
. setColor ( COLOR ) ;
let description : string = "" ;
Object . keys ( processes ) . forEach ( process = > {
description += ` ** ${ processes [ process ] . title } :** ${ processes [ process ] . description } . \ n ` ;
} ) ;
embed . setDescription ( description ) ;
msg . author . send ( embed ) ;
}
2019-03-19 12:06:46 -07:00
function read ( msg : any , data : player ) : void {
let embed = new Discord . RichEmbed ( )
. setTitle ( msg . author . username )
2019-03-21 12:47:06 -07:00
. setColor ( COLOR )
2019-03-19 12:06:46 -07:00
. setThumbnail ( msg . author . avatarURL )
2019-03-22 14:19:23 -07:00
. setDescription ( "Squirrel Info" )
2019-03-19 12:06:46 -07:00
. 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 ) ;
}
2019-03-22 14:02:36 -07:00
function create ( msg : any , args : Array < string > , file : any ) : boolean {
2019-03-19 12:06:46 -07:00
if ( args . length === 3 ) {
let newPlayer : player = newPlayerTemplate ;
let incorrect : string = "" ;
incorrect = " race" ;
races . some ( type = > {
2019-03-22 15:08:20 -07:00
if ( args [ 1 ] . toLowerCase ( ) == type ) {
2019-03-19 12:06:46 -07:00
incorrect = "" ;
return true ;
}
} ) ;
if ( incorrect == "" )
incorrect = " class" ;
else
incorrect += ", and class" ;
classes . some ( type = > {
2019-03-22 15:08:20 -07:00
if ( args [ 2 ] . toLowerCase ( ) == type ) {
2019-03-19 12:06:46 -07:00
if ( incorrect == " race, and class" )
incorrect = " race" ;
else
incorrect = "" ;
return true ;
}
} ) ;
if ( incorrect === "" ) {
2019-03-22 15:08:20 -07:00
newPlayer . name = args [ 0 ] + " " + lastNames [ randomInt ( 0 , lastNames . length - 1 ) ] ;
newPlayer . race = args [ 1 ] ;
newPlayer . class = args [ 2 ] ;
2019-03-22 14:02:36 -07:00
fs . writeFileSync ( file , JSON . stringify ( newPlayer ) ) ;
2019-03-19 12:06:46 -07:00
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 } . ` ) ;
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 ;
}
}
2019-03-22 14:19:23 -07:00
function nameChange ( msg : any , data : player , args : string [ ] , file : any ) : void {
2019-03-19 12:06:46 -07:00
if ( data . level == 1 ) {
2019-03-22 15:08:20 -07:00
if ( args . length >= 1 ) {
data . name = capitalize ( args [ 0 ] ) + " " + data . name . split ( ' ' ) [ 1 ] ;
2019-03-19 12:06:46 -07:00
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 : any ) : void {
let print = "The races are: " ;
for ( let 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 : any ) : void {
let print = "The classes are: " ;
for ( let 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 ) ;
}