2022-06-10 16:19:47 +03:00
const { Client , Intents , MessageActionRow , MessageButton , MessageSelectMenu } = require ( 'discord.js' ) ;
2022-02-17 21:42:57 -05:00
const Discord = require ( 'discord.js' ) ;
2022-05-12 17:38:08 +03:00
const { MongoClient , ServerApiVersion } = require ( 'mongodb' ) ;
const fs = require ( 'fs' ) ;
2022-05-28 16:55:56 +03:00
const turnManager = require ( './commands/turnManager.js' ) ;
2022-06-10 16:19:47 +03:00
const { welcome } = require ( './commands/admin/welcome.js' ) ;
2022-05-12 17:38:08 +03:00
const { exit } = require ( 'process' ) ;
const BASE _LVL _XP = 20 ;
2022-05-20 12:19:27 +03:00
//Token area
//Adding integration for development mode
let token ;
2022-05-25 22:33:42 +03:00
let IDM = false ;
2022-06-11 17:53:44 +03:00
let home _server ;
2022-05-20 12:19:27 +03:00
if ( process . env . token != undefined ) {
//Use "setx NAME VALUE" in the local powershell terminal to set
token = process . env . token ;
2022-06-11 17:53:44 +03:00
home _server = process . env . home _server ;
2022-05-20 12:19:27 +03:00
} else {
token = require ( './config.json' ) . token ;
2022-06-11 17:53:44 +03:00
home _server = require ( './config.json' ) . home _server ;
2022-05-25 22:33:42 +03:00
IDM = true ;
2022-05-20 12:19:27 +03:00
}
2022-04-30 17:41:44 -04:00
// const { token } = require('./config.json');
2022-04-30 17:02:26 -04:00
//Heroku part
2022-05-12 17:48:49 +03:00
// const { token } = process.env.token;
2022-04-17 08:51:30 -04:00
2022-04-30 15:51:55 -04:00
const bot = new Client ( {
2022-02-17 21:42:57 -05:00
intents : [
Intents . FLAGS . GUILDS ,
Intents . FLAGS . GUILD _MESSAGES ,
2022-04-30 15:51:55 -04:00
Intents . FLAGS . GUILD _MESSAGE _REACTIONS ,
2022-05-13 12:14:24 +03:00
Intents . FLAGS . GUILD _VOICE _STATES ,
2022-06-10 16:19:47 +03:00
Intents . FLAGS . GUILD _EMOJIS _AND _STICKERS ,
Intents . FLAGS . GUILD _PRESENCES ,
Intents . FLAGS . GUILD _MEMBERS
2022-02-17 21:42:57 -05:00
] ,
} ) ;
2022-05-18 21:34:58 +03:00
const prefix = '!' ;
bot . prefix = new String ;
bot . prefix = prefix ;
2022-05-25 22:33:42 +03:00
bot . inDebugMode = IDM ;
2022-06-11 17:53:44 +03:00
bot . home _server = home _server ;
2022-02-17 21:42:57 -05:00
2022-05-09 12:11:18 +03:00
//MongoDB integration
2022-05-20 12:19:27 +03:00
//Development support
let mongouritemp ;
if ( process . env . MONGODB _URI ) {
mongouritemp = process . env . MONGODB _URI ;
} else {
mongouritemp = require ( './config.json' ) ;
}
const mongouri = mongouritemp ;
2022-05-12 17:38:08 +03:00
const { connect } = require ( 'mongoose' ) ;
bot . on ( "guildCreate" , guild => {
2022-05-14 20:40:58 +03:00
guild . roles . create ( { name : 'Selmer Bot Mod' } ) ;
2022-05-28 16:55:56 +03:00
2022-06-10 16:19:47 +03:00
//const role = guild.roles.cache.find((role) => role.name === 'Selmer Bot Mod'); // member.roles.cache.has('role-id-here');
const server = bot . guilds . cache . get ( guild . id ) ;
const owner = server . members . fetch ( guild . ownerId ) . then ( function ( owner ) {
owner . send ( 'Thank you for adding Selmer Bot to your server!\nPlease give people you want to have access to Selmer Bot\'s restricted commands the "_Selmer Bot Mod_" role.' ) ;
owner . send ( 'To help set up Selmer Bot to work better with your server, use _!setup help_ in a channel Selmer Bot is in!' ) ;
} ) ;
2022-05-14 20:40:58 +03:00
2022-06-10 16:19:47 +03:00
//Set up the server
const client = new MongoClient ( mongouri , { useNewUrlParser : true , useUnifiedTopology : true , serverApi : ServerApiVersion . v1 } ) ;
client . connect ( err => {
if ( err ) { return console . log ( err ) ; }
const dbo = client . db ( guild . id ) . collection ( 'SETUP' ) ;
2022-06-10 20:48:43 +03:00
dbo . insertMany ( [ { _id : 'WELCOME' , 'welcomechannel' : null , 'welcomemessage' : null , 'welcomebanner' : null } ] ) ;
2022-06-10 16:19:47 +03:00
} ) ;
client . close ( ) ;
2022-05-12 17:38:08 +03:00
} ) ;
2022-06-10 16:19:47 +03:00
2022-05-12 17:38:08 +03:00
//MongoDB Integration end
// let item = items.filter(function (item) { return item.name.toLowerCase() == 'grapes'; });
2022-05-09 12:11:18 +03:00
2022-04-30 15:51:55 -04:00
bot . commands = new Discord . Collection ( ) ;
2022-02-17 21:42:57 -05:00
const commandFiles = fs . readdirSync ( './commands/' ) . filter ( file => file . endsWith ( '.js' ) ) ;
2022-05-09 12:14:23 +03:00
2022-05-13 22:29:54 +03:00
bot . commands = new Discord . Collection ( ) ;
fs . readdirSync ( './commands' )
. forEach ( dir => {
2022-05-27 14:58:30 +03:00
if ( dir != 'db' && ! dir . endsWith ( '.js' ) ) {
2022-05-24 08:55:57 +03:00
fs . readdirSync ( ` ./commands/ ${ dir } ` )
. filter ( file => file . endsWith ( '.js' ) )
. forEach ( file => {
const command = require ( ` ./commands/ ${ dir } / ${ file } ` ) ;
bot . commands . set ( command . name , command ) ;
} ) ;
}
2022-05-13 22:29:54 +03:00
} ) ;
2022-05-13 22:03:05 +03:00
2022-05-24 08:55:57 +03:00
//Set these two manually because all the seperate games can't be included in the command list (all managed by the 'game' file)
2022-06-10 16:19:47 +03:00
let temp _command = require ( "./commands/db/econ.js" ) ;
2022-05-28 16:55:56 +03:00
const { STATE } = require ( './commands/db/econ.js' ) ;
2022-06-10 16:19:47 +03:00
bot . commands . set ( 'econ' , temp _command ) ;
temp _command = require ( './commands/db/game.js' ) ;
bot . commands . set ( 'game' , temp _command ) ;
2022-05-24 08:55:57 +03:00
2022-05-12 17:38:08 +03:00
// const econFiles = fs.readdirSync('./commands/inventory').filter(file => file.endsWith('.js'));;
2022-04-30 17:41:44 -04:00
// const currency = new Discord.Collection();
// const { Users } = require('./commands/currency/dbObjects.js');
// i++;
2022-02-17 21:42:57 -05:00
2022-05-12 17:38:08 +03:00
//XP Table section
let xp _collection = new Map ( ) ;
let items ;
2022-02-17 21:42:57 -05:00
2022-04-30 15:51:55 -04:00
bot . on ( 'ready' , async ( ) => {
2022-05-12 17:38:08 +03:00
//Make then copy the shop
const client = new MongoClient ( mongouri , { useNewUrlParser : true , useUnifiedTopology : true , serverApi : ServerApiVersion . v1 } ) ;
client . connect ( err => {
const shop = client . db ( "main" ) . collection ( "shop" ) ;
shop . find ( ) . toArray ( function ( err , itemstemp ) {
if ( err ) throw err ;
items = [ ... itemstemp ] ;
client . close ( ) ;
} ) ;
2022-05-24 08:55:57 +03:00
//Srt status and Activity (idle and listening to !help)
bot . user . setActivity ( ` ${ bot . prefix } help ` , { type : "LISTENING" } ) ;
// bot.user.setStatus('idle');
2022-05-12 17:38:08 +03:00
} ) ;
//Note the xp numbers are a little wonky on levels 6, 8 and 13 (why though?)
//See https://stackoverflow.com/questions/72212928/why-are-the-differences-between-my-numbers-inconsistent-sort-of-compund-interes
for ( let i = 1 ; i < 101 ; i ++ ) {
// xp_collection.set(i, BASE_LVL_XP * .1);
let amount = BASE _LVL _XP * ( Math . ceil ( Math . pow ( ( 1.1 ) , ( 2 * i ) ) ) + i ) ;
xp _collection . set ( i + 1 , amount ) ;
}
2022-04-17 08:51:30 -04:00
2022-05-13 12:14:24 +03:00
//Reaction map area
2022-05-25 22:33:42 +03:00
if ( ! bot . inDebugMode ) {
2022-05-20 12:19:27 +03:00
console . log ( 'SLEEMER BOT ONLINE!!!!! OH MY GOD OH MY GOD!!!' ) ;
} else {
console . log ( "Testing testing 1 2 5..." ) ;
}
2022-02-17 21:42:57 -05:00
} ) ;
2022-05-27 14:58:30 +03:00
//Button Section
2022-05-28 16:55:56 +03:00
bot . on ( 'interactionCreate' , async interaction => {
2022-06-11 17:53:44 +03:00
2022-06-10 16:19:47 +03:00
if ( interaction . isButton ( ) ) {
const battlecommandlist = [ 'ATTACK' , 'HEAL' , 'DEFEND' , 'ITEMS' ] ;
const client = new MongoClient ( mongouri , { useNewUrlParser : true , useUnifiedTopology : true , serverApi : ServerApiVersion . v1 } ) ;
client . connect ( err => {
if ( battlecommandlist . indexOf ( interaction . customId ) != - 1 ) {
let current _user = turnManager . getTurn ( client , bot , interaction ) ;
current _user . then ( function ( result ) {
const id = result [ 0 ] ;
const doc = result [ 1 ] ;
const threadname = doc . thread ;
const dbo = client . db ( interaction . guildId + '[ECON]' ) . collection ( id ) ;
dbo . find ( { 'state' : { $exists : true } } ) . toArray ( async function ( err , docs ) {
if ( interaction . user . id == id ) {
await interaction . deferReply ( ) ;
//Check State
if ( docs [ 0 ] . state == STATE . FIGHTING ) {
//Do turn stuff
bot . commands . get ( 'game' ) . in _game _redirector ( bot , interaction , threadname , doc , client , mongouri , items , xp _collection ) ;
}
//remove the old interation message
2022-06-11 17:53:44 +03:00
await interaction . message . delete ( ) ;
2022-06-10 16:19:47 +03:00
2022-06-11 17:53:44 +03:00
if ( interaction . customId . toLowerCase ( ) != 'heal' ) {
interaction . editReply ( ` <@ ${ interaction . user . id } > used _ ${ interaction . customId . toLowerCase ( ) } _! ` ) ;
}
2022-06-10 16:19:47 +03:00
} else {
console . log ( "It's not your turn!" ) ;
}
} ) ;
} ) ;
2022-06-11 17:53:44 +03:00
} //else ifs here
2022-06-10 16:19:47 +03:00
} ) ;
client . close ( ) ;
}
2022-06-11 17:53:44 +03:00
else if ( interaction . isSelectMenu ( ) ) {
if ( interaction . customId . toLowerCase ( ) . indexOf ( '|heal' ) != - 1 ) {
const client = new MongoClient ( mongouri , { useNewUrlParser : true , useUnifiedTopology : true , serverApi : ServerApiVersion . v1 } ) ;
client . connect ( err => {
const id = interaction . customId . substring ( 0 , interaction . customId . indexOf ( '|' ) )
if ( id != interaction . user . id ) { return ; }
let current _user = turnManager . getTurn ( client , bot , interaction ) ;
current _user . then ( function ( result ) {
const doc = result [ 1 ] ;
const threadname = doc . thread ;
const dbo = client . db ( interaction . guildId + '[ECON]' ) . collection ( id ) ;
dbo . find ( { 'state' : { $exists : true } } ) . toArray ( async function ( err , docs ) {
if ( interaction . user . id == id ) {
await interaction . deferReply ( ) ;
2022-06-10 16:19:47 +03:00
2022-06-11 17:53:44 +03:00
//Check State
if ( docs [ 0 ] . state == STATE . FIGHTING ) {
interaction . customId = 'usepotion' ;
//Do turn stuff
bot . commands . get ( 'game' ) . in _game _redirector ( bot , interaction , threadname , doc , client , mongouri , items , xp _collection ) ;
}
/*
let srv = bot.guilds.cache.get(bot.home_server).emojis.cache;
let sname;
if (interaction.customId.toLowerCase() == 'heal' || interaction.customId.toLowerCase() == 'mp') {
if (interaction.values[0] == 'HP Potion') { sname = 'healing_potion' }
else if (interaction.values[0] == 'MP Potion') { sname = 'mana_potion' }
else if (interaction.values[0] == 'Super HP Potion') { sname = 'superior_healing_potion' }
else if (interaction.values[0] == 'Super MP Potion') { sname = 'superior_mana_potion' }
}
// emj = srv.find((g) => { return g.name == sname });
// console.log(sname, srv);*/
interaction . editReply ( ` <@ ${ interaction . user . id } > used a _ ${ interaction . values [ 0 ] } _! ` ) ;
//remove the old interation message
await interaction . message . delete ( ) ;
} else {
console . log ( "It's not your turn!" ) ;
}
} ) ;
} ) ;
//Get all chars from after "CUSTOM|" to the end of the str
// let name = item.icon.substr(7, item.icon.length - 6);
} ) ;
}
2022-06-10 16:19:47 +03:00
}
} ) ;
//Welcome new members
bot . on ( 'guildMemberAdd' , async ( member ) => {
//Check for impartial data
if ( member . partial ) await member . fetch ( ) ;
2022-05-28 16:55:56 +03:00
const client = new MongoClient ( mongouri , { useNewUrlParser : true , useUnifiedTopology : true , serverApi : ServerApiVersion . v1 } ) ;
2022-06-10 16:19:47 +03:00
const guild = bot . guilds . cache . get ( member . guild . id ) ;
2022-05-28 16:55:56 +03:00
client . connect ( err => {
2022-06-10 16:19:47 +03:00
const dbo = client . db ( member . guild . id ) . collection ( 'SETUP' ) ;
dbo . find ( { _id : 'WELCOME' } ) . toArray ( async ( err , docs ) => {
var welcomechannel ;
if ( docs [ 0 ] . welcomechannel == null ) {
welcomechannel = guild . channels . cache . find ( channel => channel . name . toLowerCase ( ) === 'welcome' ) ;
} else {
welcomechannel = guild . channels . cache . get ( docs [ 0 ] . welcomechannel )
}
if ( welcomechannel == null ) {
return console . log ( 'No welcome channel detected' ) ;
}
2022-06-10 20:48:43 +03:00
await welcome ( member , welcomechannel , docs [ 0 ] . welcomemessage ) ;
2022-06-10 16:19:47 +03:00
} )
} )
2022-05-27 14:58:30 +03:00
} ) ;
2022-04-30 15:51:55 -04:00
bot . on ( 'messageCreate' , ( message ) => {
2022-05-25 22:33:42 +03:00
2022-06-11 17:53:44 +03:00
//Special case, testing server (still need the emojis)
2022-06-11 18:47:49 +03:00
if ( ! bot . inDebugMode && message . guild . id == bot . home _server ) { return ; }
2022-06-11 17:53:44 +03:00
2022-02-17 21:42:57 -05:00
//COMMAND AREA
//Check if the prefix exists
if ( ! message . content . startsWith ( prefix ) || message . author . bot ) return ;
2022-05-27 14:58:30 +03:00
2022-02-17 21:42:57 -05:00
const args = message . content . slice ( prefix . length ) . split ( ' ' ) ;
const command = args . shift ( ) . toLowerCase ( ) ;
2022-06-10 16:19:47 +03:00
if ( command == 'welcome' ) {
const row = new MessageActionRow ( )
. addComponents (
new MessageButton ( )
. setCustomId ( 'WELCOME' )
. setLabel ( 'WELCOME' )
. setStyle ( 'PRIMARY' )
) ;
message . channel . send ( { components : [ row ] } ) ;
}
2022-06-11 17:53:44 +03:00
//TEMP
if ( command == 'emj' ) {
let srv = bot . guilds . cache . get ( bot . home _server ) . emojis . cache ;
// console.log(srv);
emj = srv . find ( ( g ) => { return g . name == 'healing_potion' } ) ;
// console.log(emj); exit();
message . channel . send ( ` ${ emj } ` ) ;
}
2022-02-17 21:42:57 -05:00
//Check if the user has sufficient permission
//Performes the command
2022-05-14 20:40:58 +03:00
//Admin section
if ( command == 'reactionrole' ) { bot . commands . get ( command ) . execute ( message , args , Discord , bot ) ; }
2022-05-13 22:29:54 +03:00
2022-05-24 08:55:57 +03:00
else if ( bot . commands . has ( command ) && command != 'ECON' ) {
//Database access is required, change the inputs
2022-06-10 16:19:47 +03:00
if ( command == 'game' || command == 'accept' || command == 'setup' ) {
2022-05-28 16:55:56 +03:00
bot . commands . get ( command ) . execute ( bot , message , args , command , Discord , mongouri , items , xp _collection ) ;
2022-05-24 08:55:57 +03:00
} else {
bot . commands . get ( command ) . execute ( message , args , Discord , Client , bot ) ;
}
}
2022-05-13 22:29:54 +03:00
2022-06-10 16:19:47 +03:00
//Econ and also the catch statement
2022-05-24 08:55:57 +03:00
else { bot . commands . get ( 'econ' ) . execute ( bot , message , args , command , Discord , mongouri , items , xp _collection ) ; }
2022-02-17 21:42:57 -05:00
} )
2022-05-09 10:22:33 +03:00
//Look into integrating MySQL into SelmerBot instead of SQLite
2022-02-17 21:42:57 -05:00
2022-05-12 17:38:08 +03:00
//Last Line(s)
2022-05-12 17:48:49 +03:00
// bot.login(token);
2022-05-12 17:38:08 +03:00
2022-05-20 12:19:27 +03:00
bot . login ( token ) ;