Update quotes to read from file

This commit is contained in:
Em (Ethan) Ruszanowski 2023-01-10 13:11:06 -05:00
parent 57ec109290
commit c20d585a63
No known key found for this signature in database
GPG key ID: C3E7A3C0B1491DFE
3 changed files with 14 additions and 3 deletions

1
Cargo.lock generated
View file

@ -277,6 +277,7 @@ name = "echbot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"poise", "poise",
"rand",
"tokio", "tokio",
] ]

View file

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
poise = "0.5.2" poise = "0.5.2"
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] }
rand = "0.8.5"

View file

@ -1,4 +1,9 @@
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use rand::seq::IteratorRandom;
use std::{
fs::File,
io::{BufRead, BufReader}
};
struct Data {} struct Data {}
@ -10,8 +15,12 @@ type Context<'a> = poise::Context<'a, Data, Error>;
async fn slur( async fn slur(
ctx: Context<'_>, ctx: Context<'_>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let response = "If you don't like my content or the way I act, don't watch me."; let file = File::open("quotes.txt").unwrap_or_else(|_e| panic!("Quote file missing."));
ctx.say(response).await?; let file = BufReader::new(file);
let quotes = file.lines().map(|res| res.expect("Failed to read line."));
let quote = quotes.choose(&mut rand::thread_rng()).expect("No lines in file.");
ctx.say(quote).await?;
Ok(()) Ok(())
} }
@ -19,7 +28,7 @@ async fn slur(
async fn main() { async fn main() {
let framework = poise::Framework::builder() let framework = poise::Framework::builder()
.options(poise::FrameworkOptions { .options(poise::FrameworkOptions {
commands: vec![slur()], // Intellij doesn't like this, but it's fine. commands: vec![slur()], // IntelliJ doesn't like this, but it's fine.
..Default::default() ..Default::default()
}) })
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN")) .token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))