summaryrefslogtreecommitdiff
path: root/src/settings.rs
blob: 6c02e5cf9f73585e38fddbd3aac8514a58b09d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use color_eyre::eyre::{Context as _, Result};
use poise::serenity_prelude::{ChannelId, GuildId, ReactionType};
use redis::{AsyncCommands as _, Client};
use redis_macros::{FromRedisValue, ToRedisArgs};
use serde::{Deserialize, Serialize};

const ROOT_KEY: &str = "settings-v1";

#[derive(poise::ChoiceParameter)]
pub enum SettingsProperties {
	GuildId,
	PinBoardChannel,
	PinBoardWatch,
	ReactBoardChannel,
	ReactBoardRequirement,
	ReactBoardReactions,
	OptionalCommandsEnabled,
}

#[derive(Clone, Default, PartialEq, Serialize, Deserialize, FromRedisValue, ToRedisArgs)]
pub struct Settings {
	pub guild_id: GuildId,
	pub pinboard_channel: Option<ChannelId>,
	pub pinboard_watch: Option<Vec<ChannelId>>,
	pub reactboard_channel: Option<ChannelId>,
	pub reactboard_requirement: Option<u64>,
	pub reactboard_reactions: Option<Vec<ReactionType>>,
	pub optional_commands_enabled: bool,
}

impl Settings {
	pub async fn new_redis(redis: &Client, gid: &GuildId) -> Result<()> {
		let key = format!("{ROOT_KEY}:{gid}");
		let settings = Self {
			guild_id: *gid,
			optional_commands_enabled: false,
			..Default::default()
		};

		let mut con = redis.get_async_connection().await?;
		con.set(&key, settings)
			.await
			.wrap_err_with(|| format!("Couldn't set key {key} in Redis!"))?;

		Ok(())
	}

	pub async fn from_redis(redis: &Client, gid: &GuildId) -> Result<Self> {
		let key = format!("{ROOT_KEY}:{gid}");
		let mut con = redis.get_async_connection().await?;

		let settings: Settings = con
			.get(&key)
			.await
			.wrap_err_with(|| format!("Couldn't get {key} from Redis!"))?;

		Ok(settings)
	}

	pub async fn delete(&self, redis: &Client) -> Result<()> {
		let key = format!("{ROOT_KEY}:{}", self.guild_id);
		let mut con = redis.get_async_connection().await?;

		con.del(&key)
			.await
			.wrap_err_with(|| format!("Couldn't delete {key} from Redis!"))?;

		Ok(())
	}

	pub async fn save(&self, redis: &Client) -> Result<()> {
		let key = format!("{ROOT_KEY}:{}", self.guild_id);
		let mut con = redis.get_async_connection().await?;

		con.set(&key, self)
			.await
			.wrap_err_with(|| format!("Couldn't save {key} in Redis!"))?;
		Ok(())
	}

	pub fn can_use_reaction(&self, reaction: &ReactionType) -> bool {
		if let Some(reactions) = &self.reactboard_reactions {
			reactions.iter().any(|r| r == reaction)
		} else {
			false
		}
	}
}