Skip to content

Commit

Permalink
Merge pull request #2248 from HTGAzureX1212/nightly
Browse files Browse the repository at this point in the history
`/info emoji`: Explicitly Handle Unknown Emoji
  • Loading branch information
HTGAzureX1212 authored May 31, 2024
2 parents 454a21f + a1b6582 commit eca673a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

## Discord Frontend

- **Added:** `plugin` attribute macro and `Plugin` traits
- **Added:** plugin-related functions to `CommandMetadata` trait
- **Added:** checks for whether plugins are enabled before running one of their commands
- **Changed:** lookup tables are now used for command dispatch rather than `match`
- **Changed:** extracted configuration models to its own crate (`serde` support included)
- **Changed:** updated `rust-version` to 1.80
- **Changed:** `/info emoji` now sends embeds
- **Changed:** more information has been added to `/info emoji`
- **Changed:** `/info emoji` now handles the error of an emoji not being found from database
- **Changed:** removed previously deprecated functions from `CommandMetadata`
- **Changed:** deprecated `command_type` and `interaction_only` in `CommandMetadata`
- **Changed:** `/info` check on whether `Utilities` plugin has been enabled before running
- **Removed:** `redis` dependency removed

## Localization Infrastructure
Expand Down
2 changes: 2 additions & 0 deletions discord-frontend/hartex-discord-commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#![deny(clippy::pedantic)]
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(if_let_guard)]
#![feature(let_chains)]

pub mod general;
pub mod utilities;
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use hartex_discord_core::discord::model::id::marker::EmojiMarker;
use hartex_discord_core::discord::model::id::Id;
use hartex_discord_core::discord::util::builder::embed::{EmbedBuilder, EmbedFieldBuilder};
use hartex_discord_core::discord::util::builder::InteractionResponseDataBuilder;
use hartex_discord_entitycache_core::error::CacheError;
use hartex_discord_entitycache_core::traits::Repository;
use hartex_discord_entitycache_repositories::emoji::CachedEmojiRepository;
use hartex_discord_utils::commands::CommandDataOptionExt;
Expand All @@ -44,6 +45,7 @@ use hartex_discord_utils::markdown::MarkdownStyle;
use hartex_localization_core::Localizer;
use miette::IntoDiagnostic;
use regex::Regex;
use tokio_postgres::error::SqlState;

lazy_static::lazy_static! {
/// The regex for looking for a Discord emoji in the command input.
Expand Down Expand Up @@ -71,6 +73,8 @@ pub async fn execute(
localizer.utilities_plugin_emojiinfo_error_only_custom_emojis()?;
let emojiinfo_error_only_one_emoji =
localizer.utilities_plugin_emojiinfo_error_only_one_emoji()?;
let emojiinfo_error_unknown_emoji =
localizer.utilities_plugin_emojiinfo_error_unknown_emoji()?;

let Some(captures) = EMOJI_REGEX.captures(&emoji) else {
interaction_client
Expand Down Expand Up @@ -115,10 +119,33 @@ pub async fn execute(
let id = captures.get(1).unwrap().as_str();
let emoji_id = Id::<EmojiMarker>::from_str(id).unwrap();

let emoji = CachedEmojiRepository
.get(emoji_id)
.await
.into_diagnostic()?;
let result = CachedEmojiRepository.get(emoji_id).await;
let emoji = match result {
Ok(emoji) => emoji,
Err(CacheError::Postgres(postgres_error))
if let Some(code) = postgres_error.code()
&& *code == SqlState::NO_DATA =>
{
interaction_client
.create_response(
interaction.id,
&interaction.token,
&InteractionResponse {
kind: InteractionResponseType::ChannelMessageWithSource,
data: Some(
InteractionResponseDataBuilder::new()
.content(emojiinfo_error_unknown_emoji)
.build(),
),
},
)
.await
.into_diagnostic()?;

return Ok(());
}
error => error.into_diagnostic()?,
};

let emojiinfo_embed_generalinfo_field_name =
localizer.utilities_plugin_emojiinfo_embed_generalinfo_field_name()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
//! This crate contains models that are returned by evaluating Lua configuration and can be
//! serialized via `serde`.

#![deny(clippy::pedantic)]
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(result_flattening)]

use mlua::Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ impl<'lua> FromLua<'lua> for Permissions {
.map_ok(|(key, value)| {
Id::from_str(key.as_str())
.map(|id| (id, value))
.map_err(|error| Error::runtime("invalid id"))
.map_err(|_| Error::runtime("invalid id"))
})
.map(|result| result.flatten())
.map(Result::flatten)
.collect::<Result<HashMap<_, _>, _>>()?,
users: users
.pairs::<String, u8>()
.map_ok(|(key, value)| {
Id::from_str(key.as_str())
.map(|id| (id, value))
.map_err(|error| Error::runtime("invalid id"))
.map_err(|_| Error::runtime("invalid id"))
})
.map(|result| result.flatten())
.map(Result::flatten)
.collect::<Result<HashMap<_, _>, _>>()?,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ emojiinfo-embed-generalinfo-managed-subfield-name=Managed:
emojiinfo-embed-generalinfo-name-subfield-name=Name:
emojiinfo-error-only-custom-emojis=Sorry! Only custom emojis are supported at this stage.
emojiinfo-error-only-one-emoji=Querying multiple emojis is not supported.
emojiinfo-error-unknown-emoji=Unknown emoji. Perhaps the bot is not in a server that has this emoji?
roleinfo-embed-description=Information of {$roleMention}
roleinfo-embed-generalinfo-field-name=General Information
roleinfo-embed-generalinfo-id-subfield-name=ID:
Expand Down

0 comments on commit eca673a

Please sign in to comment.