· 4 years ago · Sep 04, 2021, 09:42 PM
1const { Client } = require('pg')
2const { dbclient } = require('./dbclient')
3
4dbclient.connect()
5console.clear()
6
7/**
8 * when called a api key is returned
9 * @returns {number} api_key `6rwdOG9cXoJD2IdIUn4DI6Lfx`
10 * @example ```console.log(genarateKey())```
11 */
12function genarateKey() {
13 var key = '';
14 const keyParts = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
15 for (var i = 25; i > 0; i--) {
16 key += keyParts[Math.floor(Math.random() * keyParts.length)];
17 }
18 return key;
19}
20
21/**
22 * takes in a discord ID, api key, and a key tier. then adds to `users` table
23 * @param {number} discord_id
24 * @param {number} key
25 * @param {number} key_tier
26 * @returns {string} api key
27 * @example ```let key = makeUser(message.author.id, genarateKey(), 1)```
28 */
29function makeUser(discord_id, key, key_tier) {
30 const key = client.query(`INSERT INTO users(user_id, key, tier) VALUES (${discord_id}, '${key}', ${key_tier}) RETURNING key;`)
31 return key
32}
33
34function getInfo(id) {
35 const query = client.query(`select * from users where user_id = '${id}'`)
36 return query
37}
38
39getInfo('538173471004426264').then(console.log)
40
41module.exports = {makeUser, genarateKey, getInfo}