Add Database functionality #4
|
@ -0,0 +1,18 @@
|
|||
# Set the default behavior, in case people don't have core.autocrlf set.
|
||||
<<<<<<< HEAD
|
||||
* 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
|
||||
=======
|
||||
* text eol=crlf
|
||||
>>>>>>> 543689e4ff503897e2e276ee4b29f8ee791c123a
|
|
@ -2,3 +2,4 @@
|
|||
.env
|
||||
/logs
|
||||
/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')
|
12
index.ts
12
index.ts
|
@ -3,13 +3,12 @@ dotenv.config();
|
|||
|
||||
import Discord from 'discord.js';
|
||||
import _ from 'lodash';
|
||||
|
||||
import { openDb, testDb } from './components/db';
|
||||
import logger from './logger';
|
||||
|
||||
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.';
|
||||
const BOT_TOKEN: string = process.env.BOT_TOKEN || '.';
|
||||
const BOT_PREFIX = '.';
|
||||
|
||||
const client = new Discord.Client();
|
||||
|
||||
const parseCommand = (message: Discord.Message): { command: string | null; args: string[] } => {
|
||||
|
@ -44,6 +43,12 @@ const handleCommand = async (message: Discord.Message, command: string, args: st
|
|||
switch (command) {
|
||||
case 'ping':
|
||||
await message.channel.send('pong');
|
||||
break;
|
||||
|
||||
//dev testing
|
||||
if(process.env.NODE_ENV == "dev"){
|
||||
testDb(message, command, args);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -77,10 +82,7 @@ const startBot = async () => {
|
|||
const notif = (await client.channels.fetch(NOTIF_CHANNEL_ID)) as Discord.TextChannel;
|
||||
notif.send('Codey is up!');
|
||||
});
|
||||
|
||||
client.on('message', handleMessage);
|
||||
|
||||
client.login(BOT_TOKEN);
|
||||
};
|
||||
|
||||
startBot();
|
||||
|
|
|
@ -18,11 +18,14 @@
|
|||
"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",
|
||||
|
|
Loading…
Reference in New Issue