Advertisement
Ardente

Untitled

Jun 11th, 2022
1,522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.61 KB | None | 0 0
  1. use serenity::async_trait;
  2. use serenity::model::prelude::*;
  3. use serenity::prelude::*;
  4. use std::time::{Duration, SystemTime};
  5.  
  6. const VOID_CHANNEL_ID: u64 = 952_722_147_099_422_730;
  7. const OWNER_USER_ID: u64 = 348_275_601_934_778_368;
  8.  
  9. pub struct Handler;
  10.  
  11. #[async_trait]
  12. impl EventHandler for Handler {
  13.     async fn message(&self, ctx: Context, new_message: Message) {
  14.         if new_message.channel_id.0 == VOID_CHANNEL_ID {
  15.             if let Err(why) = new_message.delete(&ctx.http).await {
  16.                 eprintln!("{:#?}", why);
  17.             }
  18.         }
  19.     }
  20.  
  21.     async fn reaction_add(&self, ctx: Context, add_reaction: Reaction) {
  22.         if add_reaction.emoji.as_data() == *"🗑️" {
  23.             match add_reaction.message(&ctx.http).await {
  24.                 Ok(msg) => {
  25.                     if let Err(why) = msg.delete(&ctx.http).await {
  26.                         eprintln!("{:#?}", why);
  27.                     }
  28.                 }
  29.                 Err(why) => eprintln!("{:#?}", why),
  30.             }
  31.         }
  32.     }
  33.  
  34.     async fn ready(&self, ctx: Context, data_about_bot: Ready) {
  35.         let me = ctx.http.get_user(OWNER_USER_ID).await.unwrap();
  36.  
  37.         match me.direct_message(&ctx.http, |msg| {
  38.             msg.embed(|embed| {
  39.                 embed.title(format!("{} is Awake and Ready to Run!", data_about_bot.user.name))
  40.                     .thumbnail(data_about_bot.user.face())
  41.                     .description(format!("It's <t:{}:f>! What are y'all doing standing around?! We've got work to do! WHERE'S MY COFFEE?", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()))
  42.             })
  43.         }).await {
  44.             Ok(message) => {
  45.                 tokio::time::sleep(Duration::from_secs(60)).await;
  46.                 if let Err(why) = message.delete(&ctx.http).await {
  47.                     eprintln!("{:#?}", why);
  48.                 }
  49.             },
  50.             Err(why) => eprintln!("{:#?}", why),
  51.         }
  52.     }
  53.  
  54.     #[allow(clippy::single_match)]
  55.     async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
  56.         match interaction {
  57.             Interaction::ApplicationCommand(command) => {
  58.                 if let Err(why) = parse_application_command(ctx, command).await {
  59.                     eprintln!("{:#?}", why);
  60.                 }
  61.             }
  62.             Interaction::Autocomplete(autocomplete) => {
  63.                 use serde_json::Value;
  64.  
  65.                 let options = &autocomplete.data.options;
  66.                 let mut prediction_groups = vec![];
  67.  
  68.                 for option in options {
  69.                     if let Some(Value::String(value)) = &option.value {
  70.                         let results = crate::fuzzy::fuzzy_search(&ctx, value).await;
  71.  
  72.                         match results {
  73.                             Ok(results) => prediction_groups.push(results),
  74.                             Err(why) => eprintln!("{:#?}", why),
  75.                         }
  76.                     }
  77.                 }
  78.  
  79.                 if let Err(why) = autocomplete
  80.                     .create_autocomplete_response(&ctx.http, |response| {
  81.                         for prediction_group in prediction_groups {
  82.                             for prediction in prediction_group.predictions {
  83.                                 response.add_string_choice(
  84.                                     &prediction.description,
  85.                                     &prediction.description,
  86.                                 );
  87.                             }
  88.                         }
  89.  
  90.                         response
  91.                     })
  92.                     .await
  93.                 {
  94.                     eprintln!("{:#?}", why);
  95.                 }
  96.             }
  97.             _ => {}
  98.         }
  99.     }
  100. }
  101.  
  102. #[allow(clippy::single_match)]
  103. async fn parse_application_command(
  104.     ctx: Context,
  105.     interaction: application_command::ApplicationCommandInteraction,
  106. ) -> anyhow::Result<()> {
  107.     use interactions::application_command::ApplicationCommandType;
  108.  
  109.     match interaction.data.kind {
  110.         ApplicationCommandType::ChatInput => parse_slash_commands(ctx, interaction).await?,
  111.         _ => {}
  112.     }
  113.  
  114.     Ok(())
  115. }
  116.  
  117. #[allow(clippy::single_match)]
  118. async fn parse_slash_commands(
  119.     ctx: Context,
  120.     interaction: application_command::ApplicationCommandInteraction,
  121. ) -> anyhow::Result<()> {
  122.     use crate::commands::directions;
  123.     use crate::commands::user_info;
  124.  
  125.     match &*interaction.data.name {
  126.         "user-info" => user_info::execute(ctx, interaction).await?,
  127.         "directions" => directions::execute(ctx, interaction).await?,
  128.         _ => {}
  129.     }
  130.  
  131.     Ok(())
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement