Add team_up command framework

This commit is contained in:
Em (Ethan) Ruszanowski 2023-01-10 19:34:35 -05:00
parent cd39dfd0ac
commit 89eb19cd9a
No known key found for this signature in database
GPG key ID: C3E7A3C0B1491DFE

View file

@ -2,7 +2,7 @@ use poise::serenity_prelude as serenity;
use rand::seq::IteratorRandom; use rand::seq::IteratorRandom;
use std::{ use std::{
fs::File, fs::File,
io::{BufRead, BufReader} io::{BufRead, BufReader},
}; };
struct Data {} struct Data {}
@ -10,25 +10,38 @@ struct Data {}
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>; type Context<'a> = poise::Context<'a, Data, Error>;
// Basically a ping command. Should be moved to a mod. /// Basically a ping command.
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
async fn slur( async fn slur(
ctx: Context<'_>, ctx: Context<'_>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let file = File::open("quotes.txt").unwrap_or_else(|_e| panic!("Quote file missing.")); let file = File::open("quotes.txt").unwrap_or_else(|_e| panic!("Quote file missing.")); // Open quotes file
let file = BufReader::new(file); let file = BufReader::new(file); // Read quotes file
let quotes = file.lines().map(|res| res.expect("Failed to read line.")); 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."); let quote = quotes.choose(&mut rand::thread_rng()).expect("No lines in file."); // Pick random quote
ctx.say(quote).await?; ctx.say(quote).await?;
Ok(()) Ok(())
} }
#[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());
ctx.say(res).await?;
Ok(())
}
#[tokio::main] #[tokio::main]
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(), team_up()], // 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"))