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
@@ -0,0 +1,9 @@
|
||||
ALTER TABLE sounds
|
||||
ADD INDEX sounds_name (name),
|
||||
ADD INDEX sounds_server_id (server_id),
|
||||
ADD INDEX sounds_uploader_id (uploader_id),
|
||||
ALGORITHM=INPLACE, LOCK=NONE;
|
||||
|
||||
ALTER TABLE join_sounds
|
||||
ADD INDEX join_sounds_user_guild (`user`, guild),
|
||||
ALGORITHM=INPLACE, LOCK=NONE;
|
||||
+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?;
|
||||
|
||||
|
||||
+10
-2
@@ -94,8 +94,16 @@ pub async fn listener(ctx: &Context, event: &FullEvent, data: &Data) -> Result<(
|
||||
FullEvent::InteractionCreate { interaction } => match interaction {
|
||||
Interaction::Component(component) => {
|
||||
if let Some(guild_id) = component.guild_id {
|
||||
if let Ok(()) = SoundPager::handle_interaction(ctx, &data, component).await {
|
||||
} else {
|
||||
let handled = match SoundPager::handle_interaction(ctx, &data, component).await
|
||||
{
|
||||
Ok(handled) => handled,
|
||||
Err(e) => {
|
||||
log::warn!("Failed to handle pager interaction: {:?}", e);
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
if !handled {
|
||||
component.defer(&ctx).await.unwrap();
|
||||
let mode = component.data.custom_id.as_str();
|
||||
match mode {
|
||||
|
||||
+3
-3
@@ -253,7 +253,7 @@ impl SoundCtx for Data {
|
||||
LIMIT ?, ?",
|
||||
user_id.into(),
|
||||
page * 25,
|
||||
(page + 1) * 25
|
||||
25u64
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
@@ -294,7 +294,7 @@ impl SoundCtx for Data {
|
||||
LIMIT ?, ?",
|
||||
user_id.into(),
|
||||
page * 25,
|
||||
(page + 1) * 25
|
||||
25u64
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
@@ -335,7 +335,7 @@ impl SoundCtx for Data {
|
||||
LIMIT ?, ?",
|
||||
guild_id.into(),
|
||||
page * 25,
|
||||
(page + 1) * 25
|
||||
25u64
|
||||
)
|
||||
.fetch_all(&self.database)
|
||||
.await?
|
||||
|
||||
Reference in new issue
Block a user