Skip to content

Commit

Permalink
Implement response deferrals, Updated dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fdnt7 committed Sep 7, 2024
1 parent 98a138c commit a9f8320
Show file tree
Hide file tree
Showing 18 changed files with 524 additions and 356 deletions.
350 changes: 156 additions & 194 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions flake.lock

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

12 changes: 6 additions & 6 deletions lyra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ lyra_ext = { path = "../lyra_ext" }

paste = "1.0.15"
const-str = "0.5.7"
const_panic = { version = "0.2.8", features = ["derive"] }
const_panic = { version = "0.2.9", features = ["derive"] }
bitflags = "2.6.0"
dashmap = "6.0.1"
dashmap = "6.1.0"
dotenvy = "0.15.7"
dotenvy_macro = "0.15.7"
thiserror = "1.0.63"
color-eyre = "0.6.3"
futures = "0.3.30"
tokio = { version = "1.39.3", features = [
tokio = { version = "1.40.0", features = [
"sync",
"signal",
"rt-multi-thread",
"macros",
] }
serde = "1.0.208"
serde_json = "1.0.125"
serde = "1.0.210"
serde_json = "1.0.128"
regex = "1.10.6"
linkify = "0.10.0"
fuzzy-matcher = "0.3.7"
Expand All @@ -62,7 +62,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
rand = "0.8.5"
itertools = "0.13.0"
rayon = "1.10.0"
sqlx = { version = "0.8.0", features = ["postgres", "runtime-tokio-rustls"] }
sqlx = { version = "0.8.2", features = ["postgres", "runtime-tokio-rustls"] }
mixbox = "2.0.0"
lavalink-rs = { version = "0.13.0", features = ["twilight16"] }
aho-corasick = "1.1.3"
Expand Down
137 changes: 64 additions & 73 deletions lyra/src/command/macros.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
macro_rules! out {
($cnt: expr, $ctx: expr) => {
use crate::core::model::AcknowledgementAware;
$ctx.respond($cnt).await?;
return Ok(());
};
($cnt: expr, ?$ctx: expr) => {
use crate::core::model::AcknowledgementAware;
$ctx.respond($cnt).await?;
};
}
Expand All @@ -20,37 +22,25 @@ macro_rules! out {

macro_rules! out_or_fol {
($cnt: expr, $ctx: expr) => {
if $ctx.acknowledged() {
$ctx.followup(&$cnt).await?;
return Ok(());
}
$ctx.respond($cnt).await?;
use crate::core::model::AcknowledgementAware;
$ctx.respond_or_followup($cnt).await?;
return Ok(());
};
($cnt: expr, ?$ctx: expr) => {
if $ctx.acknowledged() {
$ctx.followup(&$cnt).await?;
} else {
$ctx.respond($cnt).await?;
}
use crate::core::model::AcknowledgementAware;
$ctx.respond_or_followup($cnt).await?;
};
}

macro_rules! out_or_upd {
($cnt: expr, $ctx: expr) => {
if $ctx.acknowledged() {
$ctx.update_no_components_embeds(&$cnt).await?;
return Ok(());
}
$ctx.respond($cnt).await?;
use crate::core::model::AcknowledgementAware;
$ctx.respond_or_update($cnt).await?;
return Ok(());
};
($cnt: expr, ?$ctx: expr) => {
if $ctx.acknowledged() {
$ctx.update_no_components_embeds(&$cnt).await?;
} else {
$ctx.respond($cnt).await?;
}
use crate::core::model::AcknowledgementAware;
$ctx.respond_or_update($cnt).await?;
};
}

Expand All @@ -66,42 +56,43 @@ macro_rules! out_upd {

macro_rules! hid {
($cnt: expr, $ctx: expr) => {
$ctx.ephem($cnt).await?;
#[allow(unused_imports)]
use crate::core::model::AcknowledgementAware;
$ctx.respond_ephemeral($cnt).await?;
return Ok(());
};
($cnt: expr, ?$ctx: expr) => {
$ctx.ephem($cnt).await?;
#[allow(unused_imports)]
use crate::core::model::AcknowledgementAware;
$ctx.respond_ephemeral($cnt).await?;
};
}

macro_rules! hid_fol {
($cnt: expr, $ctx: expr) => {
$ctx.followup_ephem(&$cnt).await?;
#[allow(unused_imports)]
use crate::core::model::AcknowledgementAware;
$ctx.followup_ephemeral($cnt).await?;
return Ok(());
};
($cnt: expr, ?$ctx: expr) => {{
use crate::core::model::AcknowledgementAware;
$ctx.followup_ephemeral($cnt).await?
}};
}

macro_rules! hid_or_fol {
($cnt: expr, $ctx: expr) => {
use crate::core::model::AcknowledgementAware;
$ctx.respond_ephemeral_or_followup($cnt).await?;
return Ok(());
};
($cnt: expr, ?$ctx: expr) => {
$ctx.followup_ephem(&$cnt).await?
use crate::core::model::AcknowledgementAware;
$ctx.respond_ephemeral_or_followup($cnt).await?;
};
}

// macro_rules! hid_or_fol {
// ($cnt: expr, $ctx: expr) => {
// if $ctx.acknowledged() {
// $ctx.followup_ephem(&$cnt).await?;
// return Ok(());
// }
// $ctx.ephem($cnt).await?;
// return Ok(());
// };
// ($cnt: expr, ?$ctx: expr) => {
// if $ctx.acknowledged() {
// $ctx.followup_ephem(&$cnt).await?;
// } else {
// $ctx.ephem($cnt).await?;
// }
// };
// }

macro_rules! generate_hid_variants {
($($name: ident => $emoji: ident),+$(,)?) => {
$(
Expand Down Expand Up @@ -138,30 +129,30 @@ macro_rules! generate_hid_fol_variants {
}
}

// macro_rules! generate_hid_or_fol_variants {
// ($($name: ident => $emoji: ident),+$(,)?) => {
// $(
// macro_rules! $name {
// ($cnt: expr, $ctx: expr) => {
// use crate::core::consts::exit_code;
// hid_or_fol!(format!("{} {}", exit_code::$emoji, $cnt), $ctx);
// };
// ($cnt: expr, ?$ctx: expr) => {
// use crate::core::consts::exit_code;
// hid_or_fol!(format!("{} {}", exit_code::$emoji, $cnt), ?$ctx);
// };
// }
// )+

// pub(crate) use {$($name,)+};
// }
// }
macro_rules! generate_hid_or_fol_variants {
($($name: ident => $emoji: ident),+$(,)?) => {
$(
macro_rules! $name {
($cnt: expr, $ctx: expr) => {
use crate::core::r#const::exit_code;
crate::command::macros::hid_or_fol!(format!("{} {}", exit_code::$emoji, $cnt), $ctx);
};
($cnt: expr, ?$ctx: expr) => {
use crate::core::r#const::exit_code;
crate::command::macros::hid_or_fol!(format!("{} {}", exit_code::$emoji, $cnt), ?$ctx);
};
}
)+

pub(crate) use {$($name,)+};
}
}

generate_hid_variants! {
note => NOTICE,
sus => DUBIOUS,
caut => WARNING,
what => NOT_FOUND,
// what => NOT_FOUND,
bad => INVALID,
nope => PROHIBITED,
cant => FORBIDDEN,
Expand All @@ -181,16 +172,16 @@ generate_hid_fol_variants! {
// crit_fol => UNKNOWN_ERROR
}

// generate_hid_or_fol_variants! {
// note_or_fol => NOTICE,
// dub_or_fol => DUBIOUS,
// caut_or_fol => WARNING,
// miss_or_fol => NOT_FOUND,
// bad_or_fol => INVALID,
// nope_or_fol => PROHIBITED,
// cant_or_fol => FORBIDDEN,
// err_or_fol => KNOWN_ERROR,
// crit_or_fol => UNKNOWN_ERROR
// }
generate_hid_or_fol_variants! {
// note_or_fol => NOTICE,
// dub_or_fol => DUBIOUS,
// caut_or_fol => WARNING,
what_or_fol => NOT_FOUND,
bad_or_fol => INVALID,
nope_or_fol => PROHIBITED,
cant_or_fol => FORBIDDEN,
// err_or_fol => KNOWN_ERROR,
crit_or_fol => UNKNOWN_ERROR
}

pub(crate) use {hid, hid_fol, out, out_or_fol, out_or_upd, out_upd};
pub(crate) use {hid, hid_fol, hid_or_fol, out, out_or_fol, out_or_upd, out_upd};
11 changes: 7 additions & 4 deletions lyra/src/command/model/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod modal;

use std::{marker::PhantomData, sync::Arc};

use tokio::sync::oneshot;
use twilight_cache_inmemory::{model::CachedMember, InMemoryCache, Reference};
use twilight_gateway::{Latency, MessageSender};
use twilight_http::Client as HttpClient;
Expand Down Expand Up @@ -88,6 +89,7 @@ where
sender: MessageSender,
data: Option<PartialInteractionData>,
acknowledged: bool,
acknowledgement: Option<oneshot::Sender<()>>,
kind: PhantomData<fn(Of) -> Of>,
location: PhantomData<fn(In) -> In>,
}
Expand All @@ -104,6 +106,7 @@ impl<T: Kind> TryFrom<Ctx<T>> for Ctx<T, GuildMarker> {
sender: value.sender,
data: value.data,
acknowledged: value.acknowledged,
acknowledgement: value.acknowledgement,
kind: value.kind,
location: PhantomData::<fn(GuildMarker) -> GuildMarker>,
})
Expand All @@ -120,6 +123,7 @@ impl<T: Kind, U: Location> Ctx<T, U> {
location: self.location,
data: None,
acknowledged: false,
acknowledgement: None,
kind: PhantomData::<fn(ModalMarker) -> ModalMarker>,
}
}
Expand All @@ -128,12 +132,11 @@ impl<T: Kind, U: Location> Ctx<T, U> {
&self.latency
}

pub const fn acknowledged(&self) -> bool {
self.acknowledged
}

pub fn acknowledge(&mut self) {
self.acknowledged = true;
if let Some(tx) = std::mem::take(&mut self.acknowledgement) {
let _ = tx.send(());
}
}

pub fn db(&self) -> &sqlx::Pool<sqlx::Postgres> {
Expand Down
3 changes: 3 additions & 0 deletions lyra/src/command/model/ctx/command_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{hint::unreachable_unchecked, marker::PhantomData, sync::Arc};

use tokio::sync::oneshot;
use twilight_gateway::{Latency, MessageSender};
use twilight_model::{
application::interaction::application_command::{
Expand All @@ -26,6 +27,7 @@ impl<T: Aware> Ctx<T> {
bot: OwnedBotState,
latency: Latency,
sender: MessageSender,
acknowledgement: oneshot::Sender<()>,
) -> Self {
Self {
data: Some(PartialInteractionData::Command(PartialCommandData::new(
Expand All @@ -36,6 +38,7 @@ impl<T: Aware> Ctx<T> {
latency,
sender,
acknowledged: false,
acknowledgement: Some(acknowledgement),
kind: PhantomData::<fn(T) -> T>,
location: PhantomData,
}
Expand Down
Loading

0 comments on commit a9f8320

Please sign in to comment.