1use crate::commands;
2use log::info;
3use serenity::async_trait;
4use serenity::model::application::command::Command;
5use serenity::model::application::interaction::Interaction;
6use serenity::model::gateway::Activity;
7use serenity::model::gateway::Ready;
8use serenity::prelude::*;
9use std::time::Duration;
10pub struct Bot {
11 pub api: String,
12}
13#[async_trait]
14impl EventHandler for Bot {
15 async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
16 if let Interaction::ApplicationCommand(command) = interaction {
17 match command.data.name.as_str() {
18 "height" => commands::height::run(self, &ctx, &command).await,
19 "hash" => commands::hash::run(self, &ctx, &command).await,
20 "block" => commands::block::run(self, &ctx, &command).await,
21 "balance" => commands::balance::run(self, &ctx, &command).await,
22 "transaction" => commands::transaction::run(self, &ctx, &command).await,
23 "stake" => commands::stake::run(self, &ctx, &command).await,
24 _ => {}
25 };
26 }
27 }
28 async fn ready(&self, ctx: Context, ready: Ready) {
29 info!("{} is connected!", ready.user.name);
30 Command::set_global_application_commands(&ctx.http, |commands| {
31 commands
32 .create_application_command(|command| commands::height::register(command))
33 .create_application_command(|command| commands::hash::register(command))
34 .create_application_command(|command| commands::block::register(command))
35 .create_application_command(|command| commands::balance::register(command))
36 .create_application_command(|command| commands::transaction::register(command))
37 .create_application_command(|command| commands::stake::register(command))
38 })
39 .await
40 .unwrap();
41 let mut i = 0;
42 loop {
43 i += 1;
44 let activity = match i {
45 1 => {
46 let height = match reqwest::get(format!("{}/height", self.api)).await {
47 Ok(res) => res.json::<usize>().await.unwrap().to_string(),
48 Err(_) => "Unknown".to_string(),
49 };
50 Activity::playing(format!("{} blocks", height))
51 }
52 2 => Activity::playing("https://tofuri.cash"),
53 _ => {
54 i = 0;
55 Activity::playing("github.com/tofuri")
56 }
57 };
58 ctx.set_activity(activity).await;
59 tokio::time::sleep(Duration::from_secs(10)).await;
60 }
61 }
62}