diff --git a/src/cache/cache.rs b/src/cache/cache.rs index 59bde570..d077f543 100644 --- a/src/cache/cache.rs +++ b/src/cache/cache.rs @@ -46,7 +46,7 @@ impl Cache { guilds: DashMap::new().into(), users: DashMap::new().into(), messages: stretto::AsyncCache::new( - (constants::MAX_MESSAGES * 10).try_into().unwrap(), + (constants::MAX_MESSAGES * 10) as usize, constants::MAX_MESSAGES.into(), tokio::spawn, ) @@ -54,13 +54,13 @@ impl Cache { autostar_channel_ids: autostar_channel_ids.into(), guild_vote_emojis: DashMap::new().into(), guild_autostar_channel_names: stretto::AsyncCache::new( - (constants::MAX_NAMES * 10).try_into().unwrap(), + (constants::MAX_NAMES * 10) as usize, constants::MAX_NAMES.into(), tokio::spawn, ) .unwrap(), guild_starboard_names: stretto::AsyncCache::new( - (constants::MAX_NAMES * 10).try_into().unwrap(), + (constants::MAX_NAMES * 10) as usize, constants::MAX_NAMES.into(), tokio::spawn, ) @@ -92,9 +92,10 @@ impl Cache { // helper methods pub fn guild_emoji_exists(&self, guild_id: Id, emoji_id: Id) -> bool { - self.guilds.with(&guild_id, |_, guild| match guild { - None => false, - Some(guild) => guild.emojis.contains(&emoji_id), + self.guilds.with(&guild_id, |_, guild| { + guild + .as_ref() + .map_or(false, |guild| guild.emojis.contains(&emoji_id)) }) } @@ -114,7 +115,7 @@ impl Cache { let guild = guild.as_ref().unwrap(); if let Some(thread_parent_id) = guild.active_thread_parents.get(&channel_id) { - current_channel_id = Some(thread_parent_id.to_owned()); + current_channel_id = Some(*thread_parent_id); return false; } @@ -245,10 +246,10 @@ impl Cache { // check if the channel_id is a known thread, and use the parent_id // if it is. - let channel_id = match guild.active_thread_parents.get(&channel_id) { - None => channel_id, - Some(parent_id) => *parent_id, - }; + let channel_id = guild + .active_thread_parents + .get(&channel_id) + .map_or(channel_id, |&parent_id| parent_id); // check the cached nsfw/sfw channel list if let Some(channel) = guild.channels.get(&channel_id) { diff --git a/src/cache/events/channel.rs b/src/cache/events/channel.rs index c3553bbc..65594bd1 100644 --- a/src/cache/events/channel.rs +++ b/src/cache/events/channel.rs @@ -18,7 +18,7 @@ impl UpdateCache for ChannelCreate { .insert(self.id, CachedChannel::from_channel(channel, self)); guild - }) + }); } } @@ -32,14 +32,13 @@ impl UpdateCache for ChannelDelete { cache.guilds.alter(&guild_id, |_, mut guild| { guild.channels.remove(&self.id); - guild.active_thread_parents = guild + + guild .active_thread_parents - .into_iter() - .filter(|(_, channel_id)| channel_id != &self.id) - .collect(); + .retain(|_, &mut channel_id| channel_id != self.id); guild - }) + }); } } @@ -58,6 +57,6 @@ impl UpdateCache for ChannelUpdate { .insert(self.id, CachedChannel::from_channel(channel, self)); guild - }) + }); } } diff --git a/src/cache/events/message.rs b/src/cache/events/message.rs index 280c2815..fc2bfc1e 100644 --- a/src/cache/events/message.rs +++ b/src/cache/events/message.rs @@ -34,7 +34,7 @@ impl UpdateCache for MessageDelete { impl UpdateCache for MessageDeleteBulk { async fn update_cache(&self, cache: &Cache) { for id in &self.ids { - cache.messages.insert(id.clone(), None, 1).await; + cache.messages.insert(*id, None, 1).await; } } } diff --git a/src/cache/events/thread.rs b/src/cache/events/thread.rs index 48c00176..94c58673 100644 --- a/src/cache/events/thread.rs +++ b/src/cache/events/thread.rs @@ -22,7 +22,7 @@ impl UpdateCache for ThreadCreate { cache.guilds.alter(&guild_id, |_, mut guild| { guild.active_thread_parents.insert(self.id, parent_id); guild - }) + }); } } @@ -32,7 +32,7 @@ impl UpdateCache for ThreadDelete { cache.guilds.alter(&self.guild_id, |_, mut guild| { guild.active_thread_parents.remove(&self.id); guild - }) + }); } } @@ -60,7 +60,7 @@ impl UpdateCache for ThreadUpdate { } guild - }) + }); } } @@ -84,7 +84,7 @@ impl UpdateCache for ThreadListSync { .filter(|(_, parent_id)| !channel_ids.contains(parent_id)) .collect(); - for thread in self.threads.iter() { + for thread in &self.threads { if let Some(parent_id) = thread.parent_id { threads.insert(thread.id, parent_id); } @@ -94,6 +94,6 @@ impl UpdateCache for ThreadListSync { } guild - }) + }); } } diff --git a/src/client/config.rs b/src/client/config.rs index b750f311..4397135b 100644 --- a/src/client/config.rs +++ b/src/client/config.rs @@ -18,7 +18,7 @@ impl Config { }; let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN not set"); let shards = env::var("SHARDS") - .unwrap_or("1".to_string()) + .unwrap_or_else(|_| "1".to_string()) .parse() .unwrap(); let db_url = env::var("SB_DATABASE_URL").expect("No database url specified."); @@ -26,7 +26,7 @@ impl Config { .ok() .map(|v| v.parse().expect("Invalid ID for error log channel.")); let development = env::var("DEVELOPMENT") - .unwrap_or("false".to_string()) + .unwrap_or_else(|_| "false".to_string()) .parse() .expect("Invalid boolean for DEVELOPMENT."); let owner_ids = env::var("OWNER_IDS").ok().map(|var| { diff --git a/src/client/cooldowns.rs b/src/client/cooldowns.rs index f0214155..5e456166 100644 --- a/src/client/cooldowns.rs +++ b/src/client/cooldowns.rs @@ -17,3 +17,9 @@ impl Cooldowns { } } } + +impl Default for Cooldowns { + fn default() -> Self { + Self::new() + } +} diff --git a/src/core/embedder/main.rs b/src/core/embedder/main.rs index 5926d4ad..05bf967b 100644 --- a/src/core/embedder/main.rs +++ b/src/core/embedder/main.rs @@ -20,9 +20,7 @@ impl Embedder<'_> { ) -> Result, twilight_http::Error> { bot.http - .create_message(Id::new( - self.config.starboard.channel_id.try_into().unwrap(), - )) + .create_message(Id::new(self.config.starboard.channel_id as u64)) .content(&self.get_top_text(false)) .unwrap() .exec() @@ -37,10 +35,7 @@ impl Embedder<'_> { ) -> Result, twilight_http::Error> { bot.http - .update_message( - Id::new(self.config.starboard.channel_id.try_into().unwrap()), - message_id, - ) + .update_message(Id::new(self.config.starboard.channel_id as u64), message_id) .content(Some(&self.get_top_text(trashed))) .unwrap() .exec() diff --git a/src/core/emoji.rs b/src/core/emoji.rs index faaab400..4de12129 100644 --- a/src/core/emoji.rs +++ b/src/core/emoji.rs @@ -91,7 +91,7 @@ impl EmojiCommon for SimpleEmoji { // input. https://emojipedia.org/variation-selector-16/ let input = input .strip_suffix('\u{fe0f}') - .map_or((&input).to_string(), |s| s.to_string()); + .map_or_else(|| input.to_string(), |s| s.to_string()); if emojis::get(&input).is_some() { Some(Self { @@ -151,7 +151,7 @@ impl EmojiCommon for Vec { async fn from_user_input(input: String, bot: &StarboardBot, guild_id: Id) -> Self { let mut arr = Vec::new(); - for piece in (&input).split(' ') { + for piece in input.split(' ') { let emoji = SimpleEmoji::from_user_input(piece.to_string(), bot, guild_id).await; if let Some(emoji) = emoji { arr.push(emoji); diff --git a/src/core/starboard/handle.rs b/src/core/starboard/handle.rs index f39d0f37..a1477088 100644 --- a/src/core/starboard/handle.rs +++ b/src/core/starboard/handle.rs @@ -58,8 +58,8 @@ impl RefreshMessage<'_> { async fn get_configs(&mut self) -> sqlx::Result>> { if self.configs.is_none() { let msg = self.get_sql_message().await?; - let guild_id = Id::new(msg.guild_id.try_into().unwrap()); - let channel_id = Id::new(msg.channel_id.try_into().unwrap()); + let guild_id = Id::new(msg.guild_id as u64); + let channel_id = Id::new(msg.channel_id as u64); let configs = StarboardConfig::list_for_channel(self.bot, guild_id, channel_id) .await @@ -132,7 +132,7 @@ impl<'this, 'bot> RefreshStarboard<'this, 'bot> { StarboardMessage::set_last_point_count( &self.refresh.bot.pool, sb_msg.starboard_message_id, - points.try_into().unwrap(), + points as i16, ) .await?; @@ -143,8 +143,8 @@ impl<'this, 'bot> RefreshStarboard<'this, 'bot> { .bot .http .delete_message( - Id::new(self.config.starboard.channel_id.try_into().unwrap()), - Id::new(sb_msg.starboard_message_id.try_into().unwrap()), + Id::new(self.config.starboard.channel_id as u64), + Id::new(sb_msg.starboard_message_id as u64), ) .exec() .await; @@ -154,7 +154,7 @@ impl<'this, 'bot> RefreshStarboard<'this, 'bot> { let ret = embedder .edit( self.refresh.bot, - Id::new(sb_msg.starboard_message_id.try_into().unwrap()), + Id::new(sb_msg.starboard_message_id as u64), false, ) .await; @@ -164,7 +164,7 @@ impl<'this, 'bot> RefreshStarboard<'this, 'bot> { let ret = embedder .edit( self.refresh.bot, - Id::new(sb_msg.starboard_message_id.try_into().unwrap()), + Id::new(sb_msg.starboard_message_id as u64), true, ) .await; diff --git a/src/core/starboard/msg_status.rs b/src/core/starboard/msg_status.rs index 72a051be..d4653d9a 100644 --- a/src/core/starboard/msg_status.rs +++ b/src/core/starboard/msg_status.rs @@ -1,5 +1,3 @@ -use std::convert::TryInto; - use twilight_model::id::Id; use crate::{client::bot::StarboardBot, database::Message as DbMessage, errors::StarboardResult}; @@ -20,8 +18,8 @@ pub async fn get_message_status( message: &DbMessage, points: i32, ) -> StarboardResult { - let guild_id = Id::new(starboard_config.starboard.guild_id.try_into().unwrap()); - let sb_channel_id = Id::new(starboard_config.starboard.channel_id.try_into().unwrap()); + let guild_id = Id::new(starboard_config.starboard.guild_id as u64); + let sb_channel_id = Id::new(starboard_config.starboard.channel_id as u64); let sb_is_nsfw = bot .cache .fog_channel_nsfw(bot, guild_id, sb_channel_id) diff --git a/src/core/starboard/reaction_events.rs b/src/core/starboard/reaction_events.rs index 05164778..eb276a58 100644 --- a/src/core/starboard/reaction_events.rs +++ b/src/core/starboard/reaction_events.rs @@ -105,9 +105,9 @@ pub async fn handle_reaction_add( &emoji, configs, Id::new(event.user_id.try_into().unwrap()), - Id::new(orig_msg.message_id.try_into().unwrap()), - Id::new(orig_msg.channel_id.try_into().unwrap()), - Id::new(orig_msg.author_id.try_into().unwrap()), + Id::new(orig_msg.message_id as u64), + Id::new(orig_msg.channel_id as u64), + Id::new(orig_msg.author_id as u64), author_is_bot, None, ) @@ -200,9 +200,9 @@ pub async fn handle_reaction_remove( &emoji, configs, Id::new(event.user_id.try_into().unwrap()), - Id::new(orig.message_id.try_into().unwrap()), - Id::new(orig.channel_id.try_into().unwrap()), - Id::new(orig.author_id.try_into().unwrap()), + Id::new(orig.message_id as u64), + Id::new(orig.channel_id as u64), + Id::new(orig.author_id as u64), author.is_bot, None, ) diff --git a/src/core/starboard/vote_status.rs b/src/core/starboard/vote_status.rs index 43d8df57..57665f90 100644 --- a/src/core/starboard/vote_status.rs +++ b/src/core/starboard/vote_status.rs @@ -35,98 +35,90 @@ impl VoteStatus { message_author_is_bot: bool, message_has_image: Option, ) -> StarboardResult { - let message_has_image = match message_has_image { - Some(val) => Some(val), - None => match bot - .cache + let message_has_image = if let Some(val) = message_has_image { + val + } else { + bot.cache .fog_message(bot, channel_id, message_id) .await? .value() - { - Some(msg) => Some(has_image(&msg.embeds, &msg.attachments)), - None => None, - }, + .as_ref() + .map(|msg| has_image(&msg.embeds, &msg.attachments)) + == Some(true) }; let mut invalid_exists = false; let mut allow_remove = true; - let mut upvote = Vec::new(); - let mut downvote = Vec::new(); - for config in configs { + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis(); + + #[derive(Clone, Copy, PartialEq, Eq)] + enum VoteType { + Upvote, + Downvote, + } + + let eval_config = |config: StarboardConfig| { // skip disabled configurations if !config.resolved.enabled || config.starboard.premium_locked { - continue; + return None; } - let is_downvote; - if config.resolved.upvote_emojis.contains(&emoji.raw) { - is_downvote = false; + let vote_type = if config.resolved.upvote_emojis.contains(&emoji.raw) { + VoteType::Upvote } else if config.resolved.downvote_emojis.contains(&emoji.raw) { - is_downvote = true; + VoteType::Downvote } else { - continue; - } + return None; + }; // respect the `remove_invalid_reactions` setting if !config.resolved.remove_invalid_reactions { allow_remove = false; } - // check settings - let is_valid; + // message age in seconds + let message_age: i64 = { + let created_at = message_id.timestamp() as u128; + ((now - created_at) / 1000) as i64 + }; - // settings to check: - // - self_vote - // - allow_bots - // - require_image - // - older_than - // - newer_than - // alow need to check permroles + let min_age = config.resolved.older_than; + let max_age = config.resolved.newer_than; - let message_age: i64 = { - let now = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_millis(); - let created_at = message_id.timestamp(); + let self_vote_valid = config.resolved.self_vote || reactor_id != message_author_id; - ((now - created_at as u128) / 1000).try_into().unwrap() - }; - let older_than = config.resolved.older_than; - let newer_than = config.resolved.newer_than; - - if !config.resolved.self_vote && reactor_id == message_author_id { - // self-vote - is_valid = false; - } else if !config.resolved.allow_bots && message_author_is_bot { - // allow-bots - is_valid = false; - } else if config.resolved.require_image && !matches!(message_has_image, Some(true)) { - // require-image - is_valid = false; - } else if older_than != 0 && message_age < older_than { - // older-than - is_valid = false; - } else if newer_than != 0 && message_age > newer_than { - // newer-than - is_valid = false; - } else { - is_valid = true; - } + let bots_valid = config.resolved.allow_bots || !message_author_is_bot; - if !is_valid { - invalid_exists = true; - continue; - } + let images_valid = config.resolved.require_image || message_has_image; - // add to corresponding list - if is_downvote { - downvote.push(config); + let time_valid = (min_age..max_age).contains(&message_age); + + if self_vote_valid && bots_valid && images_valid && time_valid { + Some((config, vote_type)) } else { - upvote.push(config); + invalid_exists = true; + None } - } + }; + + let mut upvote = Vec::new(); + let mut downvote = Vec::new(); + + configs + .into_iter() + .filter_map(eval_config) + .for_each(|(config, vote_type)| { + if vote_type == VoteType::Upvote { + upvote.push(config) + } else { + downvote.push(config) + } + }); + if upvote.is_empty() && downvote.is_empty() { if invalid_exists && allow_remove { Ok(VoteStatus::Remove) diff --git a/src/database/models/starboard_message.rs b/src/database/models/starboard_message.rs index a50b5dc3..222847e9 100644 --- a/src/database/models/starboard_message.rs +++ b/src/database/models/starboard_message.rs @@ -14,7 +14,7 @@ impl StarboardMessage { starboard_id: i32, last_known_point_count: i32, ) -> sqlx::Result { - let point_count: i16 = last_known_point_count.try_into().unwrap(); + let point_count = last_known_point_count as i16; sqlx::query_as!( Self, r#"INSERT INTO starboard_messages diff --git a/src/interactions/commands/chat/autostar/edit.rs b/src/interactions/commands/chat/autostar/edit.rs index e7375268..126af106 100644 --- a/src/interactions/commands/chat/autostar/edit.rs +++ b/src/interactions/commands/chat/autostar/edit.rs @@ -51,18 +51,14 @@ impl EditAutoStar { .into_stored(); } if let Some(val) = self.min_chars { - let min_chars = val.try_into().unwrap(); + let min_chars = val as i16; if let Err(why) = asc.set_min_chars(min_chars) { ctx.respond_str(&why, true).await?; return Ok(()); } } if let Some(val) = self.max_chars { - let max_chars = if val == -1 { - None - } else { - Some(val.try_into().unwrap()) - }; + let max_chars = if val == -1 { None } else { Some(val as i16) }; if let Err(why) = asc.set_max_chars(max_chars) { ctx.respond_str(&why, true).await?; return Ok(()); diff --git a/src/interactions/commands/chat/autostar/view.rs b/src/interactions/commands/chat/autostar/view.rs index be1fe248..9659b7b9 100644 --- a/src/interactions/commands/chat/autostar/view.rs +++ b/src/interactions/commands/chat/autostar/view.rs @@ -31,7 +31,7 @@ impl ViewAutoStarChannels { "channel: <#{}>\n" <- asc.channel_id; "emojis: {}\n" <- Vec::::from_stored(asc.emojis).into_readable(&ctx.bot, guild_id).await; "min-chars: {}\n" <- asc.min_chars; - "max-chars: {}\n" <- asc.max_chars.map(|v| v.to_string()).unwrap_or("none".to_string()); + "max-chars: {}\n" <- asc.max_chars.map(|v| v.to_string()).unwrap_or_else(|| "none".to_string()); "require-image: {}\n" <- asc.require_image; "delete-invalid: {}" <- asc.delete_invalid; ); diff --git a/src/interactions/commands/chat/starboard/edit/requirements.rs b/src/interactions/commands/chat/starboard/edit/requirements.rs index 7709759c..bda8bb7f 100644 --- a/src/interactions/commands/chat/starboard/edit/requirements.rs +++ b/src/interactions/commands/chat/starboard/edit/requirements.rs @@ -64,7 +64,7 @@ impl EditRequirements { }; if let Some(val) = self.required { - let val = val.try_into().unwrap(); + let val = val as i16; if let Err(why) = validation::starboard_settings::validate_required( val, starboard.settings.required_remove, @@ -75,7 +75,7 @@ impl EditRequirements { starboard.settings.required = val; } if let Some(val) = self.required_remove { - let val = val.try_into().unwrap(); + let val = val as i16; if let Err(why) = validation::starboard_settings::validate_required_remove( val, starboard.settings.required, diff --git a/src/interactions/commands/format_settings.rs b/src/interactions/commands/format_settings.rs index f787c28c..9f5684e8 100644 --- a/src/interactions/commands/format_settings.rs +++ b/src/interactions/commands/format_settings.rs @@ -19,26 +19,23 @@ pub async fn format_settings( guild_id: Id, config: &StarboardConfig, ) -> FormattedStarboardSettings { - let ov_values = match config.overrides.get(0) { - None => None, - Some(ov) => Some(ov.get_overrides().unwrap()), - }; + let ov_values = config + .overrides + .get(0) + .map(|ov| ov.get_overrides().unwrap()); macro_rules! settings { ($($setting: ident, $pretty_name: expr, $value: expr;)*) => {{ let mut final_result = String::new(); $( - let is_bold = match &ov_values { - None => false, - Some(ov) => match ov.$setting { - None => false, - Some(_) => true, - }, - }; - - let setting_name = match is_bold { - true => format!("**{}**", $pretty_name), - false => $pretty_name.to_string(), + let is_bold = ov_values.as_ref().map_or(false, |ov| { + ov.$setting.is_some() + }); + + let setting_name = if is_bold { + format!("**{}**", $pretty_name) + } else { + $pretty_name.to_string() }; final_result.push_str(&format!("{}: {}\n", setting_name, $value)); @@ -54,37 +51,35 @@ pub async fn format_settings( .resolved .display_emoji .clone() - .unwrap_or("none".to_string()), + .unwrap_or_else(|| "none".to_string()), ) .into_readable(bot, guild_id) .await; - let upvote_emojis = Vec::::from_stored(res.upvote_emojis.clone()) + 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()) + let downvote_emojis = Vec::from_stored(res.downvote_emojis.clone()) .into_readable(bot, guild_id) .await; - let older_than = match res.older_than { - x if x <= 0 => "disabled".to_string(), - x => format_duration(Duration::from_secs(x.try_into().unwrap())).to_string(), + let older_than = if res.older_than <= 0 { + "disabled".to_string() + } else { + format_duration(Duration::from_secs(res.older_than as u64)).to_string() }; - let newer_than = match res.newer_than { - x if x <= 0 => "disabled".to_string(), - x => format_duration(Duration::from_secs(x.try_into().unwrap())).to_string(), + let newer_than = if res.newer_than <= 0 { + "disabled".to_string() + } else { + format_duration(Duration::from_secs(res.newer_than as u64)).to_string() }; let cooldown = { - let is_bold = match &ov_values { - None => false, - Some(ov) => ov.cooldown_count.is_some() || ov.cooldown_period.is_some(), - }; + let is_bold = ov_values.as_ref().map_or(false, |ov| { + ov.cooldown_count.is_some() || ov.cooldown_period.is_some() + }); - let setting_name = match is_bold { - false => "cooldown", - true => "**cooldown**", - }; + let setting_name = if is_bold { "**cooldown**" } else { "cooldown" }; format!( "{}: {} reactions per {} seconds\n", @@ -114,7 +109,7 @@ pub async fn format_settings( ), embed: settings!( color, "color", &format!( - "#{:X}", res.color.unwrap_or(constants::BOT_COLOR.try_into().unwrap()) + "#{:X}", res.color.unwrap_or(constants::BOT_COLOR as i32) ); jump_to_message, "jump-to-message", res.jump_to_message; attachments_list, "attachments-list", res.attachments_list; diff --git a/src/owner/commands/sql.rs b/src/owner/commands/sql.rs index a6f0161b..ccec9eae 100644 --- a/src/owner/commands/sql.rs +++ b/src/owner/commands/sql.rs @@ -42,7 +42,7 @@ pub async fn run_sql(bot: &StarboardBot, event: &MessageCreate) -> anyhow::Resul let result = SqlResult { execution_times, inspect: result.unwrap_or(None), - tag: (*meta.get("tag").unwrap_or(&"?query?")).to_string(), + tag: meta.get("tag").unwrap_or(&"?query?").to_string(), }; results.push(result); } diff --git a/src/utils/cooldowns.rs b/src/utils/cooldowns.rs index 18fd3657..d6dea2c2 100644 --- a/src/utils/cooldowns.rs +++ b/src/utils/cooldowns.rs @@ -68,6 +68,15 @@ where } } +impl Default for FlexibleMapping +where + K: Hash + Eq + Clone, +{ + fn default() -> Self { + Self::new() + } +} + pub struct JumpingWindow { last_reset: Instant, period: Duration, diff --git a/src/utils/notify.rs b/src/utils/notify.rs index b8d5783e..64f0528d 100644 --- a/src/utils/notify.rs +++ b/src/utils/notify.rs @@ -14,8 +14,7 @@ pub async fn notify(bot: &StarboardBot, user_id: Id, message: &str) return; } - let create = dm::dm(bot, user_id).await; - let create = match create { + let create = match dm::dm(bot, user_id).await { Err(_) => return, Ok(create) => create, };