Skip to content
Snippets Groups Projects
Commit 9950845a authored by Stephen D's avatar Stephen D
Browse files

Add DIR command to list user programs

parent 10f035d1
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ use serenity::model::channel::{Message, ReactionType};
use serenity::model::id::UserId;
use serenity::model::prelude::Ready;
use serenity::prelude::*;
use std::borrow::Cow;
use std::borrow::{Borrow, Cow};
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
......@@ -86,6 +86,12 @@ impl Handler {
"LIST" => {
self.interpreter_list(msg, ctx).await;
}
"DIR" => {
if self.list_saved_programs(msg, ctx).await.is_none() {
msg.channel_id
.send_message(&ctx, "Could not get list of programs from database.");
}
}
_ => {
if let Some(name) = line.strip_prefix("SAVE ") {
self.interpreter_save(name, msg, ctx).await;
......@@ -139,6 +145,24 @@ impl Handler {
.insert(msg.author.id, Program::new());
}
async fn list_saved_programs(&self, msg: &Message, ctx: &Context) -> Option<()> {
let program_names =
Program::list_programs_by_user(self.conn.lock().ok()?.borrow(), msg.author.id)?;
msg.channel_id
.say(
&ctx,
format!(
"You have {} programs saved:\r\n```\r\n{}\r\n```",
program_names.len(),
program_names.join("\r\n")
),
)
.await
.ok()?;
Some(())
}
async fn interpreter_list(&self, msg: &Message, ctx: &Context) {
msg.channel_id
.say(
......
......@@ -18,6 +18,14 @@ impl Program {
}
}
pub fn list_programs_by_user(conn: &PgConnection, user_id: UserId) -> Option<Vec<String>> {
user_programs
.filter(columns::discord_user_id.eq(BigDecimal::from_u64(*user_id.as_u64()).unwrap()))
.select(columns::name)
.get_results(conn)
.ok()
}
pub fn stringify(&self) -> String {
let mut code: Vec<(u32, String)> =
self.code.iter().map(|(a, b)| (*a, b.to_owned())).collect();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment