@@ -15,18 +15,18 @@ mod events;
15
15
mod schema;
16
16
mod state_machine;
17
17
mod tags;
18
+ mod welcome;
18
19
19
20
use crate :: db:: DB ;
20
- use crate :: schema:: { messages, roles, users} ;
21
21
use commands:: { Args , Commands } ;
22
22
use diesel:: prelude:: * ;
23
23
use events:: { EventDispatcher , MessageDispatcher } ;
24
- use serenity:: { model:: prelude:: * , utils:: parse_username, Client } ;
25
- use std:: str:: FromStr ;
24
+ use serenity:: Client ;
26
25
27
- type Result = crate :: commands:: Result < ( ) > ;
26
+ pub ( crate ) type Result = crate :: commands:: Result < ( ) > ;
28
27
29
28
fn init_data ( ) -> Result {
29
+ use crate :: schema:: roles;
30
30
info ! ( "Loading data into database" ) ;
31
31
let mod_role = std:: env:: var ( "MOD_ID" ) . map_err ( |_| "MOD_ID env var not found" ) ?;
32
32
let talk_role = std:: env:: var ( "TALK_ID" ) . map_err ( |_| "TALK_ID env var not found" ) ?;
@@ -88,16 +88,16 @@ fn app() -> Result {
88
88
89
89
// Slow mode.
90
90
// 0 seconds disables slowmode
91
- cmds. add ( "?slowmode {channel} {seconds}" , slow_mode) ;
91
+ cmds. add ( "?slowmode {channel} {seconds}" , api :: slow_mode) ;
92
92
93
93
// Kick
94
- cmds. add ( "?kick {user}" , kick) ;
94
+ cmds. add ( "?kick {user}" , api :: kick) ;
95
95
96
96
// Ban
97
- cmds. add ( "?ban {user}" , ban) ;
97
+ cmds. add ( "?ban {user}" , api :: ban) ;
98
98
99
99
// Post the welcome message to the welcome channel.
100
- cmds. add ( "?CoC {channel}" , welcome_message ) ;
100
+ cmds. add ( "?CoC {channel}" , welcome :: post_message ) ;
101
101
102
102
let menu = cmds. menu ( ) . unwrap ( ) ;
103
103
@@ -124,124 +124,3 @@ fn main() {
124
124
std:: process:: exit ( 1 ) ;
125
125
}
126
126
}
127
-
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) ) ?;
146
- }
147
- Ok ( ( ) )
148
- }
149
-
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 ( ( ) )
169
- }
170
-
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
- }
188
- }
189
- Ok ( ( ) )
190
- }
191
-
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) ?;
245
- }
246
- Ok ( ( ) )
247
- }
0 commit comments