@@ -19,17 +19,25 @@ mod welcome;
19
19
use crate :: db:: DB ;
20
20
use commands:: { Args , Commands } ;
21
21
use diesel:: prelude:: * ;
22
+ use envy;
23
+ use serde:: Deserialize ;
22
24
use serenity:: { model:: prelude:: * , prelude:: * } ;
23
25
24
26
pub ( crate ) type Result = crate :: commands:: Result < ( ) > ;
25
27
26
- fn init_data ( ) -> Result {
28
+ #[ derive( Deserialize ) ]
29
+ struct Config {
30
+ tags : bool ,
31
+ crates : bool ,
32
+ discord_token : String ,
33
+ mod_id : String ,
34
+ talk_id : String ,
35
+ wg_and_teams_id : Option < String > ,
36
+ }
37
+
38
+ fn init_data ( config : & Config ) -> Result {
27
39
use crate :: schema:: roles;
28
40
info ! ( "Loading data into database" ) ;
29
- let mod_role = std:: env:: var ( "MOD_ID" ) . map_err ( |_| "MOD_ID env var not found" ) ?;
30
- let talk_role = std:: env:: var ( "TALK_ID" ) . map_err ( |_| "TALK_ID env var not found" ) ?;
31
- let wg_and_teams_role =
32
- std:: env:: var ( "WG_AND_TEAMS_ID" ) . map_err ( |_| "WG_AND_TEAMS_ID env var not found" ) ?;
33
41
34
42
let conn = DB . get ( ) ?;
35
43
@@ -48,9 +56,16 @@ fn init_data() -> Result {
48
56
. build_transaction ( )
49
57
. read_write ( )
50
58
. run :: < _ , Box < dyn std:: error:: Error > , _ > ( || {
51
- upsert_role ( "mod" , & mod_role) ?;
52
- upsert_role ( "talk" , & talk_role) ?;
53
- upsert_role ( "wg_and_teams" , & wg_and_teams_role) ?;
59
+ upsert_role ( "mod" , & config. mod_id ) ?;
60
+ upsert_role ( "talk" , & config. talk_id ) ?;
61
+
62
+ if config. tags || config. crates {
63
+ let wg_and_teams_role = config
64
+ . wg_and_teams_id
65
+ . as_ref ( )
66
+ . ok_or_else ( || "missing value for field wg_and_teams_id.\n \n If you enabled tags or crates then you need the WG_AND_TEAMS_ID env var." ) ?;
67
+ upsert_role ( "wg_and_teams" , & wg_and_teams_role) ?;
68
+ }
54
69
55
70
Ok ( ( ) )
56
71
} ) ?;
@@ -59,30 +74,34 @@ fn init_data() -> Result {
59
74
}
60
75
61
76
fn app ( ) -> Result {
77
+ let config = envy:: from_env :: < Config > ( ) ?;
78
+
62
79
info ! ( "starting..." ) ;
63
- let token = std:: env:: var ( "DISCORD_TOKEN" )
64
- . map_err ( |_| "missing environment variable: DISCORD_TOKEN" ) ?;
65
80
66
81
let _ = db:: run_migrations ( ) ?;
67
82
68
- let _ = init_data ( ) ?;
83
+ let _ = init_data ( & config ) ?;
69
84
70
85
let mut cmds = Commands :: new ( ) ;
71
86
72
- // Tags
73
- cmds. add ( "?tags delete {key}" , tags:: delete) ;
74
- cmds. add ( "?tags create {key} value..." , tags:: post) ;
75
- cmds. add ( "?tags help" , tags:: help) ;
76
- cmds. add ( "?tags" , tags:: get_all) ;
77
- cmds. add ( "?tag {key}" , tags:: get) ;
87
+ if config. tags {
88
+ // Tags
89
+ cmds. add ( "?tags delete {key}" , tags:: delete) ;
90
+ cmds. add ( "?tags create {key} value..." , tags:: post) ;
91
+ cmds. add ( "?tags help" , tags:: help) ;
92
+ cmds. add ( "?tags" , tags:: get_all) ;
93
+ cmds. add ( "?tag {key}" , tags:: get) ;
94
+ }
78
95
79
- // crates.io
80
- cmds. add ( "?crate help" , crates:: help) ;
81
- cmds. add ( "?crate query..." , crates:: search) ;
96
+ if config. crates {
97
+ // crates.io
98
+ cmds. add ( "?crate help" , crates:: help) ;
99
+ cmds. add ( "?crate query..." , crates:: search) ;
82
100
83
- // docs.rs
84
- cmds. add ( "?docs help" , crates:: doc_help) ;
85
- cmds. add ( "?docs query..." , crates:: doc_search) ;
101
+ // docs.rs
102
+ cmds. add ( "?docs help" , crates:: doc_help) ;
103
+ cmds. add ( "?docs query..." , crates:: doc_search) ;
104
+ }
86
105
87
106
// Slow mode.
88
107
// 0 seconds disables slowmode
@@ -104,7 +123,7 @@ fn app() -> Result {
104
123
Ok ( ( ) )
105
124
} ) ;
106
125
107
- let mut client = Client :: new_with_extras ( & token , |e| {
126
+ let mut client = Client :: new_with_extras ( & config . discord_token , |e| {
108
127
e. event_handler ( Messages { cmds } ) ;
109
128
e. raw_event_handler ( Events ) ;
110
129
e
@@ -118,8 +137,8 @@ fn app() -> Result {
118
137
fn main ( ) {
119
138
env_logger:: init ( ) ;
120
139
121
- if let Err ( err ) = app ( ) {
122
- eprintln ! ( "error: {}" , err ) ;
140
+ if let Err ( e ) = app ( ) {
141
+ error ! ( "{}" , e ) ;
123
142
std:: process:: exit ( 1 ) ;
124
143
}
125
144
}
0 commit comments