Skip to content

Commit 0cf271b

Browse files
Alex Zhanggzcharleszhang
Alex Zhang
authored andcommitted
add database function with openDB
1 parent ed599bd commit 0cf271b

File tree

6 files changed

+727
-91
lines changed

6 files changed

+727
-91
lines changed

.gitattributes

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Set the default behavior, in case people don't have core.autocrlf set.
2+
* text=auto
3+
4+
# Explicitly declare text files you want to always be normalized and converted
5+
# to native line endings on checkout.
6+
*.c text
7+
*.h text
8+
9+
# Declare files that will always have CRLF line endings on checkout.
10+
*.sln text eol=crlf
11+
12+
# Denote all files that are truly binary and should not be modified.
13+
*.png binary
14+
*.jpg binary

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.env
33
/logs
44
/dist
5+
/db

components/db.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sqlite3 = require('sqlite3')
2+
import { open, Database } from 'sqlite'
3+
import Discord from 'discord.js'
4+
5+
let db : Database | null = null;
6+
7+
export async function openDB () {
8+
if(db == null){
9+
db = await open({
10+
filename: './db/bot.db',
11+
driver: sqlite3.Database
12+
})
13+
await db.run('CREATE TABLE IF NOT EXISTS saved_data (msg_id INTEGER PRIMARY KEY,data TEXT NOT NULL);')
14+
}
15+
return db;
16+
}
17+
18+
export async function testDb(message: Discord.Message, command: string, args: string[]){
19+
switch(command){
20+
case 'save':
21+
if (args.length < 1) {
22+
await message.channel.send('no args');
23+
return;
24+
}
25+
await openDB().then((db) => {
26+
db.run('INSERT INTO saved_data (msg_id,data)' + 'VALUES(?,?)', [message.id, args[0]]);
27+
});
28+
await message.channel.send('Saved ' + args[0] + ' with id ' + message.id);
29+
break;
30+
case 'dump':
31+
await openDB().then(async (db) => {
32+
let flag: boolean = true;
33+
let outEmbed = new Discord.MessageEmbed()
34+
.setColor('#0099ff')
35+
.setTitle('Database Dump')
36+
.setURL('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
37+
const res = await db.all('SELECT * FROM saved_data');
38+
for(const rows of res){
39+
console.log(rows['msg_id'], rows['data']);
40+
outEmbed = outEmbed.addField(rows['msg_id'], rows['data'], true);
41+
console.log(outEmbed);
42+
}
43+
console.log(outEmbed);
44+
if (flag) {
45+
if (outEmbed.fields.length == 0) {
46+
await message.channel.send('empty');
47+
} else {
48+
await message.channel.send(outEmbed);
49+
}
50+
} else {
51+
await message.channel.send('error');
52+
}
53+
});
54+
break;
55+
case 'clear':
56+
openDB()
57+
.then((db) => {
58+
return db.run('DELETE FROM saved_data');
59+
})
60+
.then(async () => {
61+
await message.channel.send('cleared');
62+
})
63+
.catch();
64+
break;
65+
}
66+
}
67+
68+
console.log('connected to db')

index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ dotenv.config();
33

44
import Discord from 'discord.js';
55
import _ from 'lodash';
6-
6+
import { openDB, testDb } from './components/db';
77
import logger from './logger';
88

99
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.';
@@ -44,6 +44,12 @@ const handleCommand = async (message: Discord.Message, command: string, args: st
4444
switch (command) {
4545
case 'ping':
4646
await message.channel.send('pong');
47+
break;
48+
}
49+
50+
//dev testing
51+
if (process.env.NODE_ENV == 'dev') {
52+
testDb(message, command, args);
4753
}
4854
};
4955

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
"dotenv": "^8.2.0",
1919
"lodash": "^4.17.21",
2020
"moment": "^2.29.1",
21+
"sqlite": "^4.0.22",
22+
"sqlite3": "^5.0.2",
2123
"typescript": "^4.2.4",
2224
"winston": "^3.3.3",
2325
"winston-daily-rotate-file": "^4.5.5"
2426
},
2527
"devDependencies": {
28+
"@types/sqlite3": "^3.1.7",
2629
"@tsconfig/node14": "^1.0.0",
2730
"@types/lodash": "^4.14.168",
2831
"@types/node": "^15.0.1",

0 commit comments

Comments
 (0)