Skip to content

Commit

Permalink
Fix breakage caused and use Cow builders
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Jan 6, 2024
1 parent 0d13f14 commit dad39fe
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 126 deletions.
54 changes: 28 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/feature_showcase/autocomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn autocomplete_name<'a>(
async fn autocomplete_number(
_ctx: Context<'_>,
_partial: &str,
) -> impl Iterator<Item = serenity::AutocompleteChoice> {
) -> impl Iterator<Item = serenity::AutocompleteChoice<'static>> {
// Dummy choices
[1_u32, 2, 3, 4, 5].iter().map(|&n| {
serenity::AutocompleteChoice::new(
Expand Down
8 changes: 5 additions & 3 deletions examples/fluent_localization/translation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Wraps the fluent API and provides easy to use functions and macros for translation
use std::borrow::Cow;

use crate::{Context, Data, Error};

type FluentBundle = fluent::bundle::FluentBundle<
Expand Down Expand Up @@ -136,8 +138,8 @@ pub fn apply_translations(
// If this is a choice parameter, insert its localized variants
for choice in &mut parameter.choices {
choice.localizations.insert(
locale.clone(),
format(bundle, &choice.name, None, None).unwrap(),
Cow::Owned(locale.clone()),
Cow::Owned(format(bundle, &choice.name, None, None).unwrap()),
);
}
}
Expand Down Expand Up @@ -171,7 +173,7 @@ pub fn apply_translations(

// If this is a choice parameter, set the choice names to en-US
for choice in &mut parameter.choices {
choice.name = format(bundle, &choice.name, None, None).unwrap();
choice.name = Cow::Owned(format(bundle, &choice.name, None, None).unwrap());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/invocation_data/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn main() {
poise::builtins::create_application_commands(&framework.options().commands);

serenity::GuildId::new(703332075914264606)
.set_commands(ctx, commands)
.set_commands(ctx, &commands)
.await
.unwrap();
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions macros/src/choice_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ pub fn choice_parameter(input: syn::DeriveInput) -> Result<TokenStream, darling:
fn list() -> Vec<poise::CommandParameterChoice> {
vec![ #( poise::CommandParameterChoice {
__non_exhaustive: (),
name: #names.to_string(),
name: #names.into(),
localizations: std::collections::HashMap::from([
#( (#locales.to_string(), #localized_names.to_string()) ),*
#( (#locales.into(), #localized_names.into()) ),*
]),
}, )* ]
}
Expand Down
2 changes: 1 addition & 1 deletion macros/src/command/slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn generate_parameters(inv: &Invocation) -> Result<Vec<proc_macro2::TokenStr
let choices_stream = ::poise::into_stream!(
#autocomplete_fn(ctx.into(), partial).await
);
let choices_vec = choices_stream
let choices_vec: Vec<_> = choices_stream
.take(25)
// T or AutocompleteChoice<T> -> AutocompleteChoice<T>
.map(poise::serenity_prelude::AutocompleteChoice::from)
Expand Down
2 changes: 1 addition & 1 deletion macros/src/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn modal(input: syn::DeriveInput) -> Result<TokenStream, darling::Error> {
Ok(quote::quote! { const _: () = {
use poise::serenity_prelude as serenity;
impl #impl_generics poise::Modal for #struct_ident #ty_generics #where_clause {
fn create(mut defaults: Option<Self>, custom_id: String) -> serenity::CreateInteractionResponse {
fn create(mut defaults: Option<Self>, custom_id: String) -> serenity::CreateInteractionResponse<'static> {
serenity::CreateInteractionResponse::Modal(
serenity::CreateModal::new(custom_id, #modal_title).components(vec!{#( #builders )*})
)
Expand Down
22 changes: 11 additions & 11 deletions src/builtins/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ use crate::serenity_prelude as serenity;
/// let commands = &ctx.framework().options().commands;
/// let create_commands = poise::builtins::create_application_commands(commands);
///
/// serenity::Command::set_global_commands(ctx, create_commands).await?;
/// serenity::Command::set_global_commands(ctx, &create_commands).await?;
/// # Ok(()) }
/// ```
pub fn create_application_commands<U, E>(
commands: &[crate::Command<U, E>],
) -> Vec<serenity::CreateCommand> {
) -> Vec<serenity::CreateCommand<'static>> {
/// We decided to extract context menu commands recursively, despite the subcommand hierarchy
/// not being preserved. Because it's more confusing to just silently discard context menu
/// commands if they're not top-level commands.
/// https://discord.com/channels/381880193251409931/919310428344029265/947970605985189989
fn recursively_add_context_menu_commands<U, E>(
builder: &mut Vec<serenity::CreateCommand>,
builder: &mut Vec<serenity::CreateCommand<'static>>,
command: &crate::Command<U, E>,
) {
if let Some(context_menu_command) = command.create_as_context_menu_command() {
Expand Down Expand Up @@ -54,7 +54,7 @@ pub async fn register_globally<U, E>(
commands: &[crate::Command<U, E>],
) -> Result<(), serenity::Error> {
let builder = create_application_commands(commands);
serenity::Command::set_global_commands(http, builder).await?;
serenity::Command::set_global_commands(http, &builder).await?;
Ok(())
}

Expand All @@ -68,7 +68,7 @@ pub async fn register_in_guild<U, E>(
guild_id: serenity::GuildId,
) -> Result<(), serenity::Error> {
let builder = create_application_commands(commands);
guild_id.set_commands(http, builder).await?;
guild_id.set_commands(http, &builder).await?;
Ok(())
}

Expand Down Expand Up @@ -101,7 +101,7 @@ pub async fn register_application_commands<U, E>(
if global {
ctx.say(format!("Registering {num_commands} commands...",))
.await?;
serenity::Command::set_global_commands(ctx, commands_builder).await?;
serenity::Command::set_global_commands(ctx, &commands_builder).await?;
} else {
let guild_id = match ctx.guild_id() {
Some(x) => x,
Expand All @@ -113,7 +113,7 @@ pub async fn register_application_commands<U, E>(

ctx.say(format!("Registering {num_commands} commands..."))
.await?;
guild_id.set_commands(ctx, commands_builder).await?;
guild_id.set_commands(ctx, &commands_builder).await?;
}

ctx.say("Done!").await?;
Expand Down Expand Up @@ -228,10 +228,10 @@ pub async fn register_application_commands_buttons<U, E>(
":gear: Registering {num_commands} global commands...",
))
.await?;
serenity::Command::set_global_commands(ctx, create_commands).await?;
serenity::Command::set_global_commands(ctx, &create_commands).await?;
} else {
ctx.say(":gear: Unregistering global commands...").await?;
serenity::Command::set_global_commands(ctx, vec![]).await?;
serenity::Command::set_global_commands(ctx, &[]).await?;
}
} else {
let guild_id = match ctx.guild_id() {
Expand All @@ -246,10 +246,10 @@ pub async fn register_application_commands_buttons<U, E>(
":gear: Registering {num_commands} guild commands...",
))
.await?;
guild_id.set_commands(ctx, create_commands).await?;
guild_id.set_commands(ctx, &create_commands).await?;
} else {
ctx.say(":gear: Unregistering guild commands...").await?;
guild_id.set_commands(ctx, vec![]).await?;
guild_id.set_commands(ctx, &[]).await?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/choice_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T: ChoiceParameter> crate::SlashArgument for T {
})
}

fn create(builder: serenity::CreateCommandOption) -> serenity::CreateCommandOption {
fn create(builder: serenity::CreateCommandOption<'_>) -> serenity::CreateCommandOption<'_> {
builder.kind(serenity::CommandOptionType::Integer)
}

Expand Down
7 changes: 5 additions & 2 deletions src/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn execute_modal_generic<
F: std::future::Future<Output = Result<(), serenity::Error>>,
>(
ctx: &serenity::Context,
create_interaction_response: impl FnOnce(serenity::CreateInteractionResponse) -> F,
create_interaction_response: impl FnOnce(serenity::CreateInteractionResponse<'static>) -> F,
modal_custom_id: String,
defaults: Option<M>,
timeout: Option<std::time::Duration>,
Expand Down Expand Up @@ -175,7 +175,10 @@ pub trait Modal: Sized {
///
/// Optionally takes an initialized instance as pre-filled values of this modal (see
/// [`Self::execute_with_defaults()`] for more info)
fn create(defaults: Option<Self>, custom_id: String) -> serenity::CreateInteractionResponse;
fn create(
defaults: Option<Self>,
custom_id: String,
) -> serenity::CreateInteractionResponse<'static>;

/// Parses a received modal submit interaction into this type
///
Expand Down
Loading

0 comments on commit dad39fe

Please sign in to comment.