This commit is contained in:
marko-polo-cheno 2021-05-30 15:41:18 -04:00
commit b26716f550
6 changed files with 744 additions and 19 deletions

14
.gitattributes vendored Normal file
View File

@ -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

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
.env
/logs
/dist
/db

68
components/db.ts Normal file
View File

@ -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')

View File

@ -3,7 +3,7 @@ dotenv.config();
import Discord from 'discord.js';
import _ from 'lodash';
import { openDB, testDb } from './components/db';
import logger from './logger';
import { pingCmd } from './commands/ping';
@ -48,6 +48,11 @@ const handleCommand = async (message: Discord.Message, command: string, args: st
case 'ping':
pingCmd(message, command, args);
}
//dev testing
if (process.env.NODE_ENV == 'dev') {
testDb(message, command, args);
}
};
const handleAnonCommand = async (message: Discord.Message, command: string, args: string[]) => {

View File

@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"dev": "concurrently \"yarn run-dev\" \"yarn watch\"",
"run-dev": "nodemon index.ts",
"run-dev": "cross-env NODE_ENV=dev nodemon index.ts",
"watch": "tsc --watch",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
@ -18,17 +18,21 @@
"dotenv": "^8.2.0",
"lodash": "^4.17.21",
"moment": "^2.29.1",
"sqlite": "^4.0.22",
"sqlite3": "^5.0.2",
"typescript": "^4.2.4",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5"
},
"devDependencies": {
"@types/sqlite3": "^3.1.7",
"@tsconfig/node14": "^1.0.0",
"@types/lodash": "^4.14.168",
"@types/node": "^15.0.1",
"@types/winston": "^2.4.4",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"cross-env": "^7.0.3",
"eslint": "^7.25.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",

667
yarn.lock

File diff suppressed because it is too large Load Diff