Skip to content

Commit

Permalink
feat: use serde for de/serializing GuildId
Browse files Browse the repository at this point in the history
  • Loading branch information
seqre committed Oct 31, 2023
1 parent b165bcf commit 8656bf9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
24 changes: 7 additions & 17 deletions chombot/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,9 @@ use std::path::PathBuf;
use async_trait::async_trait;
use chombot_common::tournaments_watcher::notifier::TournamentWatcherChannelListProvider;
use log::info;
use poise::serenity_prelude::ChannelId;
use poise::serenity_prelude::{ChannelId, GuildId};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct GuildId(String);

impl GuildId {
#[must_use]
pub fn new(value: u64) -> Self {
Self(value.to_string())
}
}

#[derive(Clone, Default, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Config {
/// Guild-specific configs
Expand All @@ -32,18 +21,19 @@ pub struct Config {
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Default)]
pub struct GuildConfig {
/// Tournaments watcher channel ID
pub tournaments_watcher_channel_id: Option<u64>,
pub tournaments_watcher_channel_id: Option<ChannelId>,
}

#[async_trait]
impl TournamentWatcherChannelListProvider for ChombotConfig {
type TournamentWatcherChannelList = Vec<ChannelId>;

async fn tournament_watcher_channels(&self) -> Self::TournamentWatcherChannelList {
let channel_ids =
self.config.guilds.iter().filter_map(|(_, config)| {
config.tournaments_watcher_channel_id.map(ChannelId::from)
});
let channel_ids = self
.config
.guilds
.iter()
.filter_map(|(_, config)| config.tournaments_watcher_channel_id);

channel_ids.collect()
}
Expand Down
6 changes: 3 additions & 3 deletions chombot/src/tournament_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::anyhow;
use poise::serenity_prelude::ChannelId;
use poise::serenity_prelude::{ChannelId, GuildId};

use crate::{config, PoiseContext};

Expand All @@ -16,9 +16,9 @@ pub async fn tournament_watcher(
config
.config_mut()
.guilds
.entry(config::GuildId::new(guild.0))
.entry(guild)
.or_default()
.tournaments_watcher_channel_id = channel.map(|x| x.0);
.tournaments_watcher_channel_id = channel;
}

let reply_content = channel.as_ref().map_or_else(
Expand Down

0 comments on commit 8656bf9

Please sign in to comment.