add guildData helper library
This commit is contained in:
		
							parent
							
								
									bcc30e8c93
								
							
						
					
					
						commit
						10f1a178bc
					
				
					 1 changed files with 64 additions and 0 deletions
				
			
		
							
								
								
									
										64
									
								
								src/lib/guildData.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/lib/guildData.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,64 @@
 | 
				
			||||||
 | 
					hf.database.run(
 | 
				
			||||||
 | 
					  "CREATE TABLE IF NOT EXISTS guild_data (key STRING PRIMARY KEY, value STRING NOT NULL) WITHOUT ROWID"
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function setGuildData(id, key, value) {
 | 
				
			||||||
 | 
					  return new Promise((resolve, reject) => {
 | 
				
			||||||
 | 
					    hf.database.run(
 | 
				
			||||||
 | 
					      "REPLACE INTO guild_data VALUES ($key,$value)",
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        $value: value,
 | 
				
			||||||
 | 
					        $key: `${id}[${key}]`,
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      (err) => {
 | 
				
			||||||
 | 
					        if (err == null) {
 | 
				
			||||||
 | 
					          resolve(true);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					          reject(err);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function getGuildData(id, key, fallback = null) {
 | 
				
			||||||
 | 
					  return new Promise((resolve, reject) => {
 | 
				
			||||||
 | 
					    hf.database.get(
 | 
				
			||||||
 | 
					      "SELECT value FROM guild_data WHERE key = $key",
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        $key: `${id}[${key}]`,
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      (err, row) => {
 | 
				
			||||||
 | 
					        if (err == null) {
 | 
				
			||||||
 | 
					          resolve(row?.value || fallback);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					          reject(err);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function deleteGuildData(id, key) {
 | 
				
			||||||
 | 
					  return new Promise((resolve, reject) => {
 | 
				
			||||||
 | 
					    hf.database.run(
 | 
				
			||||||
 | 
					      "DELETE FROM guild_data WHERE key = $key",
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        $key: `${id}[${key}]`,
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      (err) => {
 | 
				
			||||||
 | 
					        if (err == null) {
 | 
				
			||||||
 | 
					          resolve(true);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					          reject(err);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = {
 | 
				
			||||||
 | 
					  setGuildData,
 | 
				
			||||||
 | 
					  getGuildData,
 | 
				
			||||||
 | 
					  deleteGuildData,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue