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"
dependencies = [
"poise",
"rand",
"tokio",
]

View file

@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
poise = "0.5.2"
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 rand::seq::IteratorRandom;
use std::{
fs::File,
io::{BufRead, BufReader}
};
struct Data {}
@ -10,8 +15,12 @@ type Context<'a> = poise::Context<'a, Data, Error>;
async fn slur(
ctx: Context<'_>,
) -> Result<(), Error> {
let response = "If you don't like my content or the way I act, don't watch me.";
ctx.say(response).await?;
let file = File::open("quotes.txt").unwrap_or_else(|_e| panic!("Quote file missing."));
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(())
}
@ -19,7 +28,7 @@ async fn slur(
async fn main() {
let framework = poise::Framework::builder()
.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()
})
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))