Wip commit

This commit is contained in:
jude
2024-01-06 19:48:17 +00:00
parent cce0de7c75
commit e4e9af2bb4
37 changed files with 1051 additions and 1366 deletions

View File

@ -8,15 +8,8 @@ use log::warn;
use poise::{
serenity_prelude as serenity,
serenity_prelude::{
builder::CreateEmbed,
model::{
application::interaction::{
message_component::MessageComponentInteraction, InteractionResponseType,
MessageFlags,
},
channel::Channel,
},
Context,
builder::CreateEmbed, ComponentInteraction, ComponentInteractionDataKind, Context,
CreateEmbedFooter, CreateInteractionResponse, CreateInteractionResponseMessage,
},
};
use rmp_serde::Serializer;
@ -31,7 +24,7 @@ use crate::{
component_models::pager::{DelPager, LookPager, MacroPager, Pager, TodoPager},
consts::{EMBED_DESCRIPTION_MAX_LENGTH, THEME_COLOR},
models::reminder::Reminder,
utils::send_as_initial_response,
utils::reply_to_interaction_response_message,
Data,
};
@ -64,21 +57,23 @@ impl ComponentDataModel {
rmp_serde::from_read(cur).unwrap()
}
pub async fn act(&self, ctx: &Context, data: &Data, component: &MessageComponentInteraction) {
pub async fn act(&self, ctx: &Context, data: &Data, component: &ComponentInteraction) {
match self {
ComponentDataModel::LookPager(pager) => {
let flags = pager.flags;
let channel_opt = component.channel_id.to_channel_cached(&ctx);
let channel_id = {
let channel_opt = component.channel_id.to_channel_cached(&ctx.cache);
let channel_id = if let Some(Channel::Guild(channel)) = channel_opt {
if Some(channel.guild_id) == component.guild_id {
flags.channel_id.unwrap_or(component.channel_id)
if let Some(channel) = channel_opt {
if Some(channel.guild_id) == component.guild_id {
flags.channel_id.unwrap_or(component.channel_id)
} else {
component.channel_id
}
} else {
component.channel_id
}
} else {
component.channel_id
};
let reminders = Reminder::from_channel(&data.database, channel_id, &flags).await;
@ -90,11 +85,7 @@ impl ComponentDataModel {
.div_ceil(EMBED_DESCRIPTION_MAX_LENGTH);
let channel_name =
if let Some(Channel::Guild(channel)) = channel_id.to_channel_cached(&ctx) {
Some(channel.name)
} else {
None
};
channel_id.to_channel_cached(&ctx.cache).map(|channel| channel.name.clone());
let next_page = pager.next_page(pages);
@ -107,7 +98,7 @@ impl ComponentDataModel {
.skip_while(|p| {
skip_char_count += p.len();
skip_char_count < EMBED_DESCRIPTION_MAX_LENGTH * next_page as usize
skip_char_count < EMBED_DESCRIPTION_MAX_LENGTH * next_page
})
.take_while(|p| {
char_count += p.len();
@ -117,28 +108,24 @@ impl ComponentDataModel {
.collect::<Vec<String>>()
.join("");
let mut embed = CreateEmbed::default();
embed
let embed = CreateEmbed::default()
.title(format!(
"Reminders{}",
channel_name.map_or(String::new(), |n| format!(" on #{}", n))
))
.description(display)
.footer(|f| f.text(format!("Page {} of {}", next_page + 1, pages)))
.footer(CreateEmbedFooter::new(format!("Page {} of {}", next_page + 1, pages)))
.color(*THEME_COLOR);
let _ = component
.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::UpdateMessage).interaction_response_data(
|response| {
response.set_embeds(vec![embed]).components(|comp| {
pager.create_button_row(pages, comp);
comp
})
},
)
})
.create_response(
&ctx,
CreateInteractionResponse::UpdateMessage(
CreateInteractionResponseMessage::new()
.embed(embed)
.components(vec![pager.create_button_row(pages)]),
),
)
.await;
}
ComponentDataModel::DelPager(pager) => {
@ -155,55 +142,58 @@ impl ComponentDataModel {
let resp = show_delete_page(&reminders, pager.next_page(max_pages), pager.timezone);
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::UpdateMessage).interaction_response_data(
|d| {
send_as_initial_response(resp, d);
d
},
)
})
.create_response(
&ctx,
CreateInteractionResponse::UpdateMessage(
reply_to_interaction_response_message(resp),
),
)
.await;
}
ComponentDataModel::DelSelector(selector) => {
let selected_id = component.data.values.join(",");
if let ComponentInteractionDataKind::StringSelect { values } = &component.data.kind
{
let selected_id = values.join(",");
sqlx::query!(
"UPDATE reminders SET `status` = 'deleted' WHERE FIND_IN_SET(id, ?)",
selected_id
)
.execute(&data.database)
.await
.unwrap();
sqlx::query!(
"
UPDATE reminders SET `status` = 'deleted' WHERE FIND_IN_SET(id, ?)
",
selected_id
)
.execute(&data.database)
.await
.unwrap();
let reminders = Reminder::from_guild(
&ctx,
&data.database,
component.guild_id,
component.user.id,
)
.await;
let resp = show_delete_page(&reminders, selector.page, selector.timezone);
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::UpdateMessage).interaction_response_data(
|d| {
send_as_initial_response(resp, d);
d
},
)
})
let reminders = Reminder::from_guild(
&ctx,
&data.database,
component.guild_id,
component.user.id,
)
.await;
let resp = show_delete_page(&reminders, selector.page, selector.timezone);
let _ = component
.create_response(
&ctx,
CreateInteractionResponse::UpdateMessage(
reply_to_interaction_response_message(resp),
),
)
.await;
}
}
ComponentDataModel::TodoPager(pager) => {
if Some(component.user.id.0) == pager.user_id || pager.user_id.is_none() {
if Some(component.user.id.get()) == pager.user_id || pager.user_id.is_none() {
let values = if let Some(uid) = pager.user_id {
sqlx::query!(
"SELECT todos.id, value FROM todos
INNER JOIN users ON todos.user_id = users.id
WHERE users.user = ?",
"
SELECT todos.id, value FROM todos
INNER JOIN users ON todos.user_id = users.id
WHERE users.user = ?
",
uid,
)
.fetch_all(&data.database)
@ -214,9 +204,11 @@ WHERE users.user = ?",
.collect::<Vec<(usize, String)>>()
} else if let Some(cid) = pager.channel_id {
sqlx::query!(
"SELECT todos.id, value FROM todos
INNER JOIN channels ON todos.channel_id = channels.id
WHERE channels.channel = ?",
"
SELECT todos.id, value FROM todos
INNER JOIN channels ON todos.channel_id = channels.id
WHERE channels.channel = ?
",
cid,
)
.fetch_all(&data.database)
@ -227,9 +219,11 @@ WHERE channels.channel = ?",
.collect::<Vec<(usize, String)>>()
} else {
sqlx::query!(
"SELECT todos.id, value FROM todos
INNER JOIN guilds ON todos.guild_id = guilds.id
WHERE guilds.guild = ?",
"
SELECT todos.id, value FROM todos
INNER JOIN guilds ON todos.guild_id = guilds.id
WHERE guilds.guild = ?
",
pager.guild_id,
)
.fetch_all(&data.database)
@ -251,79 +245,86 @@ WHERE guilds.guild = ?",
);
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::UpdateMessage)
.interaction_response_data(|d| {
send_as_initial_response(resp, d);
d
})
})
.create_response(
&ctx,
CreateInteractionResponse::UpdateMessage(
reply_to_interaction_response_message(resp),
),
)
.await;
} else {
let _ = component
.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|d| {
d.flags(
MessageFlags::EPHEMERAL,
)
.create_response(
&ctx,
CreateInteractionResponse::Message(
CreateInteractionResponseMessage::new()
.ephemeral(true)
.content("Only the user who performed the command can use these components")
})
})
)
)
.await;
}
}
ComponentDataModel::TodoSelector(selector) => {
if Some(component.user.id.0) == selector.user_id || selector.user_id.is_none() {
let selected_id = component.data.values.join(",");
if Some(component.user.id.get()) == selector.user_id || selector.user_id.is_none() {
if let ComponentInteractionDataKind::StringSelect { values } =
&component.data.kind
{
let selected_id = values.join(",");
sqlx::query!("DELETE FROM todos WHERE FIND_IN_SET(id, ?)", selected_id)
sqlx::query!(
"
DELETE FROM todos WHERE FIND_IN_SET(id, ?)
",
selected_id
)
.execute(&data.database)
.await
.unwrap();
let values = sqlx::query!(
// fucking braindead mysql use <=> instead of = for null comparison
"SELECT id, value FROM todos WHERE user_id <=> ? AND channel_id <=> ? AND guild_id <=> ?",
selector.user_id,
selector.channel_id,
selector.guild_id,
)
.fetch_all(&data.database)
.await
.unwrap()
.iter()
.map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>();
let values = sqlx::query!(
// fucking braindead mysql use <=> instead of = for null comparison
"
SELECT id, value FROM todos WHERE user_id <=> ? AND channel_id <=> ? AND guild_id <=> ?
",
selector.user_id,
selector.channel_id,
selector.guild_id,
)
.fetch_all(&data.database)
.await
.unwrap()
.iter()
.map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>();
let resp = show_todo_page(
&values,
selector.page,
selector.user_id,
selector.channel_id,
selector.guild_id,
);
let resp = show_todo_page(
&values,
selector.page,
selector.user_id,
selector.channel_id,
selector.guild_id,
);
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::UpdateMessage)
.interaction_response_data(|d| {
send_as_initial_response(resp, d);
d
})
})
.await;
let _ = component
.create_response(
&ctx,
CreateInteractionResponse::UpdateMessage(
reply_to_interaction_response_message(resp),
),
)
.await;
}
} else {
let _ = component
.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|d| {
d.flags(
MessageFlags::EPHEMERAL,
)
.create_response(
&ctx,
CreateInteractionResponse::Message(
CreateInteractionResponseMessage::new()
.ephemeral(true)
.content("Only the user who performed the command can use these components")
})
})
)
)
.await;
}
}
@ -336,14 +337,12 @@ WHERE guilds.guild = ?",
let resp = show_macro_page(&macros, page);
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::UpdateMessage).interaction_response_data(
|d| {
send_as_initial_response(resp, d);
d
},
)
})
.create_response(
&ctx,
CreateInteractionResponse::UpdateMessage(
reply_to_interaction_response_message(resp),
),
)
.await;
}
ComponentDataModel::UndoReminder(undo_reminder) => {
@ -355,58 +354,56 @@ WHERE guilds.guild = ?",
match reminder.delete(&data.database).await {
Ok(()) => {
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::UpdateMessage)
.interaction_response_data(|d| {
d.embed(|e| {
e.title("Reminder Canceled")
.description(
"This reminder has been canceled.",
)
.color(*THEME_COLOR)
})
.components(|c| c)
})
})
.create_response(
&ctx,
CreateInteractionResponse::UpdateMessage(
CreateInteractionResponseMessage::new().embed(
CreateEmbed::new()
.title("Reminder Canceled")
.description("This reminder has been canceled.")
.color(*THEME_COLOR),
),
),
)
.await;
}
Err(e) => {
warn!("Error canceling reminder: {:?}", e);
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|d| {
d.content(
"The reminder could not be canceled: it may have already been deleted. Check `/del`!")
.ephemeral(true)
})
})
.create_response(
&ctx,
CreateInteractionResponse::Message(
CreateInteractionResponseMessage::new().content(
"An error occurred trying to cancel this reminder.",
).ephemeral(true),
),
)
.await;
}
}
} else {
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|d| {
d.content(
"The reminder could not be canceled: it may have already been deleted. Check `/del`!")
.ephemeral(true)
})
})
.create_response(
&ctx,
CreateInteractionResponse::Message(
CreateInteractionResponseMessage::new().content(
"The reminder could not be canceled. It may have already been deleted.",
).ephemeral(true),
),
)
.await;
}
} else {
let _ = component
.create_interaction_response(&ctx, |f| {
f.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|d| {
d.content(
"Only the user who performed the command can use this button.")
.ephemeral(true)
})
})
.create_response(
&ctx,
CreateInteractionResponse::Message(
CreateInteractionResponseMessage::new()
.content("Only the user who performed the command can use these components")
.ephemeral(true),
),
)
.await;
}
}