@@ -11,22 +11,20 @@ mod api;
11
11
mod commands;
12
12
mod crates;
13
13
mod db;
14
- mod events;
15
14
mod schema;
16
15
mod state_machine;
17
16
mod tags;
17
+ mod welcome;
18
18
19
19
use crate :: db:: DB ;
20
- use crate :: schema:: { messages, roles, users} ;
21
20
use commands:: { Args , Commands } ;
22
21
use diesel:: prelude:: * ;
23
- use events:: { EventDispatcher , MessageDispatcher } ;
24
- use serenity:: { model:: prelude:: * , utils:: parse_username, Client } ;
25
- use std:: str:: FromStr ;
22
+ use serenity:: { model:: prelude:: * , prelude:: * } ;
26
23
27
- type Result = crate :: commands:: Result < ( ) > ;
24
+ pub ( crate ) type Result = crate :: commands:: Result < ( ) > ;
28
25
29
26
fn init_data ( ) -> Result {
27
+ use crate :: schema:: roles;
30
28
info ! ( "Loading data into database" ) ;
31
29
let mod_role = std:: env:: var ( "MOD_ID" ) . map_err ( |_| "MOD_ID env var not found" ) ?;
32
30
let talk_role = std:: env:: var ( "TALK_ID" ) . map_err ( |_| "TALK_ID env var not found" ) ?;
@@ -88,16 +86,16 @@ fn app() -> Result {
88
86
89
87
// Slow mode.
90
88
// 0 seconds disables slowmode
91
- cmds. add ( "?slowmode {channel} {seconds}" , slow_mode) ;
89
+ cmds. add ( "?slowmode {channel} {seconds}" , api :: slow_mode) ;
92
90
93
91
// Kick
94
- cmds. add ( "?kick {user}" , kick) ;
92
+ cmds. add ( "?kick {user}" , api :: kick) ;
95
93
96
94
// Ban
97
- cmds. add ( "?ban {user}" , ban) ;
95
+ cmds. add ( "?ban {user}" , api :: ban) ;
98
96
99
97
// Post the welcome message to the welcome channel.
100
- cmds. add ( "?CoC {channel}" , welcome_message ) ;
98
+ cmds. add ( "?CoC {channel}" , welcome :: post_message ) ;
101
99
102
100
let menu = cmds. menu ( ) . unwrap ( ) ;
103
101
@@ -106,10 +104,11 @@ fn app() -> Result {
106
104
Ok ( ( ) )
107
105
} ) ;
108
106
109
- let messages = MessageDispatcher :: new ( cmds) ;
110
-
111
- let mut client =
112
- Client :: new_with_handlers ( & token, Some ( messages) , Some ( EventDispatcher ) ) . unwrap ( ) ;
107
+ let mut client = Client :: new_with_extras ( & token, |e| {
108
+ e. event_handler ( Messages { cmds } ) ;
109
+ e. raw_event_handler ( Events ) ;
110
+ e
111
+ } ) ?;
113
112
114
113
client. start ( ) ?;
115
114
@@ -125,123 +124,31 @@ fn main() {
125
124
}
126
125
}
127
126
128
- /// Set slow mode for a channel.
129
- ///
130
- /// A `seconds` value of 0 will disable slowmode
131
- fn slow_mode ( args : Args ) -> Result {
132
- if api:: is_mod ( & args) ? {
133
- let seconds = & args
134
- . params
135
- . get ( "seconds" )
136
- . ok_or ( "unable to retrieve seconds param" ) ?
137
- . parse :: < u64 > ( ) ?;
138
-
139
- let channel_name = & args
140
- . params
141
- . get ( "channel" )
142
- . ok_or ( "unable to retrieve channel param" ) ?;
143
-
144
- info ! ( "Applying slowmode to channel {}" , & channel_name) ;
145
- ChannelId :: from_str ( channel_name) ?. edit ( & args. cx , |c| c. slow_mode_rate ( * seconds) ) ?;
127
+ struct Events ;
128
+
129
+ impl RawEventHandler for Events {
130
+ fn raw_event ( & self , cx : Context , event : Event ) {
131
+ match event {
132
+ Event :: ReactionAdd ( ref ev) => {
133
+ if let Err ( e) = welcome:: assign_talk_role ( & cx, ev) {
134
+ println ! ( "{}" , e) ;
135
+ }
136
+ }
137
+ _ => ( ) ,
138
+ }
146
139
}
147
- Ok ( ( ) )
148
140
}
149
141
150
- /// Kick a user from the guild.
151
- ///
152
- /// Requires the kick members permission
153
- fn kick ( args : Args ) -> Result {
154
- if api:: is_mod ( & args) ? {
155
- let user_id = parse_username (
156
- & args
157
- . params
158
- . get ( "user" )
159
- . ok_or ( "unable to retrieve user param" ) ?,
160
- )
161
- . ok_or ( "unable to retrieve user id" ) ?;
162
-
163
- if let Some ( guild) = args. msg . guild ( & args. cx ) {
164
- info ! ( "Kicking user from guild" ) ;
165
- guild. read ( ) . kick ( & args. cx , UserId :: from ( user_id) ) ?
166
- }
167
- }
168
- Ok ( ( ) )
142
+ struct Messages {
143
+ cmds : Commands ,
169
144
}
170
145
171
- /// Ban an user from the guild.
172
- ///
173
- /// Requires the ban members permission
174
- fn ban ( args : Args ) -> Result {
175
- if api:: is_mod ( & args) ? {
176
- let user_id = parse_username (
177
- & args
178
- . params
179
- . get ( "user" )
180
- . ok_or ( "unable to retrieve user param" ) ?,
181
- )
182
- . ok_or ( "unable to retrieve user id" ) ?;
183
-
184
- if let Some ( guild) = args. msg . guild ( & args. cx ) {
185
- info ! ( "Banning user from guild" ) ;
186
- guild. read ( ) . ban ( args. cx , UserId :: from ( user_id) , & "all" ) ?
187
- }
146
+ impl EventHandler for Messages {
147
+ fn message ( & self , cx : Context , msg : Message ) {
148
+ self . cmds . execute ( cx, msg) ;
188
149
}
189
- Ok ( ( ) )
190
- }
191
150
192
- /// Write the welcome message to the welcome channel.
193
- fn welcome_message ( args : Args ) -> Result {
194
- const WELCOME_BILLBOARD : & ' static str = "By participating in this community, you agree to follow the Rust Code of Conduct, as linked below. Please click the :white_check_mark: below to acknowledge and gain access to the channels.
195
-
196
- https://www.rust-lang.org/policies/code-of-conduct " ;
197
-
198
- if api:: is_mod ( & args) ? {
199
- let channel_name = & args
200
- . params
201
- . get ( "channel" )
202
- . ok_or ( "unable to retrieve channel param" ) ?;
203
-
204
- let channel_id = ChannelId :: from_str ( channel_name) ?;
205
- info ! ( "Posting welcome message" ) ;
206
- let message = channel_id. say ( & args. cx , WELCOME_BILLBOARD ) ?;
207
- let bot_id = & message. author . id ;
208
-
209
- let conn = DB . get ( ) ?;
210
-
211
- let _ = conn
212
- . build_transaction ( )
213
- . read_write ( )
214
- . run :: < _ , Box < dyn std:: error:: Error > , _ > ( || {
215
- let message_id = message. id . 0 . to_string ( ) ;
216
- let channel_id = channel_id. 0 . to_string ( ) ;
217
-
218
- diesel:: insert_into ( messages:: table)
219
- . values ( (
220
- messages:: name. eq ( "welcome" ) ,
221
- messages:: message. eq ( & message_id) ,
222
- messages:: channel. eq ( & channel_id) ,
223
- ) )
224
- . on_conflict ( messages:: name)
225
- . do_update ( )
226
- . set ( (
227
- messages:: message. eq ( & message_id) ,
228
- messages:: channel. eq ( & channel_id) ,
229
- ) )
230
- . execute ( & conn) ?;
231
-
232
- let user_id = & bot_id. to_string ( ) ;
233
-
234
- diesel:: insert_into ( users:: table)
235
- . values ( ( users:: user_id. eq ( user_id) , users:: name. eq ( "me" ) ) )
236
- . on_conflict ( users:: name)
237
- . do_update ( )
238
- . set ( ( users:: name. eq ( "me" ) , users:: user_id. eq ( user_id) ) )
239
- . execute ( & conn) ?;
240
- Ok ( ( ) )
241
- } ) ?;
242
-
243
- let white_check_mark = ReactionType :: from ( "✅" ) ;
244
- message. react ( args. cx , white_check_mark) ?;
151
+ fn ready ( & self , _: Context , ready : Ready ) {
152
+ info ! ( "{} connected to discord" , ready. user. name) ;
245
153
}
246
- Ok ( ( ) )
247
154
}
0 commit comments