Skip to main content

streamocracy/
utils.rs

1//! Utility functions and helpers for the Streamocracy bot
2
3use serenity::all::{
4    Command, CommandInteraction, Context, CreateCommand, CreateInteractionResponse,
5    CreateInteractionResponseMessage, GuildId,
6};
7use tracing::{error, info};
8
9/// Utility struct for common Discord bot operations
10pub struct Utils;
11
12impl Utils {
13    /// Send an ephemeral (only visible to the user) response to a command interaction
14    pub async fn ephemeral_response(
15        http: &serenity::all::Http,
16        command: &CommandInteraction,
17        content: impl Into<String>,
18    ) {
19        if let Err(e) = command
20            .create_response(
21                http,
22                CreateInteractionResponse::Message(
23                    CreateInteractionResponseMessage::new()
24                        .content(content)
25                        .ephemeral(true),
26                ),
27            )
28            .await
29        {
30            error!("Failed to send ephemeral response: {}", e);
31        }
32    }
33
34    /// Register slash commands with Discord
35    /// If guild_id is provided, registers commands for that guild (instant update)
36    /// Otherwise registers global commands (can take up to 1 hour to propagate)
37    pub async fn register_commands(
38        ctx: &Context,
39        guild_id: Option<GuildId>,
40        commands: Vec<CreateCommand>,
41    ) {
42        if let Some(guild_id) = guild_id {
43            match guild_id.set_commands(&ctx.http, commands.clone()).await {
44                Ok(cmds) => info!("Registered {} commands in guild {}", cmds.len(), guild_id),
45                Err(e) => error!("Failed to register guild commands: {}", e),
46            }
47        } else {
48            match Command::set_global_commands(&ctx.http, commands).await {
49                Ok(cmds) => info!("Registered {} global commands", cmds.len()),
50                Err(e) => error!("Failed to register global commands: {}", e),
51            }
52        }
53    }
54}