Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleEmoji methods don't need to be async #85

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/core/emoji.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::str::FromStr;

use async_trait::async_trait;
use twilight_http::request::channel::reaction::RequestReactionType;
use twilight_mention::Mention;
use twilight_model::{
Expand All @@ -19,13 +18,12 @@ pub struct SimpleEmoji {
pub as_id: Option<Id<EmojiMarker>>,
}

#[async_trait]
pub trait EmojiCommon: Sized {
type FromOut;
type Stored;

async fn into_readable(self, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> String;
async fn from_user_input(
fn into_readable(self, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> String;
fn from_user_input(
input: String,
bot: &StarboardBot,
guild_id: Id<GuildMarker>,
Expand All @@ -47,12 +45,11 @@ impl SimpleEmoji {
}
}

#[async_trait]
impl EmojiCommon for SimpleEmoji {
type FromOut = Option<Self>;
type Stored = String;

async fn into_readable(self, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> String {
fn into_readable(self, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> String {
if self.is_custom {
let emoji_id = self.as_id.unwrap();
if bot.cache.guild_emoji_exists(guild_id, emoji_id) {
Expand Down Expand Up @@ -82,7 +79,7 @@ impl EmojiCommon for SimpleEmoji {
self.raw
}

async fn from_user_input(
fn from_user_input(
input: String,
bot: &StarboardBot,
guild_id: Id<GuildMarker>,
Expand Down Expand Up @@ -116,15 +113,14 @@ impl EmojiCommon for SimpleEmoji {
}
}

#[async_trait]
impl EmojiCommon for Vec<SimpleEmoji> {
type FromOut = Self;
type Stored = Vec<String>;

async fn into_readable(self, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> String {
fn into_readable(self, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> String {
let mut arr = Vec::new();
for emoji in self {
arr.push(emoji.into_readable(bot, guild_id).await);
arr.push(emoji.into_readable(bot, guild_id));
}
if arr.is_empty() {
"no emojis".to_string()
Expand All @@ -149,10 +145,10 @@ impl EmojiCommon for Vec<SimpleEmoji> {
arr
}

async fn from_user_input(input: String, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> Self {
fn from_user_input(input: String, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> Self {
let mut arr = Vec::new();
for piece in input.split(' ') {
let emoji = SimpleEmoji::from_user_input(piece.to_string(), bot, guild_id).await;
let emoji = SimpleEmoji::from_user_input(piece.to_string(), bot, guild_id);
if let Some(emoji) = emoji {
arr.push(emoji);
}
Expand Down
4 changes: 1 addition & 3 deletions src/interactions/commands/chat/autostar/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ impl EditAutoStar {
};

if let Some(val) = self.emojis {
asc.emojis = Vec::<SimpleEmoji>::from_user_input(val, &ctx.bot, guild_id)
.await
.into_stored();
asc.emojis = Vec::<SimpleEmoji>::from_user_input(val, &ctx.bot, guild_id).into_stored();
}
if let Some(val) = self.min_chars {
let min_chars = val as i16;
Expand Down
14 changes: 9 additions & 5 deletions src/interactions/commands/chat/autostar/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ impl ViewAutoStarChannels {
AutoStarChannel::get_by_name(&ctx.bot.pool, name, unwrap_id!(guild_id)).await?;

if let Some(asc) = asc {
let emojis =
Vec::<SimpleEmoji>::from_stored(asc.emojis).into_readable(&ctx.bot, guild_id);
let max_chars = asc
.max_chars
.map(|v| v.to_string())
.unwrap_or_else(|| "none".to_string());
let asc_settings = concat_format!(
"channel: <#{}>\n" <- asc.channel_id;
"emojis: {}\n" <- Vec::<SimpleEmoji>::from_stored(asc.emojis).into_readable(&ctx.bot, guild_id).await;
"emojis: {}\n" <- emojis;
"min-chars: {}\n" <- asc.min_chars;
"max-chars: {}\n" <- asc.max_chars.map(|v| v.to_string()).unwrap_or_else(|| "none".to_string());
"max-chars: {}\n" <- max_chars;
"require-image: {}\n" <- asc.require_image;
"delete-invalid: {}" <- asc.delete_invalid;
);
Expand Down Expand Up @@ -66,9 +72,7 @@ impl ViewAutoStarChannels {
"'{}' in <#{}>: {}",
a.name,
a.channel_id,
Vec::<SimpleEmoji>::from_stored(a.emojis)
.into_readable(&ctx.bot, guild_id)
.await
Vec::<SimpleEmoji>::from_stored(a.emojis).into_readable(&ctx.bot, guild_id)
)
.unwrap();
}
Expand Down
8 changes: 2 additions & 6 deletions src/interactions/commands/chat/starboard/edit/requirements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ impl EditRequirements {
starboard.settings.required_remove = val;
}
if let Some(val) = self.upvote_emojis {
let emojis = Vec::<SimpleEmoji>::from_user_input(val, &ctx.bot, guild_id)
.await
.into_stored();
let emojis = Vec::<SimpleEmoji>::from_user_input(val, &ctx.bot, guild_id).into_stored();
if let Err(why) = validation::starboard_settings::validate_vote_emojis(
&emojis,
&starboard.settings.downvote_emojis,
Expand All @@ -105,9 +103,7 @@ impl EditRequirements {
.remove(&unwrap_id!(guild_id));
}
if let Some(val) = self.downvote_emojis {
let emojis = Vec::<SimpleEmoji>::from_user_input(val, &ctx.bot, guild_id)
.await
.into_stored();
let emojis = Vec::<SimpleEmoji>::from_user_input(val, &ctx.bot, guild_id).into_stored();
if let Err(why) = validation::starboard_settings::validate_vote_emojis(
&starboard.settings.upvote_emojis,
&emojis,
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/starboard/edit/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl EditGeneralStyle {
let emoji = if val == "none" {
None
} else {
match SimpleEmoji::from_user_input(val, &ctx.bot, guild_id).await {
match SimpleEmoji::from_user_input(val, &ctx.bot, guild_id) {
None => {
ctx.respond_str("Invalid emoji for `display-emoji`.", true)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/starboard/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl ViewStarboard {

if let Some(starboard) = starboard {
let config = StarboardConfig::new(starboard, vec![])?;
let pretty = format_settings(&ctx.bot, guild_id, &config).await;
let pretty = format_settings(&ctx.bot, guild_id, &config);

let embed = embed::build()
.title(format!("Starboard '{}'", &config.starboard.name))
Expand Down
16 changes: 6 additions & 10 deletions src/interactions/commands/format_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
},
};

pub async fn format_settings(
pub fn format_settings(
bot: &StarboardBot,
guild_id: Id<GuildMarker>,
config: &StarboardConfig,
Expand Down Expand Up @@ -56,15 +56,11 @@ pub async fn format_settings(
.clone()
.unwrap_or_else(|| "none".to_string()),
)
.into_readable(bot, guild_id)
.await;

let upvote_emojis = Vec::from_stored(res.upvote_emojis.clone())
.into_readable(bot, guild_id)
.await;
let downvote_emojis = Vec::from_stored(res.downvote_emojis.clone())
.into_readable(bot, guild_id)
.await;
.into_readable(bot, guild_id);

let upvote_emojis = Vec::from_stored(res.upvote_emojis.clone()).into_readable(bot, guild_id);
let downvote_emojis =
Vec::from_stored(res.downvote_emojis.clone()).into_readable(bot, guild_id);

let older_than = if res.older_than <= 0 {
"disabled".to_string()
Expand Down