From 65751723765451c510981dc36c6b3a1a048615e5 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 23 May 2021 21:18:09 -0400 Subject: [PATCH] merge fixes and add gitattributes --- .gitattributes | 2 ++ components/db.ts | 52 +++++++++++++++++++++++++++++++ index.ts | 53 +++----------------------------- logger.ts | 80 ++++++++++++++++++++++++------------------------ package.json | 2 +- tsconfig.json | 18 +++++------ 6 files changed, 109 insertions(+), 98 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..09bc62c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto \ No newline at end of file diff --git a/components/db.ts b/components/db.ts index d235826..e7633f4 100644 --- a/components/db.ts +++ b/components/db.ts @@ -1,5 +1,6 @@ import sqlite3 = require('sqlite3') import { open, Database } from 'sqlite' +import Discord from 'discord.js' let db : Database | null = null; @@ -13,4 +14,55 @@ export async function openDb () { } 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') diff --git a/index.ts b/index.ts index 8d14cd5..2595822 100644 --- a/index.ts +++ b/index.ts @@ -3,7 +3,7 @@ dotenv.config(); import Discord from 'discord.js'; import _ from 'lodash'; -import { openDb } from './components/db'; +import { openDb, testDb } from './components/db'; import logger from './logger'; const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.'; @@ -44,54 +44,11 @@ const handleCommand = async (message: Discord.Message, command: string, args: st case 'ping': await message.channel.send('pong'); break; - - //dev testing commands - 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; + //dev testing + if(process.env.NODE_ENV == "dev"){ + testDb(message, command, args); + } } }; diff --git a/logger.ts b/logger.ts index 3ea5c09..4fe8398 100644 --- a/logger.ts +++ b/logger.ts @@ -1,40 +1,40 @@ -const winston = require('winston'); -require('winston-daily-rotate-file'); - -const dailyRotateTransport = new winston.transports.DailyRotateFile({ - filename: '%DATE%.log', - dirname: 'logs', - zippedArchive: true -}); - -const dailyRotateErrorTransport = new winston.transports.DailyRotateFile({ - filename: 'error-%DATE%.log', - dirname: 'logs', - zippedArchive: true, - level: 'error' -}); - -const consoleTransport = new winston.transports.Console({ - format: winston.format.prettyPrint() -}); - -const logger = winston.createLogger({ - format: winston.format.combine( - winston.format.timestamp(), - winston.format.printf( - ({ level, message, timestamp }: { level: string; message: string; timestamp: string }) => - `[${timestamp}] ${level}: ${JSON.stringify(message)}` - ) - ), - transports: [dailyRotateTransport, dailyRotateErrorTransport] -}); - -if (process.env.NODE_ENV === 'dev') { - logger.add( - new winston.transports.Console({ - format: winston.format.prettyPrint() - }) - ); -} - -export default logger; +const winston = require('winston'); +require('winston-daily-rotate-file'); + +const dailyRotateTransport = new winston.transports.DailyRotateFile({ + filename: '%DATE%.log', + dirname: 'logs', + zippedArchive: true +}); + +const dailyRotateErrorTransport = new winston.transports.DailyRotateFile({ + filename: 'error-%DATE%.log', + dirname: 'logs', + zippedArchive: true, + level: 'error' +}); + +const consoleTransport = new winston.transports.Console({ + format: winston.format.prettyPrint() +}); + +const logger = winston.createLogger({ + format: winston.format.combine( + winston.format.timestamp(), + winston.format.printf( + ({ level, message, timestamp }: { level: string; message: string; timestamp: string }) => + `[${timestamp}] ${level}: ${JSON.stringify(message)}` + ) + ), + transports: [dailyRotateTransport, dailyRotateErrorTransport] +}); + +if (process.env.NODE_ENV === 'dev') { + logger.add( + new winston.transports.Console({ + format: winston.format.prettyPrint() + }) + ); +} + +export default logger; diff --git a/package.json b/package.json index 760d10b..fb650cf 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "author": "", "license": "ISC", "dependencies": { - "@types/sqlite3": "^3.1.7", "concurrently": "^6.1.0", "discord.js": "^12.5.3", "dotenv": "^8.2.0", @@ -26,6 +25,7 @@ "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", diff --git a/tsconfig.json b/tsconfig.json index c7a4974..c3a239e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,9 @@ -{ - "extends": "@tsconfig/node14/tsconfig.json", - "compilerOptions": { - "outDir": "dist", - "preserveConstEnums": true, - "esModuleInterop": true - }, - "include": ["**/*.ts"] -} +{ + "extends": "@tsconfig/node14/tsconfig.json", + "compilerOptions": { + "outDir": "dist", + "preserveConstEnums": true, + "esModuleInterop": true + }, + "include": ["**/*.ts"] +}