diff --git a/commands/ping.ts b/commands/ping.ts index a089b04..30a8261 100644 --- a/commands/ping.ts +++ b/commands/ping.ts @@ -2,6 +2,6 @@ import Discord from 'discord.js'; -export const pingCmd = async (message: Discord.Message, command: string, args: string[]) => { +export const pingCmd = async (message: Discord.Message) => { message.channel.send('pong'); }; diff --git a/commands/suggest.ts b/commands/suggest.ts index 24ca672..b840d3b 100644 --- a/commands/suggest.ts +++ b/commands/suggest.ts @@ -1,10 +1,42 @@ // Codey suggest Command import Discord from 'discord.js'; +import { openDB, testDb } from '../components/db'; -export const suggestCmd = async (message: Discord.Message, command: string, args: string[]) => { - message.channel.send('yay'); - message.channel.send('test suggestion was: ' + args.toString()); +export const suggestCmd = async (message: Discord.Message, args: string[]) => { + try { + // save suggestion into DB + const state = 'C'; // Create state = C + const db = openDB(); + var words = ''; + var word = ''; + for (word in args) { + words += word + ' '; + } - // ack suggestion was taken - // save suggestion into DB + (await db).run( + 'BEGIN TRANSACTION;' + + 'CREATE TABLE IF NOT EXISTS suggestions (' + + ' suggestion_id IDENTITY(1,1) PRIMARY KEY,' + + ' suggestion_author VARCHAR(255) NOT NULL,' + + ' created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,' + + ' suggestion VARCHAR(500) NOT NULL,' + + ' suggestion_state VARCHAR(1) NOT NULL' + + ');' + + 'INSERT INTO suggestions(suggestion_author, suggestion, suggestion_state)' + + ' VALUES(' + + message.id + + ', ' + + words + + ', ' + + state + + ');' + + 'COMMIT;' + ); + + // confirm suggestion was taken + message.channel.send('Codey has recieved your suggestion: ' + args[0] + ' ' + args[1] + ' ' + args[2] + '... '); + } catch (err) { + // Error message + message.channel.send('Sorry! There has been an error. Please try again later or let a mod know this happened.'); + } }; diff --git a/index.ts b/index.ts index f283c84..b3fa003 100644 --- a/index.ts +++ b/index.ts @@ -8,6 +8,7 @@ import logger from './logger'; import { pingCmd } from './commands/ping'; import { suggestCmd } from './commands/suggest'; +import { Database, Statement } from 'sqlite3'; const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.'; const BOT_TOKEN: string = process.env.BOT_TOKEN || '.'; @@ -46,7 +47,7 @@ const handleCommand = async (message: Discord.Message, command: string, args: st switch (command) { case 'ping': - pingCmd(message, command, args); + pingCmd(message); } //dev testing @@ -66,7 +67,7 @@ const handleAnonCommand = async (message: Discord.Message, command: string, args switch (command) { case 'suggest': - suggestCmd(message, command, args); + suggestCmd(message, args); } };