Refactor for better command structure

This commit is contained in:
Em (Ethan) Ruszanowski 2023-01-11 16:31:15 -05:00
parent a815ca04bb
commit a3a673c7e6
No known key found for this signature in database
GPG key ID: C3E7A3C0B1491DFE
5 changed files with 75 additions and 38 deletions

View file

@ -1,2 +1,18 @@
# echbot # echbot
A trash bot for Smite fun. A trash bot for Smite fun.
## Environment
You must set the following for the bot to run.
```yml
environment:
- DISCORD_TOKEN=your_token_here
- GUILD_ID=your_guild_id_here
```
## Quotes
A `quotes.txt` file must be present in the root directory for the ping command to function. The format is one quote per
line.

3
src/commands/mod.rs Normal file
View file

@ -0,0 +1,3 @@
// Group all commands for registration
pub(crate) mod slur;
pub(crate) mod team_up;

24
src/commands/slur.rs Normal file
View file

@ -0,0 +1,24 @@
use crate::{Context, Error};
use rand::seq::IteratorRandom;
use std::{
fs::File,
io::{BufRead, BufReader},
};
/// Basically a ping command
#[poise::command(slash_command, prefix_command)]
pub(crate) async fn slur(
ctx: Context<'_>,
) -> Result<(), Error> {
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
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."); // Pick a random quote
ctx.say(quote).await?;
Ok(())
}

17
src/commands/team_up.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::{Context, Error};
use crate::serenity;
/// Split up users for custom joust matches
#[poise::command(slash_command, prefix_command)]
pub(crate) 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(())
}

View file

@ -1,56 +1,33 @@
mod commands;
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 {}
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.
#[poise::command(slash_command, prefix_command)]
async fn slur(
ctx: Context<'_>,
) -> Result<(), Error> {
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
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."); // Pick a random quote
ctx.say(quote).await?;
Ok(())
}
/// Split up users for custom joust matches.
#[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(), team_up()], // IntelliJ doesn't like this, but it's fine. commands: vec![
commands::slur::slur(),
commands::team_up::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")
.intents(serenity::GatewayIntents::non_privileged()) .expect("Missing DISCORD_TOKEN"))
.intents(serenity::GatewayIntents::non_privileged()) // Set intents for Discord dev portal
.setup(|ctx, _ready, framework| { .setup(|ctx, _ready, framework| {
Box::pin(async move { Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?; poise::builtins::register_in_guild(
ctx,
&framework.options().commands,
serenity::GuildId(std::env::var("GUILD_ID")
.expect("Missing GUILD_ID") // Get GID from env and parse
.parse::<u64>().unwrap())).await?; // Update slash commands in GID
Ok(Data {}) Ok(Data {})
}) })
}); });