Skip to content

Commit

Permalink
🌆 Update to twilight =0.16.0-rc.1 (#14)
Browse files Browse the repository at this point in the history
* Update to `twilight` `=0.16.0-rc.1`

* Fixed deprioritised partial move error

* Update to use custom fork of `twilight-interactions`, rev `82b707f` & benefit from `bitflags` 2.x.x

* Update to `lyra` `0.5.0`

* Improved `.expect(...)` and `panic!(...)` messages & removed dead code
  • Loading branch information
fdnt7 authored Apr 10, 2024
1 parent 1c9a8bb commit d102e0c
Show file tree
Hide file tree
Showing 35 changed files with 452 additions and 416 deletions.
424 changes: 250 additions & 174 deletions lyra/Cargo.lock

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions lyra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "lyra"
readme = "../README.md"
description = "A featureful, self-hostable Discord music bot."
version = "0.4.0"
version = "0.5.0"
edition = "2021"
license = "GPL-3.0"
repository = "https://github.com/lyra-music/lyra"
Expand Down Expand Up @@ -48,7 +48,7 @@ dotenvy = "0.15.7"
dotenvy_macro = "0.15.7"
thiserror = "1.0.58"
color-eyre = "0.6.3"
rstest = "0.18.2"
rstest = "0.19.0"
futures = "0.3.30"
tokio = { version = "1.37.0", features = [
"sync",
Expand All @@ -69,24 +69,24 @@ lazy_static = "1.4.0"
chrono = "0.4.37"
sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio-rustls"] }
mixbox = "2.0.0"
lavalink-rs = { version = "0.11.0", features = ["twilight"] }
lavalink-rs = { version = "0.11.0", features = ["twilight16"] }
aho-corasick = "1.1.3"

twilight = "0.15.1"
twilight-cache-inmemory = { version = "0.15.4", features = [
twilight = "=0.16.0-rc.1"
twilight-cache-inmemory = { version = "=0.16.0-rc.1", features = [
"permission-calculator",
] }
twilight-gateway = "0.15.4"
twilight-http = "0.15.4"
twilight-model = "0.15.4"
twilight-standby = "0.15.4"
twilight-validate = "0.15.3"
twilight-mention = "0.15.3"
twilight-util = { version = "0.15.4", features = [
twilight-gateway = "=0.16.0-rc.1"
twilight-http = "=0.16.0-rc.1"
twilight-model = "=0.16.0-rc.1"
twilight-standby = "=0.16.0-rc.1"
twilight-validate = "=0.16.0-rc.1"
twilight-mention = "=0.16.0-rc.1"
twilight-util = { version = "=0.16.0-rc.1", features = [
"permission-calculator",
"builder",
] }
twilight-interactions = "0.15.2"
twilight-interactions = { git = "https://github.com/fdnt7/twilight-interactions", rev = "82b707f" }

[dependencies.kmeans_colors]
version = "0.6.0"
Expand Down
2 changes: 1 addition & 1 deletion lyra/lyra_proc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ unwrap_used = "deny"

[dependencies]
syn = "2.0.58"
quote = "1.0.35"
quote = "1.0.36"
itertools = "0.12.1"
heck = "0.5.0"
10 changes: 4 additions & 6 deletions lyra/src/bot/command/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ pub async fn user_allowed_in(ctx: &Ctx<impl CtxKind>) -> Result<(), check::UserA
ChannelType::PublicThread
| ChannelType::PrivateThread
| ChannelType::AnnouncementThread => {
let parent_id = channel
.parent_id
.expect("threads must have a parent channel");
let parent_id = channel.parent_id.expect("threads always have a parent id");
access_calculator_builder = access_calculator_builder
.thread(channel.id)
.text_channel(parent_id);
Expand Down Expand Up @@ -201,7 +199,7 @@ fn someone_else_in(channel_id: Id<ChannelMarker>, ctx: &Ctx<impl CtxKind>) -> Op
states.any(|v| {
!ctx.cache()
.user(v.user_id())
.expect("user of `v.user_id()` must exist in the cache")
.expect("user should be in the cache")
.bot
&& v.user_id() != ctx.author_id()
})
Expand Down Expand Up @@ -336,7 +334,7 @@ async fn currently_playing(ctx: &Ctx<impl CtxKind>) -> Result<CurrentlyPlaying,
let (current, index) = queue.current_and_index().ok_or(NotPlayingError)?;

let requester = current.requester();
let position = NonZeroUsize::new(index + 1).expect("`index + 1` must be nonzero");
let position = NonZeroUsize::new(index + 1).expect("index + 1 is non-zero");
let title = current.track().info.corrected_title().into();
let channel_id = ctx.lavalink().connection(guild_id).channel_id;

Expand Down Expand Up @@ -837,7 +835,7 @@ async fn send_superseded_win_notice(
.await?
.create_followup(&interaction_token)
.flags(MessageFlags::EPHEMERAL)
.content("🪄 The poll was superseded to win by a DJ.")?
.content("🪄 The poll was superseded to win by a DJ.")
.await?;

Ok(())
Expand Down
16 changes: 8 additions & 8 deletions lyra/src/bot/command/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ macro_rules! declare_slash_commands {
fn name() -> &'static str {
let cmd_name = stringify!($raw_cmd);
&SLASH_COMMANDS_MAP.get(cmd_name)
.unwrap_or_else(|| panic!("command {} must exist", cmd_name))
.unwrap_or_else(|| panic!("command not found: {}", cmd_name))
.name
}
}
)*

impl SlashCtx {
pub async fn execute(mut self, data: CommandData) -> Result<(), CommandExecuteError> {
pub async fn execute(self, data: CommandData) -> Result<(), CommandExecuteError> {
check::user_allowed_in(&self).await?;

match data.name {
Expand All @@ -58,7 +58,7 @@ macro_rules! declare_slash_commands {
}
)*
_ => {
let cmd_data = self.take_partial_command_data().expect("`self.data` must exist");
let cmd_data = self.into_partial_command_data();
return Err(CommandExecuteError::UnknownCommand(cmd_data))
}
}
Expand All @@ -84,14 +84,14 @@ macro_rules! declare_message_commands {
fn name() -> &'static str {
let cmd_name = stringify!($raw_cmd);
&MESSAGE_COMMANDS_MAP.get(cmd_name)
.unwrap_or_else(|| panic!("command {} must exist", cmd_name))
.unwrap_or_else(|| panic!("command not found: {}", cmd_name))
.name
}
}
)*

impl MessageCtx {
pub async fn execute(mut self, data: CommandData) -> Result<(), CommandExecuteError> {
pub async fn execute(self, data: CommandData) -> Result<(), CommandExecuteError> {
check::user_allowed_in(&self).await?;

match data.name {
Expand All @@ -101,7 +101,7 @@ macro_rules! declare_message_commands {
}
)*
_ => {
let cmd_data = self.take_partial_command_data().expect("`self.data` must exist");
let cmd_data = self.into_partial_command_data();
return Err(CommandExecuteError::UnknownCommand(cmd_data))
}
}
Expand All @@ -114,15 +114,15 @@ macro_rules! declare_message_commands {
macro_rules! declare_autocomplete {
($ ($raw_cmd: ident => $raw_autocomplete: ident) ,* $(,)? ) => {
impl AutocompleteCtx {
pub async fn execute(mut self, data: CommandData) -> Result<(), AutocompleteExecuteError> {
pub async fn execute(self, data: CommandData) -> Result<(), AutocompleteExecuteError> {
match data.name {
$(
ref n if n == <$raw_cmd>::name() => {
return Ok(<$raw_autocomplete>::from_interaction(data.into())?.execute(self).await?);
}
)*
_ => {
let cmd_data = self.take_partial_command_data().expect("`self.data` must exist");
let cmd_data = self.into_partial_command_data();
return Err(AutocompleteExecuteError::UnknownAutocomplete(cmd_data))
}
}
Expand Down
7 changes: 4 additions & 3 deletions lyra/src/bot/command/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ mod ctx;
use std::sync::Arc;

use twilight_model::{
application::interaction::application_command::{
CommandData, CommandDataOption, CommandInteractionDataResolved,
application::interaction::{
application_command::{CommandData, CommandDataOption},
InteractionDataResolved,
},
id::{
marker::{CommandMarker, GenericMarker},
Expand All @@ -24,7 +25,7 @@ pub struct PartialCommandData {
pub id: Id<CommandMarker>,
pub name: Arc<str>,
pub target_id: Option<Id<GenericMarker>>,
pub resolved: Option<CommandInteractionDataResolved>,
pub resolved: Option<InteractionDataResolved>,
pub options: Box<[CommandDataOption]>,
}

Expand Down
29 changes: 11 additions & 18 deletions lyra/src/bot/command/model/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<T: CtxKind> Ctx<T> {
pub fn bot_member(&self) -> CachedBotMember {
self.cache()
.member(self.guild_id(), self.bot().user_id())
.expect("bot's member object must exist")
.expect("bot's member should be in cache")
}

#[inline]
Expand All @@ -122,20 +122,18 @@ impl<T: CtxKind> Ctx<T> {
self.inner
.channel
.as_ref()
.expect("`self.inner.channel` must not be `None`")
.expect("interaction type is not ping")
}

pub fn author(&self) -> &User {
self.inner
.author()
.expect("`self.inner.author()` must not be `None`")
self.inner.author().expect("interaction type is not ping")
}

pub fn member(&self) -> &PartialMember {
self.inner
.member
.as_ref()
.expect("`self.inner.member` must not be `None`")
.expect("interaction invoked in a guild")
}

#[inline]
Expand All @@ -151,18 +149,14 @@ impl<T: CtxKind> Ctx<T> {
&self.inner.token
}

fn interaction_data(&self) -> &PartialInteractionData {
self.data.as_ref().expect("`self.data` must exist")
}

pub async fn interface(&self) -> Result<InteractionInterface, DeserializeBodyFromHttpError> {
Ok(self.bot.interaction().await?.interfaces(&self.inner))
}

pub fn bot_permissions(&self) -> Permissions {
self.inner
.app_permissions
.expect("this interaction must be executed in guilds")
.expect("interaction invoked in a guild")
}

pub fn bot_permissions_for(&self, channel_id: Id<ChannelMarker>) -> Permissions {
Expand All @@ -175,7 +169,7 @@ impl<T: CtxKind> Ctx<T> {
let everyone_role = self
.cache()
.role(guild_id.cast())
.expect("`@everyone` role must exist");
.expect("@everyone role should be in cache");
let member_roles = self
.bot_member()
.roles()
Expand All @@ -191,11 +185,11 @@ impl<T: CtxKind> Ctx<T> {
let channel = self
.cache()
.channel(channel_id)
.expect("channel must exist");
.expect("channel should be in cache");
let channel_overwrites = channel
.permission_overwrites
.as_ref()
.expect("permission overrwrites must exist");
.expect("channel is in a guild");

twilight_util::permission_calculator::PermissionCalculator::new(
guild_id,
Expand Down Expand Up @@ -256,8 +250,7 @@ impl<T: CtxKind> GuildIdAware for Ctx<T> {

impl<T: CtxKind> ExpectedGuildIdAware for Ctx<T> {
fn guild_id(&self) -> Id<GuildMarker> {
self.get_guild_id()
.expect("this interaction must be executed in guilds")
self.get_guild_id().expect("interaction invoked in a guild")
}
}

Expand All @@ -266,8 +259,8 @@ impl<T: CtxKind> AuthorPermissionsAware for Ctx<T> {
self.inner
.member
.as_ref()
.expect("this interaction must be executed in guilds")
.expect("interaction invoked in a guild")
.permissions
.expect("this field should exist")
.expect("member from an interaction")
}
}
32 changes: 23 additions & 9 deletions lyra/src/bot/command/model/ctx/command_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,27 @@ impl<T: CommandDataAware> Ctx<T> {
}
}

pub fn command_data(&self) -> &PartialCommandData {
fn interaction_data(&self) -> &PartialInteractionData {
self.data.as_ref().expect("T: CommandDataAware")
}

fn into_interaction_data(self) -> PartialInteractionData {
self.data.expect("T: CommandDataAware")
}

pub fn partial_command_data(&self) -> &PartialCommandData {
let PartialInteractionData::Command(data) = self.interaction_data() else {
unreachable!()
};
data
}

pub fn take_partial_command_data(&mut self) -> Option<PartialCommandData> {
self.data.take().and_then(|d| match d {
PartialInteractionData::Command(data) => Some(data),
_ => None,
})
pub fn into_partial_command_data(self) -> PartialCommandData {
let data = self.into_interaction_data();
let PartialInteractionData::Command(command_data) = data else {
unreachable!()
};
command_data
}

pub fn command_name_full(&self) -> Box<str> {
Expand All @@ -74,14 +83,19 @@ impl<T: CommandDataAware> Ctx<T> {
}

recurse_through_names(
vec![self.command_data().name.clone()],
&self.command_data().options,
vec![self.partial_command_data().name.clone()],
&self.partial_command_data().options,
)
.join(" ")
.into()
}

pub fn command_mention_full(&self) -> Box<str> {
format!("</{}:{}>", self.command_name_full(), self.command_data().id).into()
format!(
"</{}:{}>",
self.command_name_full(),
self.partial_command_data().id
)
.into()
}
}
16 changes: 8 additions & 8 deletions lyra/src/bot/command/model/ctx/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl TargetIdAware for MessageAppMarker {}

impl<T: TargetIdAware + AppCtxKind> Ctx<AppCtxMarker<T>> {
pub fn target_id(&self) -> Id<GenericMarker> {
self.command_data()
self.partial_command_data()
.target_id
.expect("`self.command_data().target_id` must exist")
.expect("T: TargetIdAware")
}
}

Expand All @@ -40,13 +40,13 @@ impl UserCtx {
}

pub fn target_user(&self) -> &User {
self.command_data()
self.partial_command_data()
.resolved
.as_ref()
.expect("`self.command_data().resolved` must exist")
.expect("interaction type is application command")
.users
.get(&self.target_user_id())
.expect("user must exist")
.expect("user should be resolved")
}
}

Expand All @@ -57,12 +57,12 @@ impl MessageCtx {
}

pub fn target_message(&self) -> &Message {
self.command_data()
self.partial_command_data()
.resolved
.as_ref()
.expect("`self.command_data().resolved` must exist")
.expect("interaction type is application command")
.messages
.get(&self.target_message_id())
.expect("message must exist")
.expect("message should be resolved")
}
}
Loading

0 comments on commit d102e0c

Please sign in to comment.