· 4 years ago · Sep 04, 2021, 09:04 PM
1const { Client } = require("pg");
2const config = require("./config.json");
3console.clear();
4
5const client = new Client({
6 user: config.user,
7 host: config.host,
8 database: config.database,
9 password: config.password,
10 port: 5432,
11});
12
13/**
14 * when called a api key is returned
15 * @returns {number} api_key `6rwdOG9cXoJD2IdIUn4DI6Lfx`
16 * @example ```console.log(genarateKey())```
17 */
18function genarateKey() {
19 var key = "";
20 const keyParts = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
21 for (var i = 25; i > 0; i--) {
22 key += keyParts[Math.floor(Math.random() * keyParts.length)];
23 }
24 return key;
25}
26
27// /**
28// * takes in a discord ID, api key, and a key tier. then adds to `users` table
29// * @param {number} discord_id
30// * @param {number} key
31// * @param {number} key_tier
32// * @returns {string} api key
33// * @example ```let key = makeUser(message.author.id, genarateKey(), 1)```
34// */
35
36// function makeUser(discord_id, key, key_tier) {
37// const client = new Client({
38// user: config.user,
39// host: config.host,
40// database: config.database,
41// password: config.password,
42// port: 5432,
43// });
44// client.connect();
45// client.query(`INSERT INTO users(user_id, key, tier) VALUES (${discord_id}, '${key}', ${key_tier}) RETURNING key;`, (err, res) => {
46// const key = res.rows[0].key;
47// client.end();
48// });
49
50// return key;
51// }
52
53function getInfo(id) {
54 client.connect();
55 return new Promise((resolve, reject) => {
56 client.query(client.query({text: 'select * from users where user_id = $1', values: [id]}), (err, res) => {
57 const resolt = err;
58 resolve(resolt);
59 }).then(client.end());
60
61 });
62}
63
64getInfo('538173471004426264').then(console.log)
65
66module.exports = { makeUser, genarateKey, getInfo };
67