songy

telegram bot as a songbook
git clone git://git.relim.de/songy.git
Log | Files | Refs | README | LICENSE

commit b18b3bb89cdc65fcaec4b70cc57b47d2f5aa6264
parent 3c5873885a412437f9afd6c43e21adfe800b11c3
Author: devrobinkroeker <kroekerrobin@gmail.com>
Date:   Sat,  3 Jun 2023 17:37:46 +0200

List all available cmds in /start message

Diffstat:
Msrc/langs.rs | 65++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/main.rs | 20++------------------
2 files changed, 50 insertions(+), 35 deletions(-)

diff --git a/src/langs.rs b/src/langs.rs @@ -1,4 +1,4 @@ -use std::str; +use std::{str, fs}; pub struct Strings { lang: String, @@ -7,35 +7,44 @@ pub struct Strings { } impl Strings { - pub fn new(lang: String) -> Self { + pub fn new(lang: String, songs_path: String) -> Self { let start_msg: String; let song_not_found: String; let l: &str = lang.as_str(); match l { "de" => { - start_msg = concat!( - "Hallo. Dies ist ein digitales Liederbuch. :)\n", - "/list - Listet alle Lieder auf.\n", - "Ansonsten tippe einfach den Titel oder Teile des Titels", - " des Liedes ein und du bekommst dein Lied zugeschickt." + start_msg = format!( + "Hallo. Dies ist ein digitales Liederbuch. :)\n\ + Befehle:\n\ + /list - Listet alle Lieder auf\n\ + {}\ + Ansonsten tippe einfach den Titel oder Teile des Titels \ + des Liedes ein und du bekommst dein Lied zugeschickt.", + get_commands(songs_path).as_str() ).to_string(); song_not_found = "Kein Lied mit diesem Titel gefunden.".to_string(); }, "md" => { - start_msg = concat!( - "Salut! Această e o carte de cântari digitală. :)\n", - "/list - Listează toate cântările.\n", - "Deasemenea puteți introduce titlul sau cuvinte din titlul", - " cântării iar bot-ul va găsi piesa corespondentă." + start_msg = format!( + "Salut! Această e o carte de cântari digitală. :)\n\ + Comenzi:\n\ + /list - Listează toate cântările\n\ + {}\ + Deasemenea puteți introduce titlul sau cuvinte din titlul \ + cântării iar bot-ul va găsi piesa corespondentă.", + get_commands(songs_path).as_str() ).to_string(); song_not_found = "Niciun cântec găsit cu acest nume".to_string(); }, _ => { - start_msg = concat!( - "Hello. This is a digital song book. :)\n", - "/list - Lists all songs.\n", - "Otherwise simply type the title or parts of the title", - " of the song and you will receive the song." + start_msg = format!( + "Hello. This is a digital song book. :)\n\ + Commands:\n\ + /list - Lists all songs\n\ + {}\ + Otherwise simply type the title or parts of the title \ + of the song and you will receive the song.", + get_commands(songs_path).as_str() ).to_string(); song_not_found = "Didn't find any song with this title.".to_string(); } @@ -74,4 +83,26 @@ impl Strings { } } +fn get_commands(songs_path: String) -> String { + let mut commands: String = String::new(); + for name in get_folder_names(&songs_path) { + commands.push_str(&("/".to_owned() + name.as_str() + "\n")); + } + return commands; +} +pub fn get_folder_names(songs_path: &String) -> Vec<String> { + let songs_dir = fs::read_dir(songs_path).expect("Error: read_dir songs_path"); + let mut folder_names: Vec<String> = vec![]; + let mut is_dir: bool; + let mut dir_entry; + for f in songs_dir { + dir_entry = f.expect("Error: f"); + is_dir = dir_entry.file_type().expect("Error: is_dir").is_dir(); + if is_dir { + let name: String = dir_entry.file_name().to_str().expect("Error: filename").to_string(); + folder_names.push(name) + } + } + return folder_names; +} diff --git a/src/main.rs b/src/main.rs @@ -39,8 +39,8 @@ struct Args { fn main() { let args = Args::parse(); - let strings = langs::Strings::new(args.lang); let songs_path: String = add_ending_slash(args.songs_path); + let strings = langs::Strings::new(args.lang, songs_path.clone()); let api = Api::new(&args.token.as_str()); let mut updates_params = GetUpdatesParams::builder() .allowed_updates(vec![AllowedUpdate::Message]) @@ -104,7 +104,7 @@ fn handle_text_message(api: &Api, msg: &Message, strings: &langs::Strings, songs }, _ => { if text.starts_with("/") { - for name in get_folder_names(songs_path) { + for name in langs::get_folder_names(songs_path) { if text == "/".to_owned() + name.as_str() { let songs = get_songs(songs_path, Some(&name)); params.text = form_message(&songs); @@ -261,22 +261,6 @@ fn form_message(songs: &Vec<fs::DirEntry>) -> String { return message; } -fn get_folder_names(songs_path: &String) -> Vec<String> { - let songs_dir = fs::read_dir(songs_path).expect("Error: read_dir songs_path"); - let mut folder_names: Vec<String> = vec![]; - let mut is_dir: bool; - let mut dir_entry; - for f in songs_dir { - dir_entry = f.expect("Error: f"); - is_dir = dir_entry.file_type().expect("Error: is_dir").is_dir(); - if is_dir { - let name: String = dir_entry.file_name().to_str().expect("Error: filename").to_string(); - folder_names.push(name) - } - } - return folder_names; -} - fn find_songs(search_string: &String, songs_path: &String, strings: &langs::Strings) -> Result<Vec<fs::DirEntry>, ErrNotFound> { let mut result: Vec<fs::DirEntry> = vec![]; let mut filename: String;