separate interactions and handler
This commit is contained in:
		
							
								
								
									
										1
									
								
								src/commands/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/commands/mod.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
pub mod general;
 | 
			
		||||
							
								
								
									
										28
									
								
								src/handler/interactions.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/handler/interactions.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
use serenity::builder::{CreateApplicationCommand, CreateInteractionResponseData};
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
 | 
			
		||||
pub fn general() -> Vec<CreateApplicationCommand> {
 | 
			
		||||
    let allah: CreateApplicationCommand = CreateApplicationCommand(HashMap::new())
 | 
			
		||||
        .name("allah")
 | 
			
		||||
        .description("acha bhay islam")
 | 
			
		||||
        .to_owned();
 | 
			
		||||
    let ping: CreateApplicationCommand = CreateApplicationCommand(HashMap::new())
 | 
			
		||||
        .name("ping")
 | 
			
		||||
        .description("Pong! bhay")
 | 
			
		||||
        .to_owned();
 | 
			
		||||
    let chut: CreateApplicationCommand = CreateApplicationCommand(HashMap::new())
 | 
			
		||||
        .name("chut")
 | 
			
		||||
        .description("yummy parantha")
 | 
			
		||||
        .to_owned();
 | 
			
		||||
    vec![allah, ping, chut]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn responses(
 | 
			
		||||
    interaction_name: String,
 | 
			
		||||
    message: &mut CreateInteractionResponseData,
 | 
			
		||||
) -> &mut CreateInteractionResponseData {
 | 
			
		||||
    match interaction_name.as_str() {
 | 
			
		||||
        "allah" => message.content("suar ki chamdi".to_string()),
 | 
			
		||||
        _ => message.content("na hai bhay".to_string()),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										46
									
								
								src/handler/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/handler/mod.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
mod interactions;
 | 
			
		||||
use serenity::{
 | 
			
		||||
    async_trait,
 | 
			
		||||
    model::{
 | 
			
		||||
        event::ResumedEvent,
 | 
			
		||||
        gateway::Ready,
 | 
			
		||||
        interactions::{
 | 
			
		||||
            ApplicationCommand, Interaction, InteractionData, InteractionResponseType,
 | 
			
		||||
            InteractionType,
 | 
			
		||||
        },
 | 
			
		||||
    },
 | 
			
		||||
    prelude::*,
 | 
			
		||||
};
 | 
			
		||||
use tracing::info;
 | 
			
		||||
 | 
			
		||||
pub struct Handler;
 | 
			
		||||
 | 
			
		||||
#[async_trait]
 | 
			
		||||
impl EventHandler for Handler {
 | 
			
		||||
    async fn ready(&self, ctx: Context, ready: Ready) {
 | 
			
		||||
        info!("{} connected bhay", ready.user.name);
 | 
			
		||||
        let _ = ApplicationCommand::create_global_application_commands(&ctx.http, |commands| {
 | 
			
		||||
            commands.set_application_commands(interactions::general())
 | 
			
		||||
        })
 | 
			
		||||
        .await;
 | 
			
		||||
    }
 | 
			
		||||
    async fn resume(&self, _: Context, _: ResumedEvent) {
 | 
			
		||||
        info!("how th when the");
 | 
			
		||||
    }
 | 
			
		||||
    async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
 | 
			
		||||
        if interaction.kind == InteractionType::ApplicationCommand {
 | 
			
		||||
            if let Some(InteractionData::ApplicationCommand(data)) = interaction.data.as_ref() {
 | 
			
		||||
                if let Err(why) = interaction
 | 
			
		||||
                    .create_interaction_response(&ctx.http, |response| {
 | 
			
		||||
                        response
 | 
			
		||||
                            .kind(InteractionResponseType::ChannelMessageWithSource)
 | 
			
		||||
                            .interaction_response_data(|message| interactions::responses(data.name.to_string(), message))
 | 
			
		||||
                    })
 | 
			
		||||
                    .await
 | 
			
		||||
                {
 | 
			
		||||
                    println!("Cannot respond to slash command: {}", why);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										56
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,61 +1,21 @@
 | 
			
		||||
mod general;
 | 
			
		||||
mod commands;
 | 
			
		||||
mod handler;
 | 
			
		||||
use serenity::{
 | 
			
		||||
    async_trait,
 | 
			
		||||
    client::bridge::gateway::ShardManager,
 | 
			
		||||
    framework::{standard::macros::group, StandardFramework},
 | 
			
		||||
    http::Http,
 | 
			
		||||
    model::{
 | 
			
		||||
        event::ResumedEvent,
 | 
			
		||||
        gateway::Ready,
 | 
			
		||||
        interactions::{ApplicationCommand, Interaction, InteractionResponseType, InteractionType, InteractionData},
 | 
			
		||||
    },
 | 
			
		||||
    prelude::*,
 | 
			
		||||
};
 | 
			
		||||
use std::{collections::HashSet, env, sync::Arc};
 | 
			
		||||
use tracing::{error, info};
 | 
			
		||||
 | 
			
		||||
use tracing::error;
 | 
			
		||||
pub struct ShardManagerContainer;
 | 
			
		||||
use general::*;
 | 
			
		||||
use commands::general::*;
 | 
			
		||||
use handler::Handler;
 | 
			
		||||
 | 
			
		||||
impl TypeMapKey for ShardManagerContainer {
 | 
			
		||||
    type Value = Arc<Mutex<ShardManager>>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Handler;
 | 
			
		||||
 | 
			
		||||
#[async_trait]
 | 
			
		||||
impl EventHandler for Handler {
 | 
			
		||||
    async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
 | 
			
		||||
        if interaction.kind == InteractionType::ApplicationCommand {
 | 
			
		||||
            if let Some(InteractionData::ApplicationCommand(data)) = interaction.data.as_ref() {
 | 
			
		||||
                let content = match data.name.as_str() {
 | 
			
		||||
                    "ping" => "Hey, I'm alive!".to_string(),
 | 
			
		||||
                    _ => "not implemented :(".to_string(),
 | 
			
		||||
                };
 | 
			
		||||
 | 
			
		||||
                if let Err(why) = interaction
 | 
			
		||||
                    .create_interaction_response(&ctx.http, |response| {
 | 
			
		||||
                        response
 | 
			
		||||
                            .kind(InteractionResponseType::ChannelMessageWithSource)
 | 
			
		||||
                            .interaction_response_data(|message| message.content(content))
 | 
			
		||||
                    })
 | 
			
		||||
                    .await
 | 
			
		||||
                {
 | 
			
		||||
                    println!("Cannot respond to slash command: {}", why);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn ready(&self, _: Context, ready: Ready) {
 | 
			
		||||
        info!("{} connected bhay", ready.user.name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn resume(&self, _: Context, _: ResumedEvent) {
 | 
			
		||||
        info!("how th when the");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[group]
 | 
			
		||||
#[commands(ping)]
 | 
			
		||||
struct General;
 | 
			
		||||
@@ -63,9 +23,7 @@ struct General;
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() {
 | 
			
		||||
    let token = env::var("DISCORD_TOKEN").expect("Token daal madarchod");
 | 
			
		||||
 | 
			
		||||
    let http = Http::new_with_token(&token);
 | 
			
		||||
 | 
			
		||||
    let (owners, bot_id) = match http.get_current_application_info().await {
 | 
			
		||||
        Ok(info) => {
 | 
			
		||||
            let mut owners = HashSet::new();
 | 
			
		||||
@@ -93,10 +51,6 @@ async fn main() {
 | 
			
		||||
        let mut data = client.data.write().await;
 | 
			
		||||
        data.insert::<ShardManagerContainer>(client.shard_manager.clone());
 | 
			
		||||
    }
 | 
			
		||||
    let _ = ApplicationCommand::create_global_application_command(&http, |a| {
 | 
			
		||||
        a.name("ping").description("A simple ping command")
 | 
			
		||||
    })
 | 
			
		||||
    .await;
 | 
			
		||||
 | 
			
		||||
    if let Err(why) = client.start().await {
 | 
			
		||||
        error!("Client error: {:?}", why);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user