Skip to content

Commit

Permalink
Convert poise builders to new serenity style
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 22, 2023
1 parent 9b25cb2 commit c33ebdc
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 200 deletions.
28 changes: 16 additions & 12 deletions examples/feature_showcase/collector.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use crate::{Context, Error};
use poise::serenity_prelude as serenity;
use poise::{serenity_prelude as serenity, CreateReply};

/// Boop the bot!
#[poise::command(prefix_command, track_edits, slash_command)]
pub async fn boop(ctx: Context<'_>) -> Result<(), Error> {
let uuid_boop = ctx.id();

ctx.send(|m| {
m.content("I want some boops!").components(|c| {
c.create_action_row(|ar| {
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Primary)
.label("Boop me!")
.custom_id(uuid_boop)
})
let reply = {
let mut components = serenity::CreateComponents::default();
components.create_action_row(|ar| {
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Primary)
.label("Boop me!")
.custom_id(uuid_boop)
})
})
})
.await?;
});

CreateReply::default()
.content("I want some boops!")
.components(components)
};

ctx.send(reply).await?;

let mut boop_count = 0;
while let Some(mci) = serenity::CollectComponentInteraction::new(ctx)
Expand Down
28 changes: 16 additions & 12 deletions examples/feature_showcase/modal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Data, Error};
use poise::serenity_prelude as serenity;

