From 1175cdc48ab2b68902442f4f33d7fbe884f013ce Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Fri, 24 Jun 2022 13:11:45 -0400 Subject: [PATCH] Fix examples 03, 14, and 17 --- examples/e03_struct_utilities/src/main.rs | 12 ++++--- examples/e14_slash_commands/src/main.rs | 11 +++--- examples/e17_message_components/src/main.rs | 39 +++++++++++---------- src/model/channel/channel_id.rs | 6 +++- src/model/channel/guild_channel.rs | 6 +++- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/examples/e03_struct_utilities/src/main.rs b/examples/e03_struct_utilities/src/main.rs index 29a854ba0a9..825ac1032ef 100644 --- a/examples/e03_struct_utilities/src/main.rs +++ b/examples/e03_struct_utilities/src/main.rs @@ -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), } } } diff --git a/examples/e14_slash_commands/src/main.rs b/examples/e14_slash_commands/src/main.rs index a58670862f9..330bd243807 100644 --- a/examples/e14_slash_commands/src/main.rs +++ b/examples/e14_slash_commands/src/main.rs @@ -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}; @@ -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); diff --git a/examples/e17_message_components/src/main.rs b/examples/e17_message_components/src/main.rs index 03a0a3e42ba..223e7d8e79f 100644 --- a/examples/e17_message_components/src/main.rs +++ b/examples/e17_message_components/src/main.rs @@ -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; @@ -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 @@ -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 diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index 6377cdcda9a..0f81f615cd7 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -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. diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index f265ca4daa9..3cc04d59e2c 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -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.