2023-01-25 08:07:41 +00:00
const dotenv = require ( 'dotenv' ) ;
dotenv . config ( ) ;
const debugMode = process . env . DEBUG || true ;
const mysql = require ( 'mysql' ) ;
2023-02-20 01:42:14 +00:00
const { GuildInfo } = require ( './CustomClasses.js' ) ;
2023-01-25 08:07:41 +00:00
/ * T a b l e S t r u c t u r e s
guild _info
+ -- -- -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- - + -- -- -- + -- -- - + -- -- -- -- - + -- -- -- -- -- -- -- -- +
| Field | Type | Null | Key | Default | Extra |
+ -- -- -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- - + -- -- -- + -- -- - + -- -- -- -- - + -- -- -- -- -- -- -- -- +
| guild _id | varchar ( 50 ) | NO | PRI | NULL | auto _increment |
| tree _name | varchar ( 100 ) | NO | | | |
| tree _height | varchar ( 10 ) | NO | | 0 | |
| tree _message _id | varchar ( 50 ) | NO | | | |
| tree _channel _id | varchar ( 50 ) | NO | | | |
| leaderboard _message _id | varchar ( 50 ) | NO | | | |
| leaderboard _channel _id | varchar ( 50 ) | NO | | | |
+ -- -- -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- - + -- -- -- + -- -- - + -- -- -- -- - + -- -- -- -- -- -- -- -- +
* /
/ *
leaderboard
+ -- -- -- -- -- -- - + -- -- -- -- -- -- -- + -- -- -- + -- -- - + -- -- -- -- - + -- -- -- -- -- -- -- -- +
| Field | Type | Null | Key | Default | Extra |
+ -- -- -- -- -- -- - + -- -- -- -- -- -- -- + -- -- -- + -- -- - + -- -- -- -- - + -- -- -- -- -- -- -- -- +
| id | int ( 10 ) | NO | PRI | NULL | auto _increment |
| guild _id | varchar ( 50 ) | NO | | | |
| tree _name | varchar ( 100 ) | NO | | | |
| tree _rank | int ( 10 ) | NO | | | |
| tree _height | int ( 10 ) | NO | | 1 | |
| has _pin | tinyint ( 1 ) | NO | | 0 | |
| timestamp | varchar ( 50 ) | NO | | | |
+ -- -- -- -- -- -- - + -- -- -- -- -- -- -- + -- -- -- + -- -- - + -- -- -- -- - + -- -- -- -- -- -- -- -- +
* /
module . exports = {
2023-02-20 01:42:14 +00:00
getGuildInfo ( guildId ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-02-20 01:42:14 +00:00
// Get a server's tree information from the database
const selectGuildInfoQuery = ` SELECT * FROM guild_info WHERE guild_id = ${ db . escape ( guildId ) } ` ;
// TODO run this query and return a promise then structure the output into a GuildInfo object. resolve with { "status": , "data": guildInfo }
2023-01-25 08:07:41 +00:00
return new Promise ( ( resolve , reject ) => {
2023-02-20 01:42:14 +00:00
db . query ( selectGuildInfoQuery , ( err , res ) => {
2023-01-25 08:07:41 +00:00
if ( err ) {
2023-02-20 01:42:14 +00:00
console . error ( err ) ;
reject ( "Error fetching guild information: " + err . message ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
return ;
}
2023-02-20 01:42:14 +00:00
row = res [ 0 ] ;
const guildInfo = new GuildInfo ( )
2023-06-17 00:01:29 +00:00
. setIds ( row . guild _id , row . owner _id )
2023-02-20 01:42:14 +00:00
. setName ( row . tree _name )
. setHeight ( row . tree _height )
. setTreeMessage ( row . tree _message _id , row . tree _channel _id )
. setLeaderboardMessage ( row . leaderboard _message _id , row . leaderboard _channel _id )
. setReminders ( row . water _message , row . fruit _message , row . reminder _channel _id , row . watch _channel _id , row . notifications _enabled )
2023-03-11 20:12:34 +00:00
. setRoles ( row . water _role _id , row . fruit _role _id )
. setCompareMessage ( row . compare _channel _id , row . compare _message _id ) ;
2023-02-20 01:42:14 +00:00
db . end ( ) ;
resolve ( guildInfo ) ;
2023-01-25 08:07:41 +00:00
} ) ;
} ) ;
2023-02-01 03:51:10 +00:00
} ,
2023-02-20 01:42:14 +00:00
getAllGuildInfos ( ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-02-01 03:51:10 +00:00
// Get a server's tree information from the database
2023-02-20 01:42:14 +00:00
const query = 'SELECT * FROM guild_info' ;
2023-01-25 08:07:41 +00:00
// TODO run this query and return a promise then structure the output into a GuildInfo object. resolve with { "status": , "data": guildInfo }
return new Promise ( ( resolve , reject ) => {
2023-02-20 01:42:14 +00:00
db . query ( query , ( err , res ) => {
2023-01-25 08:07:41 +00:00
if ( err ) {
console . error ( err ) ;
2023-02-20 01:42:14 +00:00
reject ( "Error fetching all guild infos: " + err . message ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
return ;
}
if ( res . length == 0 ) {
2023-02-20 01:42:14 +00:00
reject ( "There are no servers yet!" ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
return ;
}
2023-02-20 01:42:14 +00:00
let guildInfos = [ ] ;
for ( let i = 0 ; i < res . length ; i ++ ) {
let row = res [ i ] ;
guildInfos . push ( new GuildInfo ( )
2023-06-17 00:01:29 +00:00
. setIds ( row . guild _id , row . owner _id )
2023-02-20 01:42:14 +00:00
. setName ( row . tree _name )
. setHeight ( row . tree _height )
. setTreeMessage ( row . tree _message _id , row . tree _channel _id )
. setLeaderboardMessage ( row . leaderboard _message _id , row . leaderboard _channel _id )
. setReminders ( row . water _message , row . fruit _message , row . reminder _channel _id , row . watch _channel _id , row . notifications _enabled )
. setRoles ( row . water _role _id , row . fruit _role _id )
2023-03-11 20:12:34 +00:00
. setCompareMessage ( row . compare _channel _id , row . compare _message _id )
2023-02-20 01:42:14 +00:00
) ;
}
2023-07-10 23:46:30 +00:00
// console.log(res.length + " // " + guildInfos.length);
2023-02-20 01:42:14 +00:00
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-02-20 01:42:14 +00:00
resolve ( guildInfos ) ;
2023-01-25 08:07:41 +00:00
} ) ;
} ) ;
2023-02-01 03:51:10 +00:00
} ,
2023-02-20 01:42:14 +00:00
setGuildInfo ( query ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-01-25 08:07:41 +00:00
return new Promise ( ( resolve , reject ) => {
2023-02-20 01:42:14 +00:00
db . query ( query , ( err , res ) => {
2023-01-25 08:07:41 +00:00
if ( err ) {
2023-02-20 01:42:14 +00:00
console . error ( err + "\n" + query ) ;
2023-01-25 08:07:41 +00:00
reject ( "Error setting the guild info: " + err . message ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
return ;
}
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-06-03 21:48:56 +00:00
// console.log("Updated the database");
2023-02-20 01:42:14 +00:00
resolve ( ) ;
2023-01-25 08:07:41 +00:00
} ) ;
} ) ;
2023-02-01 03:51:10 +00:00
} ,
setTreeInfo ( guildInfo ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-01-25 08:07:41 +00:00
// Returns a Promise, resolve({ "status": "", "data": null })
// guildInfo = { "guildId": "123", "treeName": "name", "treeHeight": 123, "treeMessageId": "123", "treeChannelId": "123", "leaderboardMessageId": "123", "leaderboardChannelId": "123"}
2023-02-01 03:51:10 +00:00
// Set a server's tree information in the database)
2023-01-25 08:07:41 +00:00
const insertGuildInfoQuery = ` INSERT INTO guild_info (guild_id, tree_name, tree_height, tree_message_id, tree_channel_id) VALUES ( ${ db . escape ( guildInfo . guildId ) } , ${ db . escape ( guildInfo . treeName ) } , ${ db . escape ( guildInfo . treeHeight ) } , ${ db . escape ( guildInfo . treeMessageId ) } , ${ db . escape ( guildInfo . treeChannelId ) } ) ON DUPLICATE KEY UPDATE tree_name = ${ db . escape ( guildInfo . treeName ) } ,tree_height = ${ db . escape ( guildInfo . treeHeight ) } ,tree_message_id = ${ db . escape ( guildInfo . treeMessageId ) } ,tree_channel_id = ${ db . escape ( guildInfo . treeChannelId ) } ` ;
// TODO run this query and return a promise, then resolve with { "status": , "data": null }
return new Promise ( ( resolve , reject ) => {
db . query ( insertGuildInfoQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
reject ( "Error setting the guild info: " + err . message ) ;
return ;
}
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-03-11 20:12:34 +00:00
console . log ( "Updated the database" ) ;
2023-01-25 08:07:41 +00:00
resolve ( { "status" : "Successfully set the guild information" , "data" : null } ) ;
} ) ;
} ) ;
2023-02-01 03:51:10 +00:00
} ,
setLeaderboardInfo ( guildInfo ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-01-25 08:07:41 +00:00
// Returns a Promise, resolve({ "status": "", "data": null })
// guildInfo = { "guildId": "123", "treeName": "name", "treeHeight": 123, "treeMessageId": "123", "treeChannelId": "123", "leaderboardMessageId": "123", "leaderboardChannelId": "123"}
2023-02-01 03:51:10 +00:00
// Set a server's tree information in the database
2023-01-25 08:07:41 +00:00
const insertGuildInfoQuery = ` INSERT INTO guild_info (guild_id, leaderboard_message_id, leaderboard_channel_id) VALUES ( ${ db . escape ( guildInfo . guildId ) } , ${ db . escape ( guildInfo . leaderboardMessageId ) } , ${ db . escape ( guildInfo . leaderboardChannelId ) } ) ON DUPLICATE KEY UPDATE leaderboard_message_id = ${ db . escape ( guildInfo . leaderboardMessageId ) } ,leaderboard_channel_id = ${ db . escape ( guildInfo . leaderboardChannelId ) } ` ;
// TODO run this query and return a promise, then resolve with { "status": , "data": null }
return new Promise ( ( resolve , reject ) => {
db . query ( insertGuildInfoQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
reject ( "Error setting the guild info: " + err . message ) ;
return ;
}
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
resolve ( { "status" : "Successfully set the guild information" , "data" : null } ) ;
} ) ;
} ) ;
2023-02-01 03:51:10 +00:00
} ,
2023-01-25 08:07:41 +00:00
deleteGuildInfo ( guildId ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-01-25 08:07:41 +00:00
// Returns a Promise, resolve({ "status": "", "data": null })
// guildInfo = { "guildId": "123", "treeName": "name", "treeHeight": 123, "treeMessageId": "123", "treeChannelId": "123", "leaderboardMessageId": "123", "leaderboardChannelId": "123"}
2023-02-01 03:51:10 +00:00
// Set a server's tree information in the database
2023-01-25 08:07:41 +00:00
const deleteGuildInfoQuery = ` DELETE FROM guild_info WHERE guild_id = ${ db . escape ( guildId ) } ` ;
// TODO run this query and return a promise, then resolve with { "status": , "data": null }
return new Promise ( ( resolve , reject ) => {
db . query ( deleteGuildInfoQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
reject ( "Error deleting the guild info: " + err . message ) ;
return ;
}
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
resolve ( { "status" : "Successfully deleted the guild information" , "data" : null } ) ;
} ) ;
} ) ;
} ,
2023-02-01 03:51:10 +00:00
getLeaderboard ( guildId ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-01-25 08:07:41 +00:00
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
const selectLeaderboardQuery = ` SELECT id, tree_name, tree_rank, tree_height, has_pin FROM leaderboard WHERE guild_id = ${ db . escape ( guildId ) } ORDER BY id DESC LIMIT 10 ` ;
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise ( ( resolve , reject ) => {
db . query ( selectLeaderboardQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
reject ( "Error fetching the most recent leaderboard: " + err . message ) ;
return ;
}
let leaderboard = [ ] ;
res . forEach ( row => {
leaderboard . push ( {
"treeName" : row . tree _name ,
"treeRank" : row . tree _rank ,
"treeHeight" : row . tree _height ,
"hasPin" : row . has _pin
} ) ;
} ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
resolve ( { "status" : "Successfully fetched leaderboard." , "data" : leaderboard } ) ;
} ) ;
} ) ;
2023-02-01 03:51:10 +00:00
} ,
2023-01-25 08:07:41 +00:00
uploadLeaderboard ( leaderboard ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-02-01 03:51:10 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-25 09:53:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-01-25 08:07:41 +00:00
// Returns a Promise, resolve({ "status": "", "data": res })
// leaderboard = { "guildId": 1234, "entries": [ { "treeHeight": 12, "treeRank": 34, "treeName": "name", "hasPin": false }, {...}, {...} ] }
const insertLeaderboardQuery = "INSERT INTO `leaderboard` (guild_id, tree_name, tree_rank, tree_height, has_pin) VALUES ?" ;
const leaderboardValues = [ ] ;
leaderboard . entries . forEach ( ranking => {
leaderboardValues . push ( [ leaderboard . guildId , ranking . treeName , ranking . treeRank , ranking . treeHeight , ranking . hasPin ] ) ;
} ) ;
return new Promise ( ( resolve , reject ) => {
db . query ( insertLeaderboardQuery , [ leaderboardValues ] , ( err , res ) => {
if ( err ) {
reject ( "Error uploading the leaderboard: " + err . message ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
console . error ( err ) ;
return ;
}
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
resolve ( { "status" : "Successfully uploaded the leaderboard" , "data" : res } ) ;
} ) ;
} ) ;
2023-01-25 09:53:49 +00:00
}
2023-06-18 20:51:35 +00:00
} ;