Fix slow queries and pagers
This commit is contained in:
1 parent
ee36958b39
commit
ed87cb6018
4 files changed
+41
-13
No files matched your search
+19
-8
@@ -2,7 +2,8 @@ use poise::{
|
||||
serenity_prelude,
|
||||
serenity_prelude::{
|
||||
constants::MESSAGE_CODE_LIMIT, ButtonStyle, ComponentInteraction, CreateActionRow,
|
||||
CreateButton, CreateEmbed, EditInteractionResponse, GuildId, UserId,
|
||||
CreateButton, CreateEmbed, CreateInteractionResponse, EditInteractionResponse, GuildId,
|
||||
UserId,
|
||||
},
|
||||
CreateReply,
|
||||
};
|
||||
@@ -192,15 +193,25 @@ impl SoundPager {
|
||||
}))
|
||||
}
|
||||
|
||||
/// Handle a pager button click. Returns `Ok(false)` if the interaction is not a pager button,
|
||||
/// so the caller can route it elsewhere. Errors mean it was a pager button that failed.
|
||||
pub async fn handle_interaction(
|
||||
ctx: &serenity_prelude::Context,
|
||||
data: &Data,
|
||||
interaction: &ComponentInteraction,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<bool, Error> {
|
||||
let Ok(pager) = serde_json::from_str::<Self>(&interaction.data.custom_id) else {
|
||||
return Ok(false);
|
||||
};
|
||||
|
||||
let user_id = interaction.user.id;
|
||||
let guild_id = interaction.guild_id.unwrap();
|
||||
|
||||
let pager = serde_json::from_str::<Self>(&interaction.data.custom_id)?;
|
||||
// Acknowledge straight away so the DB queries below can't exceed Discord's 3s deadline
|
||||
interaction
|
||||
.create_response(&ctx, CreateInteractionResponse::Acknowledge)
|
||||
.await?;
|
||||
|
||||
let sounds = pager.get_page(data, user_id, guild_id).await?;
|
||||
let count = match pager.context {
|
||||
ListContext::User => data.count_user_sounds(user_id).await?,
|
||||
@@ -211,13 +222,13 @@ impl SoundPager {
|
||||
interaction
|
||||
.edit_response(
|
||||
&ctx,
|
||||
EditInteractionResponse::default()
|
||||
.add_embed(pager.embed(&sounds, count))
|
||||
.components(vec![pager.create_action_row(count / 25)]),
|
||||
EditInteractionResponse::new()
|
||||
.embed(pager.embed(&sounds, count))
|
||||
.components(vec![pager.create_action_row(count.saturating_sub(1) / 25)]),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn reply(&self, ctx: Context<'_>) -> Result<(), Error> {
|
||||
@@ -238,7 +249,7 @@ impl SoundPager {
|
||||
CreateReply::default()
|
||||
.ephemeral(true)
|
||||
.embed(self.embed(&sounds, count))
|
||||
.components(vec![self.create_action_row(count / 25)]),
|
||||
.components(vec![self.create_action_row(count.saturating_sub(1) / 25)]),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in new issue
Block a user