Skip to content

Commit

Permalink
Use serenity 0.12, songbird 4.0, and poise 'current'
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 28, 2023
1 parent 838d1f5 commit 99f5e64
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 214 deletions.
322 changes: 185 additions & 137 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 3 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,16 @@ version = "5"
default-features = false

[dependencies.serenity_feature_only]
git = "https://github.com/GnomedDev/serenity"
features = ["unstable_discord_api", "typesize"]
default-features = false
package = "serenity"
branch = "typesize"
version = "0.12.0"

[dependencies.poise]
git = "https://github.com/serenity-rs/poise"
features = ["cache"]
branch = "serenity-next"
branch = "current"

[dependencies.songbird]
git = "https://github.com/serenity-rs/songbird"
version = "0.4"
features = ["builtin-queue"]
branch = "next"

[patch.crates-io]
serenity = { git = "https://github.com/GnomedDev/serenity", branch = "typesize" }
serenity-voice-model = { git = "https://github.com/GnomedDev/serenity", branch = "typesize" }

[patch."https://github.com/serenity-rs/serenity"]
serenity = { git = "https://github.com/GnomedDev/serenity", branch = "typesize" }
serenity-voice-model = { git = "https://github.com/GnomedDev/serenity", branch = "typesize" }
4 changes: 2 additions & 2 deletions src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn get_command_mapping(commands: &[Command]) -> IndexMap<&str, Vec<&Command>> {

fn format_params(buf: &mut String, command: &Command) {
for p in &command.parameters {
let name = p.name.as_deref().unwrap_or("unnamed");
let name = &p.name;
if p.required {
write!(buf, " <{name}>").unwrap();
} else {
Expand Down Expand Up @@ -206,7 +206,7 @@ pub async fn command_func(ctx: Context<'_>, command: Option<&str>) -> CommandRes
if !command_obj.parameters.is_empty() {
msg.push_str(ctx.gettext("__**Parameter Descriptions**__\n"));
command_obj.parameters.iter().for_each(|p| {
let name = p.name.as_deref().unwrap_or_else(|| ctx.gettext("unnamed"));
let name = &p.name;
let description = p
.description
.as_deref()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub async fn tts_speak_as(
context_menu_command = "Speak with your voice!"
)]
pub async fn tts_speak(ctx: ApplicationContext<'_>, message: serenity::Message) -> CommandResult {
_tts(ctx.into(), ctx.interaction.user(), &message.content).await
_tts(ctx.into(), &ctx.interaction.user, &message.content).await
}

/// Shows various different stats
Expand Down
32 changes: 16 additions & 16 deletions src/commands/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ impl<'a> MenuPaginator<'a> {
async fn voice_autocomplete(
ctx: ApplicationContext<'_>,
searching: &str,
) -> Vec<poise::AutocompleteChoice<String>> {
) -> Vec<serenity::AutocompleteChoice> {
let Ok((_, mode)) = ctx
.data
.parse_user_or_guild(ctx.interaction.user().id, ctx.interaction.guild_id())
.parse_user_or_guild(ctx.interaction.user.id, ctx.interaction.guild_id)
.await
else {
return Vec::new();
Expand All @@ -360,24 +360,20 @@ async fn voice_autocomplete(
let (mut i1, mut i2, mut i3, mut i4);
let voices: &mut dyn Iterator<Item = _> = match mode {
TTSMode::gTTS => {
i1 = data
.gtts_voices
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.map(|(value, name)| poise::AutocompleteChoice::new_with_value(name, value));
i1 = data.gtts_voices.iter().map(|(k, v)| (k.clone(), v.clone()));
&mut i1
}
TTSMode::eSpeak => {
i2 = data
.espeak_voices
.iter()
.cloned()
.map(poise::AutocompleteChoice::from);
.map(|voice| (voice.clone(), voice));
&mut i2
}
TTSMode::Polly => {
i3 = data.polly_voices.values().map(|voice| {
poise::AutocompleteChoice::new_with_value(
(
format!(
"{} - {} ({})",
voice.name, voice.language_name, voice.gender
Expand All @@ -390,7 +386,7 @@ async fn voice_autocomplete(
TTSMode::gCloud => {
i4 = data.gcloud_voices.iter().flat_map(|(language, variants)| {
variants.iter().map(move |(variant, gender)| {
poise::AutocompleteChoice::new_with_value(
(
format!("{language} {variant} ({gender})"),
format!("{language} {variant}"),
)
Expand All @@ -402,30 +398,34 @@ async fn voice_autocomplete(

let searching_lower = searching.to_lowercase();
let mut voices: Vec<_> = voices
.map(|choice| (choice.label.to_lowercase(), choice))
.map(|(label, value)| (label.to_lowercase(), value))
.collect();

voices.sort_by_cached_key(|(label, _)| strsim::levenshtein(label, &searching_lower));
voices.sort_by_key(|(label, _)| !label.contains(&searching_lower));
voices.into_iter().map(|(_, choice)| choice).collect()
voices
.into_iter()
.map(|(label, value)| serenity::AutocompleteChoice::new(label, value))
.collect()
}

#[allow(clippy::unused_async)]
async fn translation_languages_autocomplete(
ctx: ApplicationContext<'_>,
searching: &str,
) -> Vec<poise::AutocompleteChoice<String>> {
) -> impl Iterator<Item = serenity::AutocompleteChoice> {
let mut filtered_languages = ctx
.data
.translation_languages
.iter()
.filter(|(_, name)| name.starts_with(searching))
.map(|(value, name)| (value.clone(), name.clone()))
.map(|(value, name)| poise::AutocompleteChoice::new_with_value(name, value))
.collect::<Vec<_>>();

filtered_languages.sort_by_key(|choice| strsim::levenshtein(&choice.label, searching));
filtered_languages.sort_by_key(|(label, _)| strsim::levenshtein(label, searching));
filtered_languages
.into_iter()
.map(|(label, value)| serenity::AutocompleteChoice::new(label, value))
}

async fn bool_button(ctx: Context<'_>, value: Option<bool>) -> Result<Option<bool>, Error> {
Expand Down Expand Up @@ -1438,7 +1438,7 @@ Just do `/join` and start talking!
ctx.gettext("Okay, didn't set up update announcements.")
};

ctx.send(poise::CreateReply::new().content(reply).ephemeral(true))
ctx.send(poise::CreateReply::default().content(reply).ephemeral(true))
.await?;

Ok(())
Expand Down
15 changes: 8 additions & 7 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ pub async fn handle(error: poise::FrameworkError<'_, Data, Error>) -> Result<()>
}

poise::FrameworkError::EventHandler {
ctx,
error,
event,
framework,
Expand All @@ -477,27 +478,27 @@ pub async fn handle(error: poise::FrameworkError<'_, Data, Error>) -> Result<()>
}

match event {
Event::Message { ctx, new_message } => {
Event::Message { new_message } => {
handle_message(ctx, framework, new_message, Err(error)).await?;
}
Event::GuildMemberAddition { ctx, new_member } => {
Event::GuildMemberAddition { new_member } => {
handle_member(ctx, framework, new_member, Err(error)).await?;
}
Event::GuildCreate { ctx, guild, .. } => {
Event::GuildCreate { guild, .. } => {
handle_guild("GuildCreate", ctx, framework, Some(guild), Err(error)).await?;
}
Event::GuildDelete { ctx, full, .. } => {
Event::GuildDelete { full, .. } => {
handle_guild("GuildDelete", ctx, framework, full.as_ref(), Err(error)).await?;
}
Event::VoiceStateUpdate { ctx, .. } => {
Event::VoiceStateUpdate { .. } => {
handle_unexpected_default(ctx, framework, "VoiceStateUpdate", Err(error))
.await?;
}
Event::InteractionCreate { ctx, .. } => {
Event::InteractionCreate { .. } => {
handle_unexpected_default(ctx, framework, "InteractionCreate", Err(error))
.await?;
}
Event::Ready { ctx, .. } => {
Event::Ready { .. } => {
handle_unexpected_default(ctx, framework, "Ready", Err(error)).await?;
}
_ => {
Expand Down
36 changes: 15 additions & 21 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,33 @@ use serenity::FullEvent as Event;

use crate::structs::{FrameworkContext, Result};

pub async fn listen(framework_ctx: FrameworkContext<'_>, event: &Event) -> Result<()> {
pub async fn listen(
ctx: &serenity::Context,
event: &Event,
framework_ctx: FrameworkContext<'_>,
) -> Result<()> {
let data = framework_ctx.user_data;

match event {
Event::Message { ctx, new_message } => message(framework_ctx, ctx, new_message).await,
Event::GuildCreate { ctx, guild, is_new } => guild_create(ctx, data, guild, *is_new).await,
Event::Ready {
ctx,
data_about_bot,
} => ready(framework_ctx, ctx, data_about_bot).await,
Event::GuildDelete {
ctx,
incomplete,
full,
} => guild_delete(ctx, data, incomplete, full.as_ref()).await,
Event::GuildMemberAddition { ctx, new_member } => {
Event::Message { new_message } => message(framework_ctx, ctx, new_message).await,
Event::GuildCreate { guild, is_new } => guild_create(ctx, data, guild, *is_new).await,
Event::Ready { data_about_bot } => ready(framework_ctx, ctx, data_about_bot).await,
Event::GuildDelete { incomplete, full } => {
guild_delete(ctx, data, incomplete, full.as_ref()).await
}
Event::GuildMemberAddition { new_member } => {
guild_member_addition(ctx, data, new_member).await
}
Event::GuildMemberRemoval {
ctx,
guild_id,
user,
member_data_if_available: _,
} => guild_member_removal(ctx, data, *guild_id, user).await,
Event::VoiceStateUpdate { ctx, old, new } => {
Event::VoiceStateUpdate { old, new } => {
voice_state_update(ctx, data, old.as_ref(), new).await
}
Event::ChannelDelete {
ctx: _,
channel,
messages: _,
} => channel_delete(data, channel).await,
Event::InteractionCreate { ctx, interaction } => {
Event::ChannelDelete { channel, .. } => channel_delete(data, channel).await,
Event::InteractionCreate { interaction } => {
interaction_create(framework_ctx, ctx, interaction).await
}
Event::Resume { .. } => {
Expand Down
12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ async fn _main(start_time: std::time::SystemTime) -> Result<()> {

let framework_options = poise::FrameworkOptions {
commands: commands::commands(),
event_handler: |event, ctx, _| Box::pin(events::listen(ctx, event)),
event_handler: |ctx, event, fw_ctx, _| Box::pin(events::listen(ctx, event, fw_ctx)),
on_error: |error| {
Box::pin(async move {
let res = errors::handle(error).await;
Expand All @@ -362,11 +362,9 @@ async fn _main(start_time: std::time::SystemTime) -> Result<()> {
analytics_handler.log(
Cow::Borrowed(match ctx {
poise::Context::Prefix(_) => "command",
poise::Context::Application(ctx) => match ctx.interaction {
poise::CommandOrAutocompleteInteraction::Autocomplete(_) => {
"autocomplete"
}
poise::CommandOrAutocompleteInteraction::Command(_) => "slash_command",
poise::Context::Application(ctx) => match ctx.interaction_type {
poise::CommandInteractionType::Autocomplete => "autocomplete",
poise::CommandInteractionType::Command => "slash_command",
},
}),
false,
Expand Down Expand Up @@ -480,7 +478,7 @@ async fn _main(start_time: std::time::SystemTime) -> Result<()> {

async fn premium_command_check(ctx: Context<'_>) -> Result<bool> {
if let Context::Application(ctx) = ctx {
if let poise::CommandOrAutocompleteInteraction::Autocomplete(_) = ctx.interaction {
if ctx.interaction_type == poise::CommandInteractionType::Autocomplete {
// Ignore the premium check during autocomplete.
return Ok(true);
}
Expand Down
17 changes: 7 additions & 10 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ impl PoiseContextExt for Context<'_> {

fn current_catalog(&self) -> Option<&gettext::Catalog> {
if let poise::Context::Application(ctx) = self {
if let poise::CommandOrAutocompleteInteraction::Command(interaction) = ctx.interaction {
return ctx
.data
.translations
.get(match interaction.locale.as_str() {
"ko" => "ko-KR",
"pt-BR" => "pt",
l => l,
});
}
ctx.data
.translations
.get(match ctx.interaction.locale.as_str() {
"ko" => "ko-KR",
"pt-BR" => "pt",
l => l,
});
};

None
Expand Down

0 comments on commit 99f5e64

Please sign in to comment.