2019-03-19 12:06:46 -07:00
// Created by Julia Lange
2019-05-01 14:20:11 -07:00
//===Requirements===
2019-12-25 00:58:54 -08:00
export const Discord = require ( 'discord.js' ) ;
2019-03-19 12:06:46 -07:00
const client = new Discord . Client ( ) ;
2019-12-25 00:58:54 -08:00
export const fs = require ( 'fs' ) ;
2019-03-19 12:06:46 -07:00
const info = JSON . parse ( fs . readFileSync ( ` ./data/info.json ` ) ) ;
2019-12-25 00:58:54 -08:00
import { player } from "./scripts/enums" ;
import { processes , channels } from "./scripts/commands" ;
import { encounterInProgress } from "./scripts/functions" ;
2019-05-01 14:20:11 -07:00
2019-03-19 12:06:46 -07:00
2019-05-01 14:20:11 -07:00
//===Global editable variables===
2019-12-25 00:58:54 -08:00
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-12-25 00:58:54 -08:00
channels . general = client . channels . find ( ch = > ch . id === info . channels . general ) ;
channels . encounter = client . channels . find ( ch = > ch . id === info . channels . encounter ) ;
channels . preparing = client . channels . find ( ch = > ch . id === info . channels . preparing ) ;
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-12-25 00:58:54 -08:00
switch ( msg . channel ) {
case channels . encounter :
if ( encounterInProgress ) return ;
encounterChannelMessage ( msg ) ;
break ;
default :
defaultChannelMessage ( msg ) ;
}
} ) ;
2019-03-19 12:06:46 -07:00
2019-12-25 00:58:54 -08:00
// Log the bot in
client . login ( info . key ) ;
// Channel Cases
function defaultChannelMessage ( msg : any ) : void {
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 } ` ) ;
2019-12-25 00:58:54 -08:00
if ( processes [ "create" ] [ "create" ] . run ( msg , null , args , playerFile ) )
2019-05-01 14:20:11 -07:00
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-12-25 00:58:54 -08:00
function encounterChannelMessage ( msg : any ) : void {
2019-03-19 12:06:46 -07:00
}