Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support all interactions features (#1283)
This commit adds support to all interactions features, including slash commands permissions, which allow to restrict slash command use to a specific user/role. It also adds the `application_id` to the Http client (and Client Builder) instead of adding it to each request.
- Loading branch information
1 parent
dca5c45
commit d6e86df
Showing
28 changed files
with
2,223 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ members = [ | |
"e11_gateway_intents", | ||
"e12_global_data", | ||
"e13_parallel_loops", | ||
"e14_slash_commands" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "e14_slash_commands" | ||
version = "0.1.0" | ||
authors = ["my name <my@email.address>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "unstable_discord_api"] } | ||
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
extend = "../../Makefile.toml" | ||
|
||
[tasks.examples_build] | ||
alias = "build" | ||
|
||
[tasks.examples_build_release] | ||
alias = "build_release" | ||
|
||
[tasks.examples_run] | ||
alias = "run" | ||
|
||
[tasks.examples_run_release] | ||
alias = "run_release" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::env; | ||
|
||
use serenity::{ | ||
async_trait, | ||
client::bridge::gateway::GatewayIntents, | ||
model::{gateway::Ready, interactions::{Interaction, InteractionResponseType, ApplicationCommand}}, | ||
prelude::*, | ||
}; | ||
|
||
struct Handler; | ||
|
||
#[async_trait] | ||
impl EventHandler for Handler { | ||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) { | ||
interaction | ||
.create_interaction_response(&ctx.http, |response| { | ||
response | ||
.kind(InteractionResponseType::ChannelMessageWithSource) | ||
.interaction_response_data(|message| message.content("Received event!")) | ||
}) | ||
.await; | ||
} | ||
|
||
async fn ready(&self, ctx: Context, ready: Ready) { | ||
println!("{} is connected!", ready.user.name); | ||
|
||
let interactions = ApplicationCommand::get_global_application_commands(&ctx.http).await; | ||
|
||
println!("I have the following global slash command(s): {:?}", interactions); | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Configure the client with your Discord bot token in the environment. | ||
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); | ||
|
||
// The Application Id is usually the Bot User Id. | ||
let application_id: u64 = | ||
env::var("APPLICATION_ID").expect("Expected an application id in the environment").parse().expect("application id is not a valid id"); | ||
|
||
// Build our client. | ||
let mut client = Client::builder(token) | ||
.event_handler(Handler) | ||
.application_id(application_id) | ||
.await | ||
.expect("Error creating client"); | ||
|
||
// Finally, start a single shard, and start listening to events. | ||
// | ||
// Shards will automatically attempt to reconnect, and will perform | ||
// exponential backoff until it reconnects. | ||
if let Err(why) = client.start().await { | ||
println!("Client error: {:?}", why); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.