2022-06-17 16:41:02 +03:00
// // @ts-check //Disabled
2022-05-28 16:55:56 +03:00
2022-09-30 22:21:44 -04:00
// Maybe have the interaction type be "user" https://canary.discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-types
2022-11-16 16:53:24 -05:00
const { MessageActionRow , MessageButton , Message } = require ( 'discord.js' );
2022-05-24 08:55:57 +03:00
const { MongoClient , ServerApiVersion } = require ( 'mongodb' );
2022-07-01 15:00:52 +03:00
let ecoimport = require ( "../db/econ.js" );
//#region Game Imports
const battle = require ( "./battle.js" );
const ttt = require ( './tictactoe.js' );
2022-07-19 15:41:49 +03:00
const trivia = require ( './trivia.js' );
2022-09-02 15:27:54 -04:00
const mnswpr = require ( './minesweeper.js' );
2022-07-01 15:00:52 +03:00
//#endregion
let snowflake = require ( "../db/addons/snowflake.js" );
2022-05-24 08:55:57 +03:00
const STATE = ecoimport . STATE ;
2022-05-25 22:33:42 +03:00
const BASE = ecoimport . BASE ;
2022-05-24 08:55:57 +03:00
2022-06-11 17:53:44 +03:00
const { winGame , loseGame , equipItem } = require ( './external_game_functions.js' );
2022-06-26 08:13:47 +03:00
const { chooseClass , presentClasses } = require ( './game_classes.js' );
2022-05-29 20:59:39 +03:00
2022-05-24 08:55:57 +03:00
//Has a list of all games (used to change player state)
2022-07-01 15:00:52 +03:00
const allGames = [ 'battle' , 'Tic Tac Toe' ];
2022-09-30 22:21:44 -04:00
2022-05-24 08:55:57 +03:00
//#region functions (NOT GAME SPECIFIC)
/** Adds the game type tag to the user(s) so the system can tell what game they're playing
* @param other_dbo optional, include if the game has two players
*/
2022-05-28 16:55:56 +03:00
async function Initialize ( bot , user_dbo , command , message , first , second , other_dbo = null ) {
return new Promise ( async function ( resolve , reject ) {
2022-09-02 15:27:54 -04:00
user_dbo . findOne ({ "game" : { $exists : true }}). then ( function ( doc ){
2022-05-28 16:55:56 +03:00
if ( allGames . indexOf ( command ) != - 1 ) {
if ( other_dbo != null ) {
user_dbo . updateOne ( { "game" : { $exists : true } }, { $set : { game : command , opponent : other_dbo . s . namespace . collection , state : STATE . FIGHTING }});
other_dbo . updateOne ({ "game" : { $exists : true } }, { $set : { game : command , opponent : user_dbo . s . namespace . collection , state : STATE . FIGHTING }});
} else {
user_dbo . updateOne ({ "game" : { $exists : true } }, { $set : { game : command , state : STATE . FIGHTING }});
}
2022-05-25 22:33:42 +03:00
2022-05-28 16:55:56 +03:00
} else { message . reply ( `ERROR! ${ command } IS NOT A GAME!` ); }
});
//Create a new thread for the game (maybe uneccesary???) - done before initialize
let name_first = await bot . users . cache . get ( first );
let name_second = await bot . users . cache . get ( second );
2022-05-25 22:33:42 +03:00
2022-05-28 16:55:56 +03:00
// message.reply(`${first} [${name_first}], ${second} [${name_second}]`); throw 'ERR';
const threadname = ` ${ name_first . username } VS ${ name_second . username } [ ${ command . toUpperCase () } ]` ;
const thread = await message . channel . threads . create ({
name : threadname ,
// type: 'GUILD_PRIVATE_THREAD',
autoArchiveDuration : 60 ,
reason : `N/A` ,
});
//Need lvl 2 boost for this
// thread.add(first);
// thread.add(second);
message . channel . send ( `<@ ${ first } > and <@ ${ second } > have started a game of *** ${ command . toUpperCase () } !***` );
resolve ( thread );
});
2022-05-25 22:33:42 +03:00
}
//#endregion
2022-05-28 16:55:56 +03:00
//replies to the message with current game specifics
2022-11-16 16:53:24 -05:00
function getGame ( interaction , args , db ) {
2022-05-28 16:55:56 +03:00
let id ;
var temp ;
if ( args . length == 1 && String ( args [ 0 ]). startsWith ( '<' )) { id = args [ 0 ]. substr ( 2 , args [ 0 ]. length - 3 )}
2022-11-16 16:53:24 -05:00
else { id = interaction . user . id ; }
var user_dbo = db . collection ( interaction . user . id );
2022-05-24 08:55:57 +03:00
user_dbo . find ({ "game" : { $exists : true }}). toArray ( function ( err , docs ){
2022-05-25 22:33:42 +03:00
const doc = docs [ 0 ];
2022-05-28 16:55:56 +03:00
if ( doc . game == null ) {
2022-11-16 16:53:24 -05:00
return interaction . reply ( `<@ ${ id } > is not currently playing a game!` );
2022-05-28 16:55:56 +03:00
}
2022-05-25 22:33:42 +03:00
2022-05-28 16:55:56 +03:00
temp = `<@ ${ id } > is currently playing " ${ doc . game } "` ;
2022-05-25 22:33:42 +03:00
if ( doc . opponent != null ) {
2022-05-28 16:55:56 +03:00
temp += ` with <@ ${ doc . opponent } >`
2022-05-25 22:33:42 +03:00
}
2022-05-28 16:55:56 +03:00
2022-11-16 16:53:24 -05:00
interaction . reply ( temp );
2022-05-24 08:55:57 +03:00
});
}
2022-11-16 16:53:24 -05:00
function acceptIsValid ( bot , other_discord , interaction , invUserId , message ) {
2022-05-25 22:33:42 +03:00
if ( other_discord == undefined ) {
2022-11-16 16:53:24 -05:00
interaction . reply ( "This is not a valid invite!" );
2022-05-25 22:33:42 +03:00
return false ;
}
//Make sure the bot was the one creating the invite
2022-11-16 16:53:24 -05:00
let check0 = message . author . bot ;
2022-05-25 22:33:42 +03:00
//Author
2022-11-16 16:53:24 -05:00
let check1 = Number ( other_discord . id ) == Number ( invUserId );
2022-05-25 22:33:42 +03:00
//Time (within the last 5 min)
2022-11-16 16:53:24 -05:00
let prev = snowflake . convertSnowflakeToDate ( message . id );
let now = snowflake . convertSnowflakeToDate ( interaction . id );
2022-05-28 16:55:56 +03:00
// @ts-ignore
2022-05-25 22:33:42 +03:00
let diff = now - prev ;
var minutes = Math . floor (( diff / 1000 ) / 60 );
2022-05-28 16:55:56 +03:00
let check2 = minutes <= 5 || bot . inDebugMode ;
2022-05-24 08:55:57 +03:00
2022-11-16 16:53:24 -05:00
if ( ! check0 ) { interaction . reply ( "really?" ); }
else if ( ! check1 && check2 ) { interaction . reply ( "_INVALID USER_" ); }
else if ( check1 && ! check2 ) { interaction . reply ( "_THIS INVITE EXPIRED!_" ); }
else if ( ! check1 && ! check2 ) { interaction . reply ( "_THIS MESSAGE HAS AN INVALID USER AND HAS EXPIRED_" )}
2022-05-25 22:33:42 +03:00
return ( check0 && check1 && check2 );
2022-05-24 08:55:57 +03:00
}
2022-05-28 16:55:56 +03:00
2022-11-16 16:53:24 -05:00
function hpmp ( interaction , command , dbo ) {
2022-05-30 20:37:15 +03:00
// throw 'THIS HAS NOT BEEN UPDATED WITH THE MOST RECENT VERSION OF THE MONGODB STRUCTURE!';
2022-11-16 16:53:24 -05:00
dbo . find ({ "hpmp" : { $exists : true }}). toArray ( function ( err , doc ) {
interaction . reply ( `You have ${ String ( doc [ 0 ]. hpmp . hp ) } hp and ${ String ( doc [ 0 ]. hpmp . mp ) } mp left!` );
});
/*
2022-05-28 16:55:56 +03:00
if (command == 'hp') {
2022-06-01 20:38:39 +03:00
dbo.find({"hpmp": {$exists: true}}).toArray(function(err, doc) {
2022-11-16 16:53:24 -05:00
return interaction.reply(`You have ${String(doc[0].hpmp.hp)} hp left!`);
2022-05-28 16:55:56 +03:00
});
} else if (command == 'mp') {
2022-06-01 20:38:39 +03:00
dbo.find({"hpmp": {$exists: true}}).toArray(function(err, doc) {
2022-11-16 16:53:24 -05:00
return interaction.reply(`You have ${String(doc[0].hpmp.MP)} mp left!`);
2022-05-28 16:55:56 +03:00
});
}
2022-11-16 16:53:24 -05:00
*/
2022-05-28 16:55:56 +03:00
}
2022-06-01 20:38:39 +03:00
2022-11-16 16:53:24 -05:00
function equip ( interaction , inp , command , dbo , bot , shop ) {
// const inp = args[1];
if ( ! inp ) { return interaction . reply ( "Please provide input (either a weapon for main or shield for secondary)" ); }
2022-06-17 16:41:02 +03:00
2022-06-01 20:38:39 +03:00
//Check if the user is already in a game
2022-06-17 16:41:02 +03:00
dbo . find ({ 'game' : { $exists : true }}). toArray ( function ( err , docs ) {
const doc = docs [ 0 ];
if ( doc . game != null ) {
ret = true ;
2022-07-19 15:41:49 +03:00
// console.log(doc.game);
2022-11-16 16:53:24 -05:00
return interaction . reply ( 'You can\'t equip while in a game!' );
2022-06-17 16:41:02 +03:00
}
//If the thing is a shield, add it to secondary
if ( inp . toLowerCase (). indexOf ( 'shield' ) != - 1 ) {
dbo . find ({ def : true }). toArray ( function ( err , docs ) {
if ( docs [ 0 ] != undefined ) {
dbo . updateOne ({}, { $set : { 'equipped.weapons.secondary' : docs [ 0 ]}});
} else {
2022-11-16 16:53:24 -05:00
interaction . reply ( "You don't own a shield!" );
2022-06-17 16:41:02 +03:00
}
});
} else {
//Else, equip the weapon(s)
dbo . find ({ name : inp , sect : 'Weapons' }). toArray ( function ( err , docs ) {
if ( docs [ 0 ] != undefined ) {
//Equip the weapon
dbo . updateOne ({}, { $set : { 'equipped.weapons.main' : docs [ 0 ]}});
} else {
2022-11-16 16:53:24 -05:00
interaction . reply ( `You don't own any ${ inp } s!` );
2022-06-17 16:41:02 +03:00
}
});
}
});
2022-06-01 20:38:39 +03:00
}
2022-05-24 08:55:57 +03:00
//#endregion
2022-06-17 16:41:02 +03:00
//#region Game Handlers
2022-05-29 20:59:39 +03:00
function in_game_redirector ( bot , interaction , threadname , doc , client , mongouri , items , xp_collection ) {
2022-05-28 16:55:56 +03:00
//Maybe fix this later......
let turn = doc . turn ;
const user1 = doc [ turn ];
const user2 = doc [ Number ( ! turn )];
2022-07-09 20:30:31 +03:00
const db = client . db ( interaction . guildId );
2022-05-28 16:55:56 +03:00
const dbo = db . collection ( user1 );
const other = db . collection ( user2 );
const thread = interaction . channel ;
dbo . find ({ 'game' : { $exists : true }}). toArray ( function ( err , docs ) {
const game = docs [ 0 ]. game
2022-11-16 16:53:24 -05:00
2022-05-28 16:55:56 +03:00
switch ( game ) {
2022-07-01 15:00:52 +03:00
case 'battle' : battle . handle ( client , dbo , other , bot , thread , interaction . customId . toLowerCase (), mongouri , items , interaction , xp_collection );
break ;
case 'Tic Tac Toe' : ttt . handle ( client , db , dbo , other , bot , thread , null , doc , interaction , xp_collection );
break ;
2022-05-28 16:55:56 +03:00
}
});
}
2022-05-24 08:55:57 +03:00
module . exports = {
name : "game" ,
description : "Play a game using Selmer Bot!" ,
2022-11-16 16:53:24 -05:00
async execute ( bot , interaction , command , Discord , mongouri , items , xp_collection ) {
2022-05-28 16:55:56 +03:00
//#region Setup
2022-11-16 16:53:24 -05:00
const id = interaction . user . id ;
const server = interaction . guildId ;
2022-09-27 16:45:50 -04:00
// // @ts-ignore
// const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
// if (client.writeConcern || client.writeConcern) {
// client.close();
// return message.reply("Something went wrong with the database, please try again later and contact support if this problem persists!");
// }
var client ;
await bot . mongoconnection . then (( client1 ) => {
client = client1 ;
});
2022-05-24 08:55:57 +03:00
2022-09-27 16:45:50 -04:00
const botdb = client . db ( 'B|S' + bot . user . id );
const serverinbotdb = botdb . collection ( server );
2022-05-24 08:55:57 +03:00
2022-09-27 16:45:50 -04:00
//Initialize if necessary
2022-11-16 16:53:24 -05:00
ecoimport . CreateNewCollection ( interaction , client , server , id );
var commandName = command . name ;
2022-05-24 08:55:57 +03:00
2022-09-27 16:45:50 -04:00
//Check for a second person and create a second database entry if neccessary
2022-11-16 16:53:24 -05:00
if ( command . options && command . options . length > 0 && command . options [ 0 ]. type == "USER" ) {
ecoimport . CreateNewCollection ( interaction , client , server , command . options [ 0 ]. value );
2022-09-27 16:45:50 -04:00
}
2022-05-28 16:55:56 +03:00
2022-07-31 10:11:55 +03:00
//#endregion
const db = client . db ( String ( server ));
const dbo = db . collection ( id );
2022-05-24 08:55:57 +03:00
2022-07-31 10:11:55 +03:00
//Check if the client is currently in a game and act accordingly
2022-05-28 16:55:56 +03:00
//#region Check Game
2022-09-02 15:27:54 -04:00
dbo . find ({ "game" : { $exists : true }}). toArray ( async function ( err , docs ) {
2022-07-31 10:11:55 +03:00
if ( err ) { return console . log ( err ); }
let doc = docs [ 0 ];
let game = null ;
if ( doc ) { game = doc . game ; }
2022-05-28 16:55:56 +03:00
//#endregion
2022-05-24 08:55:57 +03:00
2022-05-28 16:55:56 +03:00
//#region non-game-specific commands
2022-07-31 10:11:55 +03:00
//For TWO+ PLAYER games only!!!
2022-11-16 16:53:24 -05:00
if ( commandName == 'accept' ) {
const args = interaction . customId . split ( '|' );
// console.log(interaction.message.interaction);
// console.log(interaction.user);
// return console.log(args);
2022-05-28 16:55:56 +03:00
2022-07-31 10:11:55 +03:00
//Should also check if the player is already playing a game!!!
2022-11-16 16:53:24 -05:00
if ( ! acceptIsValid ( bot , interaction . user , interaction , args [ 1 ], interaction . message )) { return ; }
2022-05-25 22:33:42 +03:00
2022-07-31 10:11:55 +03:00
//Get the opponent
2022-11-16 16:53:24 -05:00
const other_id = interaction . message . interaction . user . id ;
const other = db . collection ( other_id );
// return console.log(args, interaction.message.interaction);
let newCommand = interaction . message . interaction . commandName . split ( " " )[ 1 ];
2022-07-31 10:11:55 +03:00
//#region BOT SECTION
2022-05-28 16:55:56 +03:00
2022-07-31 10:11:55 +03:00
//Store both IDs in the database (for turns)
let name_first = await bot . users . cache . get ( id );
2022-11-16 16:53:24 -05:00
let name_second = await bot . users . cache . get ( other_id );
2022-05-28 16:55:56 +03:00
2022-07-31 10:11:55 +03:00
// message.reply(`${first} [${name_first}], ${second} [${name_second}]`); throw 'ERR';
const threadname = ` ${ name_first . username } VS ${ name_second . username } [ ${ newCommand . toUpperCase () } ]` ;
2022-11-16 16:53:24 -05:00
var newObj = { 0 : id , 1 : other_id , turn : 0 , thread : threadname };
2022-07-01 15:00:52 +03:00
2022-07-31 10:11:55 +03:00
if ( newCommand . replaceAll ( " " , "" ). toLowerCase () == 'tictactoe' ) { newCommand = 'Tic Tac Toe' ; }
2022-07-01 15:00:52 +03:00
2022-07-31 10:11:55 +03:00
if ( newCommand === 'Tic Tac Toe' ) {
//Create the new board
let newboard = [ "" , "" , "" , "" , "" , "" , "" , "" , "" ];
newObj . board = newboard ;
let symbols ;
2022-07-01 15:00:52 +03:00
2022-07-31 10:11:55 +03:00
/*DOES NOT WORK
if (msg.content.lastIndexOf('>') == msg.content.lenth) {
symbols = ['X', 'O'];
} else {
symbols = msg.content.substring(msg.content.lastIndexOf('>') + 2).split(' ');
2022-07-01 15:00:52 +03:00
}
2022-07-31 10:11:55 +03:00
*/
newObj . symbols = [ 'X' , 'O' ];
}
serverinbotdb . insertOne ( newObj );
2022-05-28 16:55:56 +03:00
2022-07-31 10:11:55 +03:00
//#endregion
2022-05-28 16:55:56 +03:00
2022-11-16 16:53:24 -05:00
const remAccptBtn = ( msgToDel ) => {
try {
msgToDel . edit ({ components : []});
} catch ( err ) {
console . error ( err );
}
}
2022-07-01 15:00:52 +03:00
2022-07-31 10:11:55 +03:00
//Need this for all 2 player games
2022-11-16 16:53:24 -05:00
const result = Initialize ( bot , dbo , newCommand , interaction . message , id , other_id , other );
2022-07-01 15:00:52 +03:00
2022-07-31 10:11:55 +03:00
if ( newCommand == 'battle' ) {
result . then ( function ( thread ) {
battle . handle ( client , dbo , other , bot , thread , 'initalize' , mongouri , items , null , xp_collection );
2022-11-16 16:53:24 -05:00
remAccptBtn ( interaction . message );
2022-07-31 10:11:55 +03:00
});
} else if ( newCommand == 'Tic Tac Toe' ) {
result . then ( function ( thread ) {
ttt . handle ( client , db , dbo , other , bot , thread , 'initalize' , mongouri , null , xp_collection );
2022-11-16 16:53:24 -05:00
remAccptBtn ( interaction . message );
2022-07-31 10:11:55 +03:00
});
2022-05-28 16:55:56 +03:00
}
2022-11-16 16:53:24 -05:00
} else if ( commandName == 'quit' ) {
const channel = bot . channels . cache . get ( interaction . channel . parentId );
2022-07-31 10:11:55 +03:00
//Remove the turn counter from the bot's database
serverinbotdb . deleteOne ({ 0 : id } || { 1 : id });
if ( doc . opponent != null ) {
// let other = message.guild.members.cache.get(doc.opponent);
let other = db . collection ( doc . opponent );
2022-11-16 16:53:24 -05:00
channel . send ( `<@ ${ interaction . user . id } > has quit a game of " ${ game } " with <@ ${ doc . opponent } >!` );
winGame ( client , bot , db , other , xp_collection , interaction );
2022-07-31 10:11:55 +03:00
} else {
2022-11-16 16:53:24 -05:00
loseGame ( dbo , xp_collection , interaction , bot );
channel . send ( `<@ ${ interaction . user . id } > has quit a game of " ${ game } "!` );
2022-05-28 16:55:56 +03:00
}
2022-07-31 10:11:55 +03:00
}
2022-11-16 16:53:24 -05:00
else if ( commandName == 'status' ) {
getGame ( interaction , args , db );
} else if ( commandName == 'hpmp' ) {
hpmp ( interaction , commandName , dbo );
2022-07-31 10:11:55 +03:00
} else if ( command == 'equip' ) {
// equipItem(client, bot, db, dbo, message);
2022-11-16 16:53:24 -05:00
// This does not work
equip ( interaction , args , command , dbo , bot , items );
2022-07-31 10:11:55 +03:00
} else if ( command == 'classes' ) {
2022-11-16 16:53:24 -05:00
// This does not work
2022-07-31 10:11:55 +03:00
presentClasses ( message , args [ 1 ]);
}
2022-05-28 16:55:56 +03:00
//#endregion
//#region game-specific commands
2022-07-31 10:11:55 +03:00
else {
2022-11-16 16:53:24 -05:00
if ( commandName == undefined ) { return interaction . reply ( "Please specify a game or use _!game help_" ); }
2022-07-31 10:11:55 +03:00
//Make change to new name if necessary
2022-11-16 16:53:24 -05:00
if ( commandName . replaceAll ( " " , "" ). toLowerCase () == 'tictactoe' ) { commandName = 'Tic Tac Toe' ; }
2022-07-31 10:11:55 +03:00
2022-11-16 16:53:24 -05:00
//RETURN TO THIS LATER
if ( game == 'battle' || commandName == 'battle' ) {
if ( ! bot . inDebugMode ) { return interaction . reply ( "This command is currently in development!" ); }
2022-07-31 10:11:55 +03:00
2022-11-16 16:53:24 -05:00
const row = new MessageActionRow ()
. addComponents (
new MessageButton ()
. setCustomId ( `gameaccept| ${ command . options [ 0 ]. value } | ${ interaction . user . id } ` )
. setLabel ( 'Accept Invite' )
. setStyle ( 'SUCCESS' )
);
const content = { content : ` ${ command . options [ 0 ]. user } , ${ interaction . user } has invited you to play _"Tic Tac Toe"_. Click the button to accept the invitation!` , components : [ row ]};
interaction . reply ( content ). catch (( err ) => interaction . channel . send ( content ));
} else if ( game == 'Tic Tac Toe' || commandName == 'Tic Tac Toe' ) {
const row = new MessageActionRow ()
. addComponents (
new MessageButton ()
. setCustomId ( `gameaccept| ${ command . options [ 0 ]. value } | ${ interaction . user . id } ` )
. setLabel ( 'Accept Invite' )
. setStyle ( 'SUCCESS' )
);
const content = { content : ` ${ command . options [ 0 ]. user } , ${ interaction . user } has invited you to play _"Tic Tac Toe"_. Click the button to accept the invitation!` , components : [ row ]};
interaction . reply ( content ). catch (( err ) => interaction . channel . send ( content ));
} else if ( game == 'trivia' || commandName == 'trivia' ) {
trivia . execute ( interaction , Discord , client , bot );
} else if ( game == "minesweeper" || commandName == 'minesweeper' ) {
if ( game == "minesweeper" && commandName == 'minesweeper' ) {
2022-09-02 15:27:54 -04:00
return message . reply ( "You're already in a game!" );
}
2022-11-16 16:53:24 -05:00
const threadname = ` ${ interaction . user . username } is playing Minesweeper` ;
const thread = await interaction . channel . threads . create ({
2022-09-02 15:27:54 -04:00
name : threadname ,
// type: 'GUILD_PRIVATE_THREAD',
autoArchiveDuration : 60 ,
reason : `N/A` ,
});
2022-11-16 16:53:24 -05:00
mnswpr . handle ( bot , interaction , thread );
2022-07-31 10:11:55 +03:00
}
2022-05-24 08:55:57 +03:00
2022-07-31 10:11:55 +03:00
//Catch statement (invalid command)
else {
2022-11-16 16:53:24 -05:00
interaction . reply ( `'/game ${ commandName } ' is not a command!` );
2022-05-24 08:55:57 +03:00
}
2022-07-31 10:11:55 +03:00
}
2022-05-28 16:55:56 +03:00
//#endregion
2022-05-24 08:55:57 +03:00
});
2022-05-28 16:55:56 +03:00
}, allGames , in_game_redirector
2022-06-17 16:41:02 +03:00
}
//#endregion