From 75261a8ee17d070635919f24e95555e2c7d0256f Mon Sep 17 00:00:00 2001 From: Skye Bleed Date: Sun, 8 Sep 2019 07:12:59 -0500 Subject: [PATCH] Added pull command --- src/main.rs | 57 ++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6e183fb..6465500 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use rusqlite::{Connection, NO_PARAMS}; group!({ name: "general", options: {}, - commands: [hello, add], + commands: [hello, add, pull], }); use std::env; @@ -37,23 +37,6 @@ struct Base { struct Handler; impl EventHandler for Handler { - // Set a handler for the `message` event - so that whenever a new message - // is received - the closure (or function) passed will be called. - // - // Event handlers are dispatched through a threadpool, and so multiple - // events can be dispatched simultaneously. - fn message(&self, ctx: Context, msg: Message) { - if msg.content == "!ping" { - // Sending a message can fail, due to a network error, an - // authentication error, or lack of permissions to post in the - // channel, so log to stdout when some error happens, with a - // description of it. - if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!") { - println!("Error sending message: {:?}", why); - } - } - } - // Set a handler to be called on the `ready` event. This is called when a // shard is booted, and a READY payload is sent by Discord. This payload // contains data like the current user's guild Ids, current user data, @@ -65,6 +48,13 @@ impl EventHandler for Handler { } } +fn make_base_from_args(num: usize) -> Base { + let args: Vec<_> = env::args().collect(); + Base { + connection: Connection::open(&args[num]).expect("Failed to open database..."), + } +} + fn main() { // get token from args let args: Vec<_> = env::args().collect(); @@ -76,10 +66,7 @@ fn main() { if args[1] == "create" { println!("Creating table...\n\n"); println!("Connecting to database"); - let base_path = &args[2]; - let base = Base { - connection: Connection::open(&base_path).expect("Failed to open in memory..."), - }; + let base = make_base_from_args(2); base.connection.execute( "CREATE TABLE question ( @@ -130,11 +117,7 @@ fn add(ctx: &mut Context, msg: &Message) -> CommandResult { question: addition, }; - let args: Vec<_> = env::args().collect(); - let base_path = &args[1]; - let base = Base { - connection: Connection::open(&base_path).expect("Failed to open database..."), - }; + let base = make_base_from_args(1); println!("\tOpened connection"); print!("\tExecuting insert..."); @@ -149,3 +132,23 @@ fn add(ctx: &mut Context, msg: &Message) -> CommandResult { Ok(()) } + +#[command] +fn pull(ctx: &mut Context, msg: &Message) -> CommandResult { + println!("Executing command PULL as \"{}\"", msg.content); + + let base = make_base_from_args(1); + println!("\tOpened connection"); + let mut stmt = base.connection + .prepare("SELECT id, question FROM question").expect("Failed to select from database"); + let qs: Vec<_> = stmt + .query_map(NO_PARAMS, |row| Ok(Question { + id: row.get(0).expect("Failed to get row..."), + question: row.get(1).expect("Failed to get row..."), + })).expect("Failed to create a map").collect(); + println!("\tSuccessfully created map"); + + msg.reply(ctx, format!("{:?}", &qs[qs.len() - 1]))?; + + Ok(()) +}