2023-01-25 08:07:41 +00:00
const dotenv = require ( 'dotenv' ) ;
dotenv . config ( ) ;
const debugMode = process . env . DEBUG || true ;
const mysql = require ( 'mysql' ) ;
/ * 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 = {
createGuildTables ( guildId ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +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-31 00:28:12 +00:00
// Create the guild-information and rank-information tables to be used.
2023-01-25 08:07:41 +00:00
const createGuildInfoTableQuery = "CREATE TABLE IF NOT EXISTS guild_info(guild_id VARCHAR(50) NOT NULL, tree_name VARCHAR(100) NOT NULL DEFAULT 'Run /setup where your tree is.', tree_height INT(10) NOT NULL DEFAULT 0, tree_message_id VARCHAR(50) NOT NULL DEFAULT 'Run /setup where your tree is.', tree_channel_id VARCHAR(50) NOT NULL DEFAULT 'Run /setup where your tree is.', leaderboard_message_id VARCHAR(50) NOT NULL DEFAULT 'Run /setup where your leaderboard is.', leaderboard_channel_id VARCHAR(50) NOT NULL DEFAULT 'Run /setup where your leaderboard is.', CONSTRAINT guild_pk PRIMARY KEY (guild_id))" ;
const createLeaderboardTableQuery = "CREATE TABLE IF NOT EXISTS leaderboard(id INT(10) NOT NULL AUTO_INCREMENT,guild_id VARCHAR(50) NOT NULL,tree_name VARCHAR(100) NOT NULL,tree_rank INT(10) NOT NULL,tree_height INT(10) NOT NULL DEFAULT 1,has_pin TINYINT(1) NOT NULL DEFAULT 0,timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, CONSTRAINT id_pk PRIMARY KEY(id))" ;
// TODO run the queries, then add a call to this function at the beginning of main.js or functions.js
return new Promise ( ( resolve , reject ) => {
db . query ( createGuildInfoTableQuery , ( err ) => {
if ( err ) {
reject ( "Error creating the guild_info table: " + err . message ) ;
console . error ( "Offending query: " + createGuildInfoTableQuery ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
return ;
}
db . query ( createLeaderboardTableQuery , ( err ) => {
if ( err ) {
reject ( "Error creating the leaderboard table: " + err . message ) ;
console . error ( "Offending query: " + createLeaderboardTableQuery ) ;
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
resolve ( { "status" : "Successfully checked both tables." , "data" : null } ) ;
db . end ( ) ;
2023-01-25 08:07:41 +00:00
} ) ;
} ) ;
} ) ;
2023-01-31 00:28:12 +00:00
} ,
getGuildInfo ( guildId ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +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-31 00:28:12 +00:00
// Get a server's tree information from the database
2023-01-31 22:23:26 +00:00
const selectGuildInfoQuery = ` SELECT * FROM guild_info WHERE guild_id = ${ db . escape ( guildId ) } ` ;
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 ) => {
db . query ( selectGuildInfoQuery , ( err , res ) => {
if ( err ) {
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 ;
}
/ * c o n s t g u i l d I n f o = { " g u i l d I d " : " 1 2 3 " ,
"treeName" : "name" ,
"treeHeight" : 123 ,
"treeMessageId" : "123" ,
"treeChannelId" : "123" ,
"leaderboardMessageId" : "123" ,
2023-01-27 03:31:48 +00:00
"leaderboardChannelId" : "123" ,
2023-01-27 23:14:01 +00:00
"reminderMessage" : "Abc" ,
"reminderChannelId" : "123" ,
2023-01-31 22:23:26 +00:00
"remindedStatus" : 0 ,
"comparisonMessageId" : "123"
2023-01-25 08:07:41 +00:00
} ; * /
if ( res . length == 0 ) {
reject ( "There is no database entry for your guild yet. Try running /setup" ) ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
2023-01-25 08:07:41 +00:00
return ;
}
row = res [ 0 ] ;
2023-01-31 00:28:12 +00:00
const guildInfo = {
"guildId" : guildId ,
2023-01-25 08:07:41 +00:00
"treeName" : row . tree _name ,
"treeHeight" : row . tree _height ,
"treeMessageId" : row . tree _message _id ,
"treeChannelId" : row . tree _channel _id ,
"leaderboardMessageId" : row . leaderboard _message _id ,
2023-01-27 03:31:48 +00:00
"leaderboardChannelId" : row . leaderboard _channel _id ,
2023-01-28 01:57:40 +00:00
"reminderMessage" : row . ping _role _id ,
"reminderChannelId" : row . ping _channel _id ,
2023-01-31 00:28:12 +00:00
"remindedStatus" : row . reminded _status ,
2023-01-31 22:23:26 +00:00
"reminderOptIn" : row . reminder _optin ,
"comparisonMessageId" : row . comparison _message _id ,
"comparisonChannelId" : row . comparison _channel _id
2023-01-25 08:07:41 +00:00
} ;
2023-01-25 09:53:49 +00:00
db . end ( ) ;
resolve ( { "status" : "Successfully fetched guild information" , "data" : guildInfo } ) ;
2023-01-25 08:07:41 +00:00
} ) ;
} ) ;
2023-01-31 00:28:12 +00:00
} ,
2023-02-06 01:54:56 +00:00
getAllGuildInfos ( ) {
const db = mysql . createConnection ( {
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
// Get a server's tree information from the database
const selectAllGuildInfosQuery = ` SELECT * FROM guild_info ` ;
// 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 ) => {
db . query ( selectAllGuildInfosQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
reject ( "Error fetching all guilds information: " + err . message ) ;
db . end ( ) ;
return ;
}
/ * c o n s t g u i l d I n f o = { " g u i l d I d " : " 1 2 3 " ,
"treeName" : "name" ,
"treeHeight" : 123 ,
"treeMessageId" : "123" ,
"treeChannelId" : "123" ,
"leaderboardMessageId" : "123" ,
"leaderboardChannelId" : "123" ,
"reminderMessage" : "Abc" ,
"reminderChannelId" : "123" ,
"remindedStatus" : 0 ,
"comparisonMessageId" : "123"
} ; * /
if ( res . length == 0 ) {
reject ( "There are no guilds set up yet." ) ;
db . end ( ) ;
return ;
}
let guildInfos = new Array ( ) ;
for ( let i = 0 ; i < res . length ; i ++ ) {
let row = res [ i ] ;
let guildInfo = {
"guildId" : row . guild _id ,
"treeName" : row . tree _name ,
"treeHeight" : row . tree _height ,
"treeMessageId" : row . tree _message _id ,
"treeChannelId" : row . tree _channel _id ,
"leaderboardMessageId" : row . leaderboard _message _id ,
"leaderboardChannelId" : row . leaderboard _channel _id ,
"reminderMessage" : row . ping _role _id ,
"reminderChannelId" : row . ping _channel _id ,
"remindedStatus" : row . reminded _status ,
"reminderOptIn" : row . reminder _optin ,
"comparisonMessageId" : row . comparison _message _id ,
"comparisonChannelId" : row . comparison _channel _id
} ;
guildInfos . push ( guildInfo ) ;
}
db . end ( ) ;
resolve ( { "status" : "Successfully fetched all guilds information" , "data" : guildInfos } ) ;
} ) ;
} ) ;
} ,
2023-01-31 00:28:12 +00:00
setGuildInfo ( guildInfo ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +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-01-31 00:28:12 +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, leaderboard_message_id, leaderboard_channel_id) VALUES ( ${ db . escape ( guildInfo . guildId ) } , ${ db . escape ( guildInfo . treeName ) } , ${ db . escape ( guildInfo . treeHeight ) } , ${ db . escape ( guildInfo . treeMessageId ) } , ${ db . escape ( guildInfo . treeChannelId ) } , ${ db . escape ( guildInfo . leaderboardMessageId ) } , ${ db . escape ( guildInfo . leaderboardChannelId ) } ) 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 ) } ,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 ) ;
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-01-25 08:07:41 +00:00
resolve ( { "status" : "Successfully set the guild information" , "data" : null } ) ;
} ) ;
} ) ;
2023-01-31 00:28:12 +00:00
} ,
setTreeInfo ( guildInfo ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +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-01-31 00:28:12 +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-01-25 08:07:41 +00:00
resolve ( { "status" : "Successfully set the guild information" , "data" : null } ) ;
} ) ;
} ) ;
2023-01-31 00:28:12 +00:00
} ,
setLeaderboardInfo ( guildInfo ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +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-01-31 00:28:12 +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-01-31 00:28:12 +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-01-31 00:28:12 +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-01-31 00:28:12 +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-01-31 00:28:12 +00:00
getLeaderboard ( guildId ) {
2023-01-25 09:53:49 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +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-01-31 00:28:12 +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-01-31 00:28:12 +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-26 04:11:49 +00:00
} ,
2023-01-31 00:28:12 +00:00
get24hTree ( guildId , treeName ) {
2023-01-26 04:11:49 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-26 04:11:49 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
const select24hTreeQuery = ` SELECT id, tree_name, tree_rank, tree_height, has_pin FROM leaderboard WHERE guild_id = ${ db . escape ( guildId ) } AND tree_name = ${ db . escape ( treeName ) } AND timestamp > date_sub(now(), interval 1 day) ORDER BY id ASC LIMIT 1 ` ;
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise ( ( resolve , reject ) => {
db . query ( select24hTreeQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
db . end ( ) ;
reject ( "Error fetching the historic 24hr tree height: " + err . message ) ;
return ;
}
2023-01-28 01:57:40 +00:00
let hist24hTree = { } ;
if ( res . length > 0 ) {
hist24hTree = {
"treeName" : res [ 0 ] . tree _name ,
"treeRank" : res [ 0 ] . tree _rank ,
"treeHeight" : res [ 0 ] . tree _height ,
"hasPin" : res [ 0 ] . has _pin
}
} else {
hist24hTree = {
}
2023-01-26 04:11:49 +00:00
}
2023-01-31 00:28:12 +00:00
2023-01-26 04:11:49 +00:00
db . end ( ) ;
resolve ( { "status" : "Successfully fetched historic 24hr tree." , "data" : hist24hTree } ) ;
} ) ;
} ) ;
2023-01-31 00:28:12 +00:00
} ,
2023-01-27 23:14:01 +00:00
setReminderInfo ( guildId , reminderMessage , reminderChannelId ) {
2023-01-27 03:31:48 +00:00
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-27 03:31:48 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
2023-01-27 23:14:01 +00:00
const insertReminderInfoQuery = ` UPDATE guild_info SET ping_role_id = ${ db . escape ( reminderMessage ) } , ping_channel_id = ${ db . escape ( reminderChannelId ) } WHERE guild_id = ${ db . escape ( guildId ) } ` ;
2023-01-27 03:31:48 +00:00
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise ( ( resolve , reject ) => {
2023-01-27 23:14:01 +00:00
db . query ( insertReminderInfoQuery , ( err , res ) => {
2023-01-27 03:31:48 +00:00
if ( err ) {
console . error ( err ) ;
db . end ( ) ;
2023-01-27 23:14:01 +00:00
reject ( "Error updating the reminder info: " + err . message ) ;
2023-01-27 03:31:48 +00:00
return ;
}
db . end ( ) ;
2023-01-27 23:14:01 +00:00
resolve ( { "status" : ` Successfully set the reminder message to " ${ reminderMessage } " in <# ${ reminderChannelId } > ` , "data" : res } ) ;
} ) ;
} ) ;
} ,
setRemindedStatus ( guildId , remindedStatus ) {
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-27 23:14:01 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
const setRemindedStatusQuery = ` UPDATE guild_info SET reminded_status = ${ db . escape ( remindedStatus ) } WHERE guild_id = ${ db . escape ( guildId ) } ` ;
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise ( ( resolve , reject ) => {
db . query ( setRemindedStatusQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
db . end ( ) ;
reject ( "Error updating the reminded status: " + err . message ) ;
return ;
}
db . end ( ) ;
resolve ( { "status" : ` Successfully set the reminded status to ${ remindedStatus } ` , "data" : res } ) ;
2023-01-31 22:23:26 +00:00
// console.log("Boop: " + remindedStatus);
2023-01-27 23:14:01 +00:00
} ) ;
} ) ;
} ,
setReminderOptIn ( guildId , optIn ) {
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-27 23:14:01 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
const setReminderOptInQuery = ` UPDATE guild_info SET reminder_optin = ${ db . escape ( optIn ) } WHERE guild_id = ${ db . escape ( guildId ) } ` ;
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise ( ( resolve , reject ) => {
db . query ( setReminderOptInQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
db . end ( ) ;
reject ( "Error updating the reminder opt-in status: " + err . message ) ;
return ;
}
db . end ( ) ;
resolve ( { "status" : ` Successfully set the reminder opt-in status to ${ optIn } ` , "data" : res } ) ;
} ) ;
} ) ;
} ,
getOptedInGuilds ( ) {
const db = mysql . createConnection ( {
2023-01-31 00:28:12 +00:00
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
2023-01-27 23:14:01 +00:00
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
2023-01-31 00:28:12 +00:00
// Get a server's tree information from the database
2023-01-31 22:23:26 +00:00
const getOptedInGuildsQuery = ` SELECT * FROM guild_info WHERE reminder_optin = 1 ` ;
2023-01-27 23:14:01 +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 ) => {
db . query ( getOptedInGuildsQuery , ( err , res ) => {
if ( err ) {
console . error ( err ) ;
reject ( "Error fetching guild information: " + err . message ) ;
db . end ( ) ;
return ;
}
/ * c o n s t g u i l d I n f o = { " g u i l d I d " : " 1 2 3 " ,
"treeName" : "name" ,
"treeHeight" : 123 ,
"treeMessageId" : "123" ,
"treeChannelId" : "123" ,
"leaderboardMessageId" : "123" ,
"leaderboardChannelId" : "123" ,
"reminderMessage" : "Abc" ,
"reminderChannelId" : "123" ,
2023-01-31 22:23:26 +00:00
"remindedStatus" : 0 ,
"comparisonMessageId" : "123"
2023-01-27 23:14:01 +00:00
} ; * /
if ( res . length == 0 ) {
2023-01-31 00:28:12 +00:00
resolve ( { "status" : "No servers have opted in yet" } ) ;
2023-01-27 23:14:01 +00:00
db . end ( ) ;
return ;
}
row = res [ 0 ] ;
let guilds = [ ] ;
res . forEach ( row => {
guilds . push ( {
"guildId" : row . guild _id ,
"treeName" : row . tree _name ,
"treeHeight" : row . tree _height ,
"treeMessageId" : row . tree _message _id ,
"treeChannelId" : row . tree _channel _id ,
"leaderboardMessageId" : row . leaderboard _message _id ,
"leaderboardChannelId" : row . leaderboard _channel _id ,
"reminderMessage" : row . ping _role _id ,
"reminderChannelId" : row . ping _channel _id ,
2023-01-31 22:23:26 +00:00
"remindedStatus" : row . reminded _status ,
"comparisonMessageId" : row . comparison _message _id ,
"comparisonChannelId" : row . comparison _channel _id
2023-01-27 23:14:01 +00:00
} ) ;
} ) ;
db . end ( ) ;
resolve ( { "status" : "Successfully fetched guild information" , "data" : guilds } ) ;
2023-01-27 03:31:48 +00:00
} ) ;
} ) ;
2023-01-31 00:28:12 +00:00
} ,
2023-01-31 22:23:26 +00:00
setComparisonMessage ( comparisonMessage , guildId ) {
2023-01-31 00:28:12 +00:00
const db = mysql . createConnection ( {
host : process . env . DBHOST ,
user : process . env . DBUSER ,
password : process . env . DBPASS ,
database : process . env . DBNAME ,
port : process . env . DBPORT
} ) ;
db . connect ( ( err ) => {
if ( err ) throw ` Error connecting to the database: ${ err . message } ` ;
} ) ;
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
2023-02-01 03:30:20 +00:00
const setComparisonMessageQuery = ` UPDATE guild_info SET comparison_message_id = ${ db . escape ( comparisonMessage . id ) } , comparison_channel_id = ${ db . escape ( comparisonMessage . channel . id ) } WHERE guild_id = ${ db . escape ( guildId ) } ` ;
2023-01-31 22:23:26 +00:00
// console.log(JSON.stringify(comparisonMessage));
2023-01-31 00:28:12 +00:00
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise ( ( resolve , reject ) => {
2023-01-31 22:23:26 +00:00
db . query ( setComparisonMessageQuery , ( err , res ) => {
2023-01-31 00:28:12 +00:00
if ( err ) {
console . error ( err ) ;
db . end ( ) ;
reject ( "Error updating the comparison message ID: " + err . message ) ;
return ;
}
db . end ( ) ;
2023-01-31 22:23:26 +00:00
resolve ( { "status" : ` Successfully set the comparison message ID: ${ comparisonMessage } ` , "data" : res } ) ;
2023-01-31 00:28:12 +00:00
} ) ;
} ) ;
2023-01-27 03:31:48 +00:00
}
2023-01-25 08:07:41 +00:00
} ;