diff options
| author | seth <[email protected]> | 2023-07-10 00:18:36 -0400 |
|---|---|---|
| committer | seth <[email protected]> | 2023-11-16 00:35:07 +0000 |
| commit | a4a9353e1c8f902b7d7b3cf74e3e5b129c214330 (patch) | |
| tree | b58da1d30af52e97c0251e0d6882cd0ccdfeb20a /src/commands/convert.rs | |
| parent | 5e9ec7f008e01d25c0b7f782c5ae043bc9ca0933 (diff) | |
start using poise
Diffstat (limited to 'src/commands/convert.rs')
| -rw-r--r-- | src/commands/convert.rs | 88 |
1 files changed, 23 insertions, 65 deletions
diff --git a/src/commands/convert.rs b/src/commands/convert.rs index 8f8c424..c7e09c9 100644 --- a/src/commands/convert.rs +++ b/src/commands/convert.rs @@ -1,70 +1,28 @@ -use crate::utils; -use serenity::builder::CreateApplicationCommand; -use serenity::model::prelude::command::CommandOptionType; -use serenity::model::prelude::interaction::application_command::{ - CommandDataOption, CommandDataOptionValue, -}; +use crate::{Context, Error}; -pub fn run(options: &[CommandDataOption]) -> String { - let err = "couldn't get convert subcommand!"; - let data = options - .get(0) - .unwrap_or_else(|| panic!("{} {:?}", err, options)); - let subcommand = data.name.as_str(); - // get message content - let option = data - .options - .get(0) - .unwrap_or_else(|| panic!("{} {:?}", err, data)) - .resolved - .as_ref() - .expect("failed to resolve string!"); - - let temp = if let &CommandDataOptionValue::Number(number) = option { - match subcommand { - "fahrenheit" => Some(utils::celsius_to_fahrenheit(number)), - "celsius" => Some(utils::fahrenheit_to_celsius(number)), - _ => None, - } - } else { - None - }; +#[poise::command(slash_command, subcommands("to_fahrenheit", "to_celsius"))] +pub async fn convert(_ctx: Context<'_>) -> Result<(), Error> { + Ok(()) +} - if let Some(temp) = temp { - format!("{temp:.2}") - } else { - "couldn't figure it out oops".to_owned() - } +/// ask teawie to convert °F to °C +#[poise::command(slash_command)] +pub async fn to_celsius( + ctx: Context<'_>, + #[description = "what teawie will convert"] degrees_fahrenheit: f32, +) -> Result<(), Error> { + let temp = (degrees_fahrenheit - 32.0) * (5.0 / 9.0); + ctx.say(temp.to_string()).await?; + Ok(()) } -pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { - command - .name("convertto") - .description("ask teawie to convert something for you") - .create_option(|option| { - option - .name("fahrenheit") - .description("ask teawie to convert celsius to fahrenheit") - .kind(CommandOptionType::SubCommand) - .create_sub_option(|suboption| { - suboption - .name("degrees_celsius") - .description("what teawie will convert") - .kind(CommandOptionType::Number) - .required(true) - }) - }) - .create_option(|option| { - option - .name("celsius") - .description("ask teawie to convert fahrenheit to celsius") - .kind(CommandOptionType::SubCommand) - .create_sub_option(|suboption| { - suboption - .name("degrees_fahrenheit") - .description("what teawie will convert") - .kind(CommandOptionType::Number) - .required(true) - }) - }) +/// ask teawie to convert °C to °F +#[poise::command(slash_command)] +pub async fn to_fahrenheit( + ctx: Context<'_>, + #[description = "what teawie will convert"] degrees_celsius: f32, +) -> Result<(), Error> { + let temp = (degrees_celsius * (9.0 / 5.0)) + 32.0; + ctx.say(temp.to_string()).await?; + Ok(()) } |
