summaryrefslogtreecommitdiff
path: root/src/commands/convert.rs
diff options
context:
space:
mode:
authorseth <[email protected]>2023-06-28 13:41:00 -0400
committerseth <[email protected]>2023-06-28 13:41:00 -0400
commit33c8fc7bdc1d299301a9713cefdf85f27a1b7b69 (patch)
treeff83d8c5daf474a4dbf3d45d348057ffbfb8eaff /src/commands/convert.rs
parentca089aadb68dd403ecb4c371994b0a5d40d3778b (diff)
feat: add convertto commands
Diffstat (limited to 'src/commands/convert.rs')
-rw-r--r--src/commands/convert.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/commands/convert.rs b/src/commands/convert.rs
new file mode 100644
index 0000000..6fe141d
--- /dev/null
+++ b/src/commands/convert.rs
@@ -0,0 +1,68 @@
+use crate::utils;
+use serenity::builder::CreateApplicationCommand;
+use serenity::model::prelude::command::CommandOptionType;
+use serenity::model::prelude::interaction::application_command::{
+ CommandDataOption, CommandDataOptionValue,
+};
+
+pub async 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 mut ret = 0.0;
+ if let CommandDataOptionValue::Number(number) = option {
+ match subcommand {
+ "fahrenheit" => ret = utils::celsius_to_fahrenheit(number).await,
+ "celsius" => ret = utils::fahrenheit_to_celsius(number).await,
+ _ => ret = 0.0,
+ };
+ };
+
+ if ret == 0.0 {
+ return "couldn't figure it out oops".to_string();
+ }
+ format!("{:.2}", ret)
+}
+
+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)
+ })
+ })
+}