shibe-bot/src/command/util.rs
2024-05-16 17:22:29 -04:00

147 lines
4.8 KiB
Rust

use poise::serenity_prelude as serenity;
use rand::Rng;
use crate::Context;
use crate::Error;
const INVITE_LINK: &str = "https://discord.com/oauth2/authorize?client_id=1030701552941412382&permissions=116736&response_type=code&redirect_uri=https%3A%2F%2Fdiscordapp.com%2Foauth2%2Fauthorize%3F%26client_id%3D1030701552941412382%26scope%3Dbot&scope=guilds+bot";
/// Add this bot to your server
#[poise::command(slash_command)]
pub async fn invite(ctx: Context<'_>) -> Result<(), Error> {
ctx.defer_ephemeral().await?;
ctx.say(format!(
"To add me to your server, click [this link]({}), or open it in the \
browser and enable all the requested permissions. Then select your \
server to add it.",
INVITE_LINK
))
.await?;
info!("Executed command `invite` successfully");
Ok(())
}
/// Displays your or another user's account creation date
#[poise::command(slash_command)]
pub async fn age(
ctx: Context<'_>,
#[description = "Selected user"] user: Option<serenity::User>,
) -> Result<(), Error> {
let u = user.as_ref().unwrap_or_else(|| ctx.author());
let response = format!("{}'s account was created at {}", u.name, u.created_at());
ctx.say(response).await?;
info!("Executed command `age` successfully");
Ok(())
}
/// Show information about this bot
#[poise::command(slash_command, global_cooldown = 30)]
pub async fn info(ctx: Context<'_>) -> Result<(), Error> {
ctx.say(format!(
"Shibe Bot v{} was created by Shibe Drill (@shibedrill) using Rust and \
Poise.\nWebsite: <https://riverdev.carrd.co>\n\
Source code: <https://github.com/shibedrill/shibe-bot>\n\
Poise: <https://docs.rs/poise/latest/poise/>\n\
Rust: <https://www.rust-lang.org/>",
env!("CARGO_PKG_VERSION")
))
.await?;
info!("Executed command `info` successfully");
Ok(())
}
/// Add channel to the registry
#[poise::command(slash_command, required_permissions = "MANAGE_CHANNELS")]
pub async fn add_channel(
ctx: Context<'_>,
#[description = "Selected channel"] channel: serenity::Channel,
) -> Result<(), Error> {
let config = &mut ctx.data().config_manager.lock().await;
let channel_id = { u64::from(channel.id()) };
config.channels.push(channel);
config.store().expect("Unable to store config");
ctx.say(format!(
"Successfully added <#{}> to the channel registry.",
channel_id
))
.await?;
info!("Executed command `add_channel` successfully");
Ok(())
}
/// Remove channel from the registry
#[poise::command(slash_command, required_permissions = "MANAGE_CHANNELS")]
pub async fn remove_channel(
ctx: Context<'_>,
#[description = "Selected channel"] channel: serenity::Channel,
) -> Result<(), Error> {
ctx.defer_ephemeral().await?;
let config = &mut ctx.data().config_manager.lock().await;
let channel_id = { u64::from(channel.id()) };
config.channels.retain(|c| c.id() != channel.id());
config.store().expect("Unable to store config");
ctx.say(format!(
"Successfully removed <#{}> from the channel registry.",
channel_id
))
.await?;
info!("Executed command `remove_channel` successfully");
Ok(())
}
/// List channels held in the registry
#[poise::command(slash_command, required_permissions = "MANAGE_CHANNELS")]
pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> {
ctx.defer_ephemeral().await?;
let config = &mut ctx.data().config_manager.lock().await;
let mut channel_ids: Vec<u64> = vec![];
config
.channels
.iter()
.for_each(|c| channel_ids.push(u64::from(c.id())));
ctx.say(format!(
"Current channel IDs in registry: \n{:#?}",
channel_ids
))
.await?;
info!("Executed command `list_channels` successfully");
Ok(())
}
/// Roll a dice with a certain amount of sides, 2 sides is a coin toss
#[poise::command(slash_command)]
pub async fn dice(
ctx: Context<'_>,
#[description = "The amount of sides on the dice"] sides: u32,
) -> Result<(), Error> {
let answer: u32 = {
let mut rng = rand::thread_rng();
rng.gen_range(1..=sides)
};
let response = match sides {
0 | 1 => {
ctx.defer_ephemeral().await?;
String::from("You cannot roll a dice with 0 or 1 sides.")
}
2 => {
format!(
"Coin toss landed on: {}",
match answer {
1 => "heads",
2 => "tails",
_ => unreachable!(),
}
)
}
_ => {
format!(
"Rolled a random number from 1 to {}, got: {}",
sides, answer
)
}
};
ctx.say(response).await?;
info!("Executed command `dice` successfully");
Ok(())
}