From c7c4f3952936acf1594442aa06f40d82cbc3e5c1 Mon Sep 17 00:00:00 2001
From: Timothy du Heaume <timothy.duheaume@gmail.com>
Date: Thu, 30 Jan 2020 20:30:04 +0900
Subject: [PATCH] load config from a file

---
 Cargo.toml           |  2 ++
 src/config.rs        | 34 +++++++++++++---------------------
 src/config.test.rs   | 39 ---------------------------------------
 src/config.test.toml | 23 +++++++++++++++++++++++
 src/config.toml      | 23 +++++++++++++++++++++++
 src/config.ucc.rs    | 39 ---------------------------------------
 src/config.ucc.toml  | 24 ++++++++++++++++++++++++
 src/main.rs          |  2 +-
 8 files changed, 86 insertions(+), 100 deletions(-)
 delete mode 100644 src/config.test.rs
 create mode 100644 src/config.test.toml
 create mode 100644 src/config.toml
 delete mode 100644 src/config.ucc.rs
 create mode 100644 src/config.ucc.toml

diff --git a/Cargo.toml b/Cargo.toml
index 45e271a..1b9c564 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,3 +10,5 @@ rand = "^0.7.2"
 lazy_static = "^1.4.0"
 log = "^0.4.8"
 simplelog = "^0.7.4"
+toml = "^0.5.6"
+serde = "^1.0.104"
diff --git a/src/config.rs b/src/config.rs
index 8e4246e..d7aad5d 120000
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,28 +1,20 @@
 use serenity;
+use std::fs;
+use serde::Deserialize;
+use toml;
 
-pub const CONFIG: UccbotConfig = UccbotConfig {
-    discord_token: include_str!("discord_token"),
-    server_id: 606351521117896704,
-    main_channel: serenity::model::id::ChannelId(606351521117896706),
-    welcome_channel: serenity::model::id::ChannelId(606351613816209418),
-    announcement_channel: serenity::model::id::ChannelId(606351521117896706),
-    bot_id: 607078903969742848,
-    vote_pool_size: 2,
-    vote_role: 607478818038480937,
-    tiebreaker_role: 607509283483025409,
-    unregistered_member_role: 608282247350714408,
-    registered_member_role: 608282133118582815,
-    command_prefix: "!",
-    for_vote: "👍",
-    against_vote: "👎",
-    abstain_vote: "🙊",
-    approve_react: "⬆",
-    disapprove_react: "⬇",
-    unsure_react: "❔",
-};
+lazy_static! {
+    static ref CONFIG_FILE: String = fs::read_to_string("config.toml").unwrap();
+}
+
+lazy_static! {
+    pub static ref CONFIG: UccbotConfig = toml::from_str(&CONFIG_FILE).unwrap();
+}
+
+pub static DISCORD_TOKEN: &str = include_str!("discord_token");
 