#[derive(Debug, poise::Modal)]
#[allow(dead_code)] // fields only used for Debug print
Expand All @@ -22,19 +23,22 @@ pub async fn modal(ctx: poise::ApplicationContext<'_, Data, Error>) -> Result<()
/// present.
#[poise::command(prefix_command, slash_command)]
pub async fn component_modal(ctx: crate::Context<'_>) -> Result<(), Error> {
ctx.send(|m| {
m.content("Click the button below to open the modal")
.components(|c| {
c.create_action_row(|a| {
a.create_button(|b| {
b.custom_id("open_modal")
.label("Open modal")
.style(poise::serenity_prelude::ButtonStyle::Success)
})
})
let mut components = serenity::CreateComponents::default();
let reply = {
components.create_action_row(|a| {
a.create_button(|b| {
b.custom_id("open_modal")
.label("Open modal")
.style(poise::serenity_prelude::ButtonStyle::Success)
})
})
.await?;
});

poise::CreateReply::default()
.content("Click the button below to open the modal")
.components(components)
};

ctx.send(reply).await?;

while let Some(mci) =
poise::serenity_prelude::CollectComponentInteraction::new(ctx.serenity_context())
Expand Down
6 changes: 1 addition & 5 deletions examples/feature_showcase/response_with_reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ use crate::{Context, Error};

#[poise::command(slash_command, prefix_command)]
pub async fn reply(ctx: Context<'_>) -> Result<(), Error> {
ctx.send(|b| {
b.content(format!("Hello {}!", ctx.author().name))
.reply(true)
})
.await?;
ctx.reply(format!("Hello {}!", ctx.author().name)).await?;
Ok(())
}
64 changes: 38 additions & 26 deletions examples/feature_showcase/track_edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,51 @@ use poise::serenity_prelude as serenity;
#[poise::command(slash_command, prefix_command, reuse_response)]
pub async fn test_reuse_response(ctx: Context<'_>) -> Result<(), Error> {
let image_url = "https://raw.githubusercontent.com/serenity-rs/serenity/current/logo.png";
ctx.send(|b| {
b.content("message 1")
.embed(|b| b.description("embed 1").image(image_url))
.components(|b| {
b.create_action_row(|b| {
b.create_button(|b| {
b.label("button 1")
.style(serenity::ButtonStyle::Primary)
.custom_id(1)
})
})

let reply = {
let mut embed = serenity::CreateEmbed::default();
embed.description("embed 1").image(image_url);

let mut components = serenity::CreateComponents::default();
components.create_action_row(|b| {
b.create_button(|b| {
b.label("button 1")
.style(serenity::ButtonStyle::Primary)
.custom_id(1)
})
})
.await?;
});

poise::CreateReply::default()
.content("message 1")
.embed(embed)
.components(components)
};

ctx.send(reply).await?;

tokio::time::sleep(std::time::Duration::from_secs(2)).await;

let image_url = "https://raw.githubusercontent.com/serenity-rs/serenity/current/examples/e09_create_message_builder/ferris_eyes.png";
ctx.send(|b| {
b.content("message 2")
.embed(|b| b.description("embed 2").image(image_url))
.components(|b| {
b.create_action_row(|b| {
b.create_button(|b| {
b.label("button 2")
.style(serenity::ButtonStyle::Danger)
.custom_id(2)
})
})
let reply = {
let mut embed = serenity::CreateEmbed::default();
embed.description("embed 2").image(image_url);

let mut components = serenity::CreateComponents::default();
components.create_action_row(|b| {
b.create_button(|b| {
b.label("button 2")
.style(serenity::ButtonStyle::Danger)
.custom_id(2)
})
})
.await?;
});

poise::CreateReply::default()
.content("message 2")
.embed(embed)
.components(components)
};

ctx.send(reply).await?;
Ok(())
}

Expand Down
16 changes: 11 additions & 5 deletions src/builtins/help.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Contains the built-in help command and surrounding infrastructure
use crate::serenity_prelude as serenity;
use crate::{serenity_prelude as serenity, CreateReply};
use std::fmt::Write as _;

/// Optional configuration for how the help message from [`help()`] looks
Expand Down Expand Up @@ -225,8 +225,11 @@ async fn help_single_command<U, E>(
format!("No such command `{}`", command_name)
};

ctx.send(|b| b.content(reply).ephemeral(config.ephemeral))
.await?;
let reply = CreateReply::default()
.content(reply)
.ephemeral(config.ephemeral);

ctx.send(reply).await?;
Ok(())
}

Expand Down Expand Up @@ -350,8 +353,11 @@ async fn help_all_commands<U, E>(
config: HelpConfiguration<'_>,
) -> Result<(), serenity::Error> {
let menu = generate_all_commands(ctx, &config).await?;
ctx.send(|b| b.content(menu).ephemeral(config.ephemeral))
.await?;
let reply = CreateReply::default()
.content(menu)
.ephemeral(config.ephemeral);

ctx.send(reply).await?;
Ok(())
}

Expand Down
49 changes: 29 additions & 20 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod paginate;
#[cfg(any(feature = "chrono", feature = "time"))]
pub use paginate::*;

use crate::serenity_prelude as serenity;
use crate::{serenity_prelude as serenity, CreateReply};

/// An error handler that logs errors either via the [`log`] crate or via a Discord message. Set
/// up a logger (e.g. `env_logger::init()`) to see the logged errors from this method.
Expand Down Expand Up @@ -59,19 +59,19 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
"You must specify one of the following subcommands: {}",
subcommands.join(", ")
);
ctx.send(|b| b.content(response).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(response).ephemeral(true))
.await?;
}
crate::FrameworkError::CommandPanic { ctx, payload: _ } => {
// Not showing the payload to the user because it may contain sensitive info
ctx.send(|b| {
b.embed(|b| {
b.title("Internal error")
.color((255, 0, 0))
.description("An unexpected internal error has occurred.")
})
.ephemeral(true)
})
.await?;
let mut embed = serenity::CreateEmbed::default();
embed
.title("Internal error")
.color((255, 0, 0))
.description("An unexpected internal error has occurred.");

ctx.send(CreateReply::default().embed(embed).ephemeral(true))
.await?;
}
crate::FrameworkError::ArgumentParse { ctx, input, error } => {
// If we caught an argument parse error, give a helpful error message with the
Expand Down Expand Up @@ -113,7 +113,8 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
"You're too fast. Please wait {} seconds before retrying",
remaining_cooldown.as_secs()
);
ctx.send(|b| b.content(msg).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(msg).ephemeral(true))
.await?;
}
crate::FrameworkError::MissingBotPermissions {
missing_permissions,
Expand All @@ -123,7 +124,8 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
"Command cannot be executed because the bot is lacking permissions: {}",
missing_permissions,
);
ctx.send(|b| b.content(msg).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(msg).ephemeral(true))
.await?;
}
crate::FrameworkError::MissingUserPermissions {
missing_permissions,
Expand All @@ -143,23 +145,28 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
ctx.command().name,
)
};
ctx.send(|b| b.content(response).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(response).ephemeral(true))
.await?;
}
crate::FrameworkError::NotAnOwner { ctx } => {
let response = "Only bot owners can call this command";
ctx.send(|b| b.content(response).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(response).ephemeral(true))
.await?;
}
crate::FrameworkError::GuildOnly { ctx } => {
let response = "You cannot run this command in DMs.";
ctx.send(|b| b.content(response).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(response).ephemeral(true))
.await?;
}
crate::FrameworkError::DmOnly { ctx } => {
let response = "You cannot run this command outside DMs.";
ctx.send(|b| b.content(response).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(response).ephemeral(true))
.await?;
}
crate::FrameworkError::NsfwOnly { ctx } => {
let response = "You cannot run this command outside NSFW channels.";
ctx.send(|b| b.content(response).ephemeral(true)).await?;
ctx.send(CreateReply::default().content(response).ephemeral(true))
.await?;
}
crate::FrameworkError::DynamicPrefix { error, msg, .. } => {
log::error!(
Expand Down Expand Up @@ -292,8 +299,10 @@ pub async fn servers<U, E>(ctx: crate::Context<'_, U, E>) -> Result<(), serenity
}

// If we show sensitive data (private guilds), it mustn't be made public, so it's ephemeral
ctx.send(|b| b.content(response).ephemeral(show_private_guilds))
.await?;
let reply = CreateReply::default()
.content(response)
.ephemeral(show_private_guilds);

ctx.send(reply).await?;
Ok(())
}
28 changes: 17 additions & 11 deletions src/builtins/paginate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,25 @@ pub async fn paginate<U, E>(
let next_button_id = format!("{}next", ctx_id);

// Send the embed with the first page as content
let mut current_page = 0;
ctx.send(|b| {
b.embed(|b| b.description(pages[current_page]))
.components(|b| {
b.create_action_row(|b| {
b.create_button(|b| b.custom_id(&prev_button_id).emoji('◀'))
.create_button(|b| b.custom_id(&next_button_id).emoji('▶'))
})
})
})
.await?;
let reply = {
let mut embed = serenity::CreateEmbed::default();
embed.description(pages[0]);

let mut components = serenity::CreateComponents::default();
components.create_action_row(|b| {
b.create_button(|b| b.custom_id(&prev_button_id).emoji('◀'))
.create_button(|b| b.custom_id(&next_button_id).emoji('▶'))
});

crate::CreateReply::default()
.embed(embed)
.components(components)
};

ctx.send(reply).await?;

// Loop through incoming interactions with the navigation buttons
let mut current_page = 0;
while let Some(press) = serenity::CollectComponentInteraction::new(ctx)
// We defined our button IDs to start with `ctx_id`. If they don't, some other command's
// button was pressed
Expand Down
Loading

0 comments on commit c33ebdc

Please sign in to comment.