Skip to content

Commit

Permalink
Swap ctx.say to take Cow instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jun 22, 2024
1 parent e554f13 commit 8401811
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
36 changes: 16 additions & 20 deletions src/reply/send_reply.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! All functions to actually send a reply

use std::borrow::Cow;

use crate::serenity_prelude as serenity;

/// Send a message in the given context: normal message if prefix command, interaction response
Expand All @@ -23,13 +25,10 @@ use crate::serenity_prelude as serenity;
/// ).await?;
/// # Ok(()) }
/// ```
pub async fn send_reply<'ret, 'arg, U: Send + Sync + 'static, E>(
ctx: crate::Context<'ret, U, E>,
pub async fn send_reply<'ctx, 'arg, U: Send + Sync + 'static, E>(
ctx: crate::Context<'ctx, U, E>,
builder: crate::CreateReply<'arg>,
) -> Result<crate::ReplyHandle<'ret>, serenity::Error>
where
'ret: 'arg,
{
) -> Result<crate::ReplyHandle<'ctx>, serenity::Error> {
Ok(match ctx {
crate::Context::Prefix(ctx) => super::ReplyHandle(super::ReplyHandleInner::Prefix(
crate::send_prefix_reply(ctx, builder).await?,
Expand All @@ -41,11 +40,11 @@ where
/// Shorthand of [`send_reply`] for text-only messages
///
/// Note: panics when called in an autocomplete context!
pub async fn say_reply<U: Send + Sync + 'static, E>(
ctx: crate::Context<'_, U, E>,
text: impl Into<String>,
) -> Result<crate::ReplyHandle<'_>, serenity::Error> {
send_reply(ctx, crate::CreateReply::default().content(text.into())).await
pub async fn say_reply<'ctx, 'arg, U: Send + Sync + 'static, E>(
ctx: crate::Context<'ctx, U, E>,
text: impl Into<Cow<'arg, str>>,
) -> Result<crate::ReplyHandle<'ctx>, serenity::Error> {
send_reply(ctx, crate::CreateReply::default().content(text)).await
}

/// Send a response to an interaction (slash command or context menu command invocation).
Expand All @@ -54,13 +53,10 @@ pub async fn say_reply<U: Send + Sync + 'static, E>(
/// [followup](serenity::CommandInteraction::create_followup) is sent.
///
/// No-op if autocomplete context
pub async fn send_application_reply<'ret, 'arg, U: Send + Sync + 'static, E>(
ctx: crate::ApplicationContext<'ret, U, E>,
pub async fn send_application_reply<'ctx, 'arg, U: Send + Sync + 'static, E>(
ctx: crate::ApplicationContext<'ctx, U, E>,
builder: crate::CreateReply<'arg>,
) -> Result<crate::ReplyHandle<'ret>, serenity::Error>
where
'ret: 'arg,
{
) -> Result<crate::ReplyHandle<'ctx>, serenity::Error> {
let builder = ctx.reply_builder(builder);

if ctx.interaction_type == crate::CommandInteractionType::Autocomplete {
Expand Down Expand Up @@ -102,9 +98,9 @@ where
}

/// Prefix-specific reply function. For more details, see [`crate::send_reply`].
pub async fn send_prefix_reply<'a, U: Send + Sync + 'static, E>(
ctx: crate::PrefixContext<'a, U, E>,
builder: crate::CreateReply<'a>,
pub async fn send_prefix_reply<'ctx, 'arg, U: Send + Sync + 'static, E>(
ctx: crate::PrefixContext<'ctx, U, E>,
builder: crate::CreateReply<'arg>,
) -> Result<Box<serenity::Message>, serenity::Error> {
let builder = ctx.reply_builder(builder);

Expand Down
7 changes: 2 additions & 5 deletions src/structs/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ context_methods! {
///
/// Note: panics when called in an autocomplete context!
await (say self text)
(pub async fn say(
self,
text: impl Into<String>,
) -> Result<crate::ReplyHandle<'a>, serenity::Error>) {
(pub async fn say<'arg>(self, text: impl Into<Cow<'arg, str>>) -> Result<crate::ReplyHandle<'a>, serenity::Error>) {
crate::say_reply(self, text).await
}

Expand Down Expand Up @@ -482,7 +479,7 @@ context_methods! {
/// convert [`crate::CreateReply`] instances into Discord requests.
#[allow(unused_mut)] // side effect of how macro works
(reply_builder self builder)
(pub fn reply_builder(self, mut builder: crate::CreateReply<'a>) -> crate::CreateReply<'a>) {
(pub fn reply_builder<'args>(self, mut builder: crate::CreateReply<'args>) -> crate::CreateReply<'args>) {
let fw_options = self.framework().options();
builder.ephemeral = builder.ephemeral.or(Some(self.command().ephemeral));
builder.allowed_mentions = builder.allowed_mentions.or_else(|| fw_options.allowed_mentions.clone());
Expand Down
5 changes: 4 additions & 1 deletion src/structs/framework_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ pub struct FrameworkOptions<U, E> {
/// Allows you to modify every outgoing message in a central place
#[derivative(Debug = "ignore")]
pub reply_callback: Option<
for<'a> fn(crate::Context<'a, U, E>, crate::CreateReply<'a>) -> crate::CreateReply<'a>,
for<'ctx, 'arg> fn(
crate::Context<'ctx, U, E>,
crate::CreateReply<'arg>,
) -> crate::CreateReply<'arg>,
>,
/// If `true`, disables automatic cooldown handling before every command invocation.
///
Expand Down

0 comments on commit 8401811

Please sign in to comment.