Skip to content

Commit

Permalink
Fix examples 03, 14, and 17
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Jun 24, 2022
1 parent b5cb6a0 commit 1175cdc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
12 changes: 8 additions & 4 deletions examples/e03_struct_utilities/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ impl EventHandler for Handler {
// In this case, you can direct message a User directly by simply
// calling a method on its instance, with the content of the
// message.
let dm = msg.author.dm(&context, |m| m.content("Hello!")).await;

if let Err(why) = dm {
println!("Error when direct messaging user: {:?}", why);
match msg.author.dm(&context).await {
Ok(create_message) => {
let msg = create_message.content("Hello!").execute(&context).await;
if let Err(why) = msg {
println!("Error when direct messaging user: {:?}", why);
}
},
Err(why) => println!("Error when direct messaging user: {:?}", why),
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions examples/e14_slash_commands/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;

use serenity::async_trait;
use serenity::builder::*;
use serenity::model::application::command::{Command, CommandOptionType};
use serenity::model::application::interaction::application_command::CommandDataOptionValue;
use serenity::model::application::interaction::{Interaction, InteractionResponseType};
Expand Down Expand Up @@ -56,12 +57,12 @@ impl EventHandler for Handler {
_ => "not implemented :(".to_string(),
};

let data = CreateInteractionResponseData::default().content(content);
if let Err(why) = command
.create_interaction_response(&ctx.http, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message| message.content(content))
})
.create_interaction_response()
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(data)
.execute(&ctx.http)
.await
{
println!("Cannot respond to slash command: {}", why);
Expand Down
39 changes: 20 additions & 19 deletions examples/e17_message_components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{env, fmt};

use dotenv::dotenv;
use serenity::async_trait;
use serenity::builder::{CreateActionRow, CreateButton, CreateSelectMenu, CreateSelectMenuOption};
use serenity::builder::*;
use serenity::client::{Context, EventHandler};
use serenity::futures::StreamExt;
use serenity::model::application::component::ButtonStyle;
Expand Down Expand Up @@ -195,14 +195,15 @@ impl EventHandler for Handler {
let animal = Animal::from_str(mci.data.values.get(0).unwrap()).unwrap();

// Acknowledge the interaction and edit the message
mci.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::UpdateMessage).interaction_response_data(|d| {
d.content(format!("You chose: **{}**\nNow choose a sound!", animal))
.components(|c| c.add_action_row(Sound::action_row()))
})
})
.await
.unwrap();
let data = CreateInteractionResponseData::default()
.content(format!("You chose: **{}**\nNow choose a sound!", animal))
.components(|c| c.add_action_row(Sound::action_row()));
mci.create_interaction_response()
.kind(InteractionResponseType::UpdateMessage)
.interaction_response_data(data)
.execute(&ctx)
.await
.unwrap();

// Wait for multiple interactions

Expand All @@ -211,18 +212,18 @@ impl EventHandler for Handler {

while let Some(mci) = cib.next().await {
let sound = Sound::from_str(&mci.data.custom_id).unwrap();
let data = CreateInteractionResponseData::default()
// Make the message hidden for other users by setting `ephemeral(true)`.
.ephemeral(true)
.content(format!("The **{}** says __{}__", animal, sound));
// Acknowledge the interaction and send a reply
mci.create_interaction_response(&ctx, |r| {
mci.create_interaction_response()
// This time we dont edit the message but reply to it
r.kind(InteractionResponseType::ChannelMessageWithSource).interaction_response_data(
|d| {
// Make the message hidden for other users by setting `ephemeral(true)`.
d.ephemeral(true).content(format!("The **{}** says __{}__", animal, sound))
},
)
})
.await
.unwrap();
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(data)
.execute(&ctx)
.await
.unwrap();
}

// Delete the orig message or there will be dangling components
Expand Down
6 changes: 5 additions & 1 deletion src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,11 @@ impl ChannelId {
/// Refer to the documentation for [`CreateMessage`] for more information regarding message
/// restrictions and requirements.
pub fn send_message<'a>(self) -> CreateMessage<'a> {
CreateMessage::new(self, None)
CreateMessage::new(
self,
#[cfg(feature = "cache")]
None,
)
}

/// Starts typing in the channel for an indefinite period of time.
Expand Down
6 changes: 5 additions & 1 deletion src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,11 @@ impl GuildChannel {
/// Refer to the documentation for [`CreateMessage`] for more information regarding message
/// restrictions and requirements.
pub fn send_message<'a>(&self) -> CreateMessage<'a> {
CreateMessage::new(self.id, Some(self.guild_id))
CreateMessage::new(
self.id,
#[cfg(feature = "cache")]
Some(self.guild_id),
)
}

/// Starts typing in the channel for an indefinite period of time.
Expand Down

0 comments on commit 1175cdc

Please sign in to comment.