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

Few more minor refactorings #71

Merged
merged 8 commits into from
Sep 17, 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
23 changes: 12 additions & 11 deletions src/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ 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,
)
.unwrap(),
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,
)
Expand Down Expand Up @@ -92,9 +92,10 @@ impl Cache {

// helper methods
pub fn guild_emoji_exists(&self, guild_id: Id<GuildMarker>, emoji_id: Id<EmojiMarker>) -> 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))
})
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions src/cache/events/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl UpdateCache for ChannelCreate {
.insert(self.id, CachedChannel::from_channel(channel, self));

guild
})
});
}
}

Expand All @@ -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);
circuitsacul marked this conversation as resolved.
Show resolved Hide resolved

guild
})
});
}
}

Expand All @@ -58,6 +57,6 @@ impl UpdateCache for ChannelUpdate {
.insert(self.id, CachedChannel::from_channel(channel, self));

guild
})
});
}
}
2 changes: 1 addition & 1 deletion src/cache/events/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/cache/events/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
});
}
}

Expand All @@ -32,7 +32,7 @@ impl UpdateCache for ThreadDelete {
cache.guilds.alter(&self.guild_id, |_, mut guild| {
guild.active_thread_parents.remove(&self.id);
guild
})
});
}
}

Expand Down Expand Up @@ -60,7 +60,7 @@ impl UpdateCache for ThreadUpdate {
}

guild
})
});
}
}

Expand All @@ -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);
}
Expand All @@ -94,6 +94,6 @@ impl UpdateCache for ThreadListSync {
}

guild
})
});
}
}
4 changes: 2 additions & 2 deletions src/client/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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.");
let error_channel = env::var("ERROR_CHANNEL_ID")
.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| {
Expand Down
6 changes: 6 additions & 0 deletions src/client/cooldowns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ impl Cooldowns {
}
}
}

impl Default for Cooldowns {
fn default() -> Self {
Self::new()
}
}
9 changes: 2 additions & 7 deletions src/core/embedder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ impl Embedder<'_> {
) -> Result<twilight_http::Response<twilight_model::channel::Message>, 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()
Expand All @@ -37,10 +35,7 @@ impl Embedder<'_> {
) -> Result<twilight_http::Response<twilight_model::channel::Message>, 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()
Expand Down
4 changes: 2 additions & 2 deletions src/core/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -151,7 +151,7 @@ impl EmojiCommon for Vec<SimpleEmoji> {

async fn from_user_input(input: String, bot: &StarboardBot, guild_id: Id<GuildMarker>) -> 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);
Expand Down
14 changes: 7 additions & 7 deletions src/core/starboard/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl RefreshMessage<'_> {
async fn get_configs(&mut self) -> sqlx::Result<Arc<Vec<StarboardConfig>>> {
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
Expand Down Expand Up @@ -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?;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/core/starboard/msg_status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use twilight_model::id::Id;

use crate::{client::bot::StarboardBot, database::Message as DbMessage, errors::StarboardResult};
Expand All @@ -20,8 +18,8 @@ pub async fn get_message_status(
message: &DbMessage,
points: i32,
) -> StarboardResult<MessageStatus> {
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)
Expand Down
12 changes: 6 additions & 6 deletions src/core/starboard/reaction_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down
Loading