echbot/src/main.rs

60 lines
2.1 KiB
Rust
Raw Normal View History

2023-01-09 18:38:08 -05:00
use poise::serenity_prelude as serenity;
2023-01-10 13:11:06 -05:00
use rand::seq::IteratorRandom;
use std::{
fs::File,
2023-01-10 19:34:35 -05:00
io::{BufRead, BufReader},
2023-01-10 13:11:06 -05:00
};
2023-01-09 18:38:08 -05:00
struct Data {}
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
2023-01-10 19:34:35 -05:00
/// Basically a ping command.
2023-01-09 18:38:08 -05:00
#[poise::command(slash_command, prefix_command)]
async fn slur(
ctx: Context<'_>,
) -> Result<(), Error> {
2023-01-10 19:38:31 -05:00
let file = File::open("quotes.txt").unwrap_or_else(|_e| panic!("Quote file missing.")); // Open the quotes file
let file = BufReader::new(file); // Read the quotes file
2023-01-10 13:11:06 -05:00
let quotes = file.lines().map(|res| res.expect("Failed to read line."));
2023-01-10 19:38:31 -05:00
let quote = quotes.choose(&mut rand::thread_rng()).expect("No lines in file."); // Pick a random quote
2023-01-10 13:11:06 -05:00
ctx.say(quote).await?;
2023-01-09 18:38:08 -05:00
Ok(())
}
2023-01-10 19:38:31 -05:00
/// Split up users for custom joust matches.
2023-01-10 19:34:35 -05:00
#[poise::command(slash_command, prefix_command)]
async fn team_up(
ctx: Context<'_>,
#[description = "Your voice channel"] channel: Option<serenity::Channel>,
) -> Result<(), Error> {
let c = channel.as_ref().unwrap(); // Get channel info from object
let mut v = ctx.guild().unwrap().voice_states; // Get hashmap of users' voice states within the guild
v.retain(|_, s| s.channel_id == Some(c.id())); // Drop users not active in requested voice channel from hashmap
let res = format!("Channel {} has {} active users", c.id(), v.keys().len());
2023-01-10 19:38:31 -05:00
2023-01-10 19:34:35 -05:00
ctx.say(res).await?;
Ok(())
}
2023-01-09 18:38:08 -05:00
#[tokio::main]
async fn main() {
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
2023-01-10 19:34:35 -05:00
commands: vec![slur(), team_up()], // IntelliJ doesn't like this, but it's fine.
2023-01-09 18:38:08 -05:00
..Default::default()
})
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))
.intents(serenity::GatewayIntents::non_privileged())
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
})
});
framework.run().await.unwrap();
2023-01-09 18:41:05 -05:00
}