+#[derive(Deserialize)]
 pub struct UccbotConfig {
-    pub discord_token: &'static str,
     pub server_id: u64,
     // #general
     pub main_channel: serenity::model::id::ChannelId,
diff --git a/src/config.test.rs b/src/config.test.rs
deleted file mode 100644
index 5fc3bb7..0000000
--- a/src/config.test.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-use serenity;
-
-pub static DISCORD_TOKEN: &str = include_str!("discord_token");
-
-pub static SERVER_ID: u64 = 606351521117896704;
-// #general
-pub static MAIN_CHANNEL: serenity::model::id::ChannelId =
-    serenity::model::id::ChannelId(606351521117896706);
-// #the-corner
-pub static WELCOME_CHANNEL: serenity::model::id::ChannelId =
-    serenity::model::id::ChannelId(606351613816209418);
-// #general
-pub static ANNOUNCEMENT_CHANNEL: serenity::model::id::ChannelId =
-    serenity::model::id::ChannelId(606351521117896706);
-
-pub static BOT_ID: u64 = 607078903969742848;
-
-pub static VOTE_POOL_SIZE: i8 = 2;
-pub static VOTE_ROLE: u64 = 607478818038480937;
-pub static TIEBREAKER_ROLE: u64 = 607509283483025409;
-pub static UNREGISTERED_MEMBER_ROLE: u64 = 608282247350714408;
-pub static REGISTERED_MEMBER_ROLE: u64 = 608282133118582815;
-
-pub static COMMAND_PREFIX: &str = "!";
-
-pub static FOR_VOTE: &str = "👍";
-pub static AGAINST_VOTE: &str = "👎";
-pub static ABSTAIN_VOTE: &str = "🙊";
-pub static APPROVE_REACT: &str = "⬆";
-pub static DISAPPROVE_REACT: &str = "⬇";
-pub static UNSURE_REACT: &str = "❔";
-pub static ALLOWED_REACTS: &[&'static str] = &[
-    FOR_VOTE,
-    AGAINST_VOTE,
-    ABSTAIN_VOTE,
-    APPROVE_REACT,
-    DISAPPROVE_REACT,
-    UNSURE_REACT,
-];
diff --git a/src/config.test.toml b/src/config.test.toml
new file mode 100644
index 0000000..bb0b38e
--- /dev/null
+++ b/src/config.test.toml
@@ -0,0 +1,23 @@
+server_id = 606351521117896704
+#general
+main_channel = 606351521117896706
+#the-corner
+welcome_channel = 606351613816209418
+#general
+announcement_channel = 606351521117896706
+
+bot_id = 607078903969742848
+
+vote_pool_size = 2
+vote_role = 607478818038480937
+tiebreaker_role = 607509283483025409
+unregistered_member_role = 608282247350714408
+registered_member_role = 608282133118582815
+command_prefix = "!"
+
+for_vote = "👍"
+against_vote = "👎"
+abstain_vote = "🙊"
+approve_react = "⬆"
+disapprove_react = "⬇"
+unsure_react = "❔"
diff --git a/src/config.toml b/src/config.toml
new file mode 100644
index 0000000..bb0b38e
--- /dev/null
+++ b/src/config.toml
@@ -0,0 +1,23 @@
+server_id = 606351521117896704
+#general
+main_channel = 606351521117896706
+#the-corner
+welcome_channel = 606351613816209418
+#general
+announcement_channel = 606351521117896706
+
+bot_id = 607078903969742848
+
+vote_pool_size = 2
+vote_role = 607478818038480937
+tiebreaker_role = 607509283483025409
+unregistered_member_role = 608282247350714408
+registered_member_role = 608282133118582815
+command_prefix = "!"
+
+for_vote = "👍"
+against_vote = "👎"
+abstain_vote = "🙊"
+approve_react = "⬆"
+disapprove_react = "⬇"
+unsure_react = "❔"
diff --git a/src/config.ucc.rs b/src/config.ucc.rs
deleted file mode 100644
index 58aa01c..0000000
--- a/src/config.ucc.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-use serenity;
-
-pub static DISCORD_TOKEN: &str = include_str!("discord_token");
-
-pub static SERVER_ID: u64 = 264401248676085760;
-// #ucc
-pub static MAIN_CHANNEL: serenity::model::id::ChannelId =
-    serenity::model::id::ChannelId(264401248676085760);
-// #welcome
-pub static WELCOME_CHANNEL: serenity::model::id::ChannelId =
-    serenity::model::id::ChannelId(606750983699300372);
-// #committee
-pub static ANNOUNCEMENT_CHANNEL: serenity::model::id::ChannelId =
-    serenity::model::id::ChannelId(264411219627212801);
-
-pub static BOT_ID: u64 = 635407267881156618;
-
-pub static VOTE_POOL_SIZE: i8 = 7;
-pub static VOTE_ROLE: u64 = 269817189966544896;
-pub static TIEBREAKER_ROLE: u64 = 635370432568098817;
-pub static UNREGISTERED_MEMBER_ROLE: u64 = 0; // does not exist
-pub static REGISTERED_MEMBER_ROLE: u64 = 0; // does not exist
-
-pub static COMMAND_PREFIX: &str = "!";
-
-pub static FOR_VOTE: &str = "👍";
-pub static AGAINST_VOTE: &str = "👎";
-pub static ABSTAIN_VOTE: &str = "🙊";
-pub static APPROVE_REACT: &str = "⬆";
-pub static DISAPPROVE_REACT: &str = "⬇";
-pub static UNSURE_REACT: &str = "❔";
-pub static ALLOWED_REACTS: &[&'static str] = &[
-    FOR_VOTE,
-    AGAINST_VOTE,
-    ABSTAIN_VOTE,
-    APPROVE_REACT,
-    DISAPPROVE_REACT,
-    UNSURE_REACT,
-];
diff --git a/src/config.ucc.toml b/src/config.ucc.toml
new file mode 100644
index 0000000..2c995db
--- /dev/null
+++ b/src/config.ucc.toml
@@ -0,0 +1,24 @@
+server_id = 264401248676085760
+#ucc
+main_channel    = 264401248676085760
+#welcome
+welcome_channel    = 606750983699300372
+#committee
+announcement_channel    = 264411219627212801
+
+bot_id = 635407267881156618
+
+vote_pool_size = 7
+vote_role = 269817189966544896
+tiebreaker_role = 635370432568098817
+unregistered_member_role = 0 # does not exist
+registered_member_role = 0 # does not exist
+
+command_prefix = "!"
+
+for_vote = "👍"
+against_vote = "👎"
+abstain_vote = "🙊"
+approve_react = "⬆"
+disapprove_react = "⬇"
+unsure_react = "❔"
diff --git a/src/main.rs b/src/main.rs
index bc28fa4..53cb2ff 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -155,7 +155,7 @@ fn main() {
 
 
     // Configure the client with your Discord bot token in the environment.
-    let token = CONFIG.discord_token;
+    let token = config::DISCORD_TOKEN;
 
     // Create a new instance of the Client, logging in as a bot. This will
     // automatically prepend your bot token with "Bot ", which is a requirement
-- 
GitLab