Merge branch 'Database' into 'master'
add database function with openDB See merge request csc/discord-bot!6
This commit is contained in:
commit
92407cf007
|
@ -0,0 +1,14 @@
|
||||||
|
# Set the default behavior, in case people don't have core.autocrlf set.
|
||||||
|
* text=auto
|
||||||
|
|
||||||
|
# Explicitly declare text files you want to always be normalized and converted
|
||||||
|
# to native line endings on checkout.
|
||||||
|
*.c text
|
||||||
|
*.h text
|
||||||
|
|
||||||
|
# Declare files that will always have CRLF line endings on checkout.
|
||||||
|
*.sln text eol=crlf
|
||||||
|
|
||||||
|
# Denote all files that are truly binary and should not be modified.
|
||||||
|
*.png binary
|
||||||
|
*.jpg binary
|
|
@ -2,3 +2,4 @@
|
||||||
.env
|
.env
|
||||||
/logs
|
/logs
|
||||||
/dist
|
/dist
|
||||||
|
/db
|
|
@ -0,0 +1,68 @@
|
||||||
|
import sqlite3 = require('sqlite3')
|
||||||
|
import { open, Database } from 'sqlite'
|
||||||
|
import Discord from 'discord.js'
|
||||||
|
|
||||||
|
let db : Database | null = null;
|
||||||
|
|
||||||
|
export async function openDB () {
|
||||||
|
if(db == null){
|
||||||
|
db = await open({
|
||||||
|
filename: './db/bot.db',
|
||||||
|
driver: sqlite3.Database
|
||||||
|
})
|
||||||
|
await db.run('CREATE TABLE IF NOT EXISTS saved_data (msg_id INTEGER PRIMARY KEY,data TEXT NOT NULL);')
|
||||||
|
}
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function testDb(message: Discord.Message, command: string, args: string[]){
|
||||||
|
switch(command){
|
||||||
|
case 'save':
|
||||||
|
if (args.length < 1) {
|
||||||
|
await message.channel.send('no args');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await openDB().then((db) => {
|
||||||
|
db.run('INSERT INTO saved_data (msg_id,data)' + 'VALUES(?,?)', [message.id, args[0]]);
|
||||||
|
});
|
||||||
|
await message.channel.send('Saved ' + args[0] + ' with id ' + message.id);
|
||||||
|
break;
|
||||||
|
case 'dump':
|
||||||
|
await openDB().then(async (db) => {
|
||||||
|
let flag: boolean = true;
|
||||||
|
let outEmbed = new Discord.MessageEmbed()
|
||||||
|
.setColor('#0099ff')
|
||||||
|
.setTitle('Database Dump')
|
||||||
|
.setURL('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
||||||
|
const res = await db.all('SELECT * FROM saved_data');
|
||||||
|
for(const rows of res){
|
||||||
|
console.log(rows['msg_id'], rows['data']);
|
||||||
|
outEmbed = outEmbed.addField(rows['msg_id'], rows['data'], true);
|
||||||
|
console.log(outEmbed);
|
||||||
|
}
|
||||||
|
console.log(outEmbed);
|
||||||
|
if (flag) {
|
||||||
|
if (outEmbed.fields.length == 0) {
|
||||||
|
await message.channel.send('empty');
|
||||||
|
} else {
|
||||||
|
await message.channel.send(outEmbed);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await message.channel.send('error');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case 'clear':
|
||||||
|
openDB()
|
||||||
|
.then((db) => {
|
||||||
|
return db.run('DELETE FROM saved_data');
|
||||||
|
})
|
||||||
|
.then(async () => {
|
||||||
|
await message.channel.send('cleared');
|
||||||
|
})
|
||||||
|
.catch();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('connected to db')
|
8
index.ts
8
index.ts
|
@ -3,7 +3,7 @@ dotenv.config();
|
||||||
|
|
||||||
import Discord from 'discord.js';
|
import Discord from 'discord.js';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import { openDB, testDb } from './components/db';
|
||||||
import logger from './logger';
|
import logger from './logger';
|
||||||
|
|
||||||
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.';
|
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.';
|
||||||
|
@ -44,6 +44,12 @@ const handleCommand = async (message: Discord.Message, command: string, args: st
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case 'ping':
|
case 'ping':
|
||||||
await message.channel.send('pong');
|
await message.channel.send('pong');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//dev testing
|
||||||
|
if (process.env.NODE_ENV == 'dev') {
|
||||||
|
testDb(message, command, args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -18,11 +18,14 @@
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"moment": "^2.29.1",
|
"moment": "^2.29.1",
|
||||||
|
"sqlite": "^4.0.22",
|
||||||
|
"sqlite3": "^5.0.2",
|
||||||
"typescript": "^4.2.4",
|
"typescript": "^4.2.4",
|
||||||
"winston": "^3.3.3",
|
"winston": "^3.3.3",
|
||||||
"winston-daily-rotate-file": "^4.5.5"
|
"winston-daily-rotate-file": "^4.5.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/sqlite3": "^3.1.7",
|
||||||
"@tsconfig/node14": "^1.0.0",
|
"@tsconfig/node14": "^1.0.0",
|
||||||
"@types/lodash": "^4.14.168",
|
"@types/lodash": "^4.14.168",
|
||||||
"@types/node": "^15.0.1",
|
"@types/node": "^15.0.1",
|
||||||
|
|
Loading…
Reference in New Issue