slash_commands/
slash_commands.rs1use bevy::prelude::*;
5use bevy_discord::DiscordBotPlugin;
6use bevy_discord::config::DiscordBotConfig;
7use bevy_discord::messages::bot::*;
8use bevy_discord::runtime::tokio_runtime;
9use bevy_discord::serenity::all::{
10 Command, CreateCommand, CreateInteractionResponse, CreateInteractionResponseMessage,
11 GatewayIntents,
12};
13use bevy_discord::serenity::model::application::Interaction;
14
15fn main() {
16 let config = DiscordBotConfig::default()
17 .token("YOUR_BOT_TOKEN_HERE".to_string())
18 .gateway_intents(GatewayIntents::GUILDS);
19
20 App::new()
21 .add_plugins(MinimalPlugins)
22 .add_plugins(DiscordBotPlugin::new(config))
23 .add_systems(Update, (handle_ready, handle_interactions))
24 .run();
25}
26
27fn handle_ready(mut ready_events: MessageReader<BotReadyMessage>) {
28 for event in ready_events.read() {
29 let http = event.ctx.http.clone();
30
31 tokio_runtime().spawn(async move {
33 let command = Command::create_global_command(
34 &http,
35 CreateCommand::new("ping").description("A simple ping command"),
36 )
37 .await;
38
39 if let Err(why) = command {
40 println!("Error creating command: {:?}", why);
41 }
42 });
43 }
44}
45
46fn handle_interactions(mut interaction_events: MessageReader<InteractionCreateMessage>) {
47 for event in interaction_events.read() {
48 if let Interaction::Command(command) = &event.interaction {
49 if command.data.name.as_str() == "ping" {
50 let http = event.ctx.http.clone();
51 let command = command.clone();
52
53 tokio_runtime().spawn(async move {
54 let _ = command
55 .create_response(
56 &http,
57 CreateInteractionResponse::Message(
58 CreateInteractionResponseMessage::new().content("Pong! 🏓"),
59 ),
60 )
61 .await;
62 });
63 }
64 }
65 }
66}