Added pull command
This commit is contained in:
parent
5932e5273a
commit
75261a8ee1
1 changed files with 30 additions and 27 deletions
57
src/main.rs
57
src/main.rs
|
@ -14,7 +14,7 @@ use rusqlite::{Connection, NO_PARAMS};
|
||||||
group!({
|
group!({
|
||||||
name: "general",
|
name: "general",
|
||||||
options: {},
|
options: {},
|
||||||
commands: [hello, add],
|
commands: [hello, add, pull],
|
||||||
});
|
});
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
@ -37,23 +37,6 @@ struct Base {
|
||||||
struct Handler;
|
struct Handler;
|
||||||
|
|
||||||
impl EventHandler for 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
|
// 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
|
// 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,
|
// 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() {
|
fn main() {
|
||||||
// get token from args
|
// get token from args
|
||||||
let args: Vec<_> = env::args().collect();
|
let args: Vec<_> = env::args().collect();
|
||||||
|
@ -76,10 +66,7 @@ fn main() {
|
||||||
if args[1] == "create" {
|
if args[1] == "create" {
|
||||||
println!("Creating table...\n\n");
|
println!("Creating table...\n\n");
|
||||||
println!("Connecting to database");
|
println!("Connecting to database");
|
||||||
let base_path = &args[2];
|
let base = make_base_from_args(2);
|
||||||
let base = Base {
|
|
||||||
connection: Connection::open(&base_path).expect("Failed to open in memory..."),
|
|
||||||
};
|
|
||||||
|
|
||||||
base.connection.execute(
|
base.connection.execute(
|
||||||
"CREATE TABLE question (
|
"CREATE TABLE question (
|
||||||
|
@ -130,11 +117,7 @@ fn add(ctx: &mut Context, msg: &Message) -> CommandResult {
|
||||||
question: addition,
|
question: addition,
|
||||||
};
|
};
|
||||||
|
|
||||||
let args: Vec<_> = env::args().collect();
|
let base = make_base_from_args(1);
|
||||||
let base_path = &args[1];
|
|
||||||
let base = Base {
|
|
||||||
connection: Connection::open(&base_path).expect("Failed to open database..."),
|
|
||||||
};
|
|
||||||
println!("\tOpened connection");
|
println!("\tOpened connection");
|
||||||
|
|
||||||
print!("\tExecuting insert...");
|
print!("\tExecuting insert...");
|
||||||
|
@ -149,3 +132,23 @@ fn add(ctx: &mut Context, msg: &Message) -> CommandResult {
|
||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue