1- use super :: CommandHistory ;
21use crate :: {
3- api,
4- commands:: { Args , Result } ,
5- db:: DB ,
6- schema:: bans,
7- text:: ban_message,
2+ api, commands:: Args , db:: DB , schema:: bans, text:: ban_message, Error , SendSyncError , HOUR ,
83} ;
94use diesel:: prelude:: * ;
105use serenity:: { model:: prelude:: * , prelude:: * , utils:: parse_username} ;
11- use std:: {
12- sync:: atomic:: { AtomicBool , Ordering } ,
13- thread:: sleep,
14- time:: { Duration , SystemTime } ,
15- } ;
16-
17- const HOUR : u64 = 3600 ;
18- static UNBAN_THREAD_INITIALIZED : AtomicBool = AtomicBool :: new ( false ) ;
6+ use std:: time:: { Duration , SystemTime } ;
197
20- pub ( crate ) fn save_ban ( user_id : String , guild_id : String , hours : u64 ) -> Result < ( ) > {
8+ pub ( crate ) fn save_ban ( user_id : String , guild_id : String , hours : u64 ) -> Result < ( ) , Error > {
219 info ! ( "Recording ban for user {}" , & user_id) ;
2210 let conn = DB . get ( ) ?;
2311 diesel:: insert_into ( bans:: table)
@@ -34,7 +22,7 @@ pub(crate) fn save_ban(user_id: String, guild_id: String, hours: u64) -> Result<
3422 Ok ( ( ) )
3523}
3624
37- pub ( crate ) fn save_unban ( user_id : String , guild_id : String ) -> Result < ( ) > {
25+ pub ( crate ) fn save_unban ( user_id : String , guild_id : String ) -> Result < ( ) , Error > {
3826 info ! ( "Recording unban for user {}" , & user_id) ;
3927 let conn = DB . get ( ) ?;
4028 diesel:: update ( bans:: table)
@@ -49,49 +37,30 @@ pub(crate) fn save_unban(user_id: String, guild_id: String) -> Result<()> {
4937 Ok ( ( ) )
5038}
5139
52- pub ( crate ) fn start_cleanup_thread ( cx : Context ) {
40+ pub ( crate ) fn unban_users ( cx : & Context ) -> Result < ( ) , SendSyncError > {
5341 use std:: str:: FromStr ;
54- if !UNBAN_THREAD_INITIALIZED . load ( Ordering :: SeqCst ) {
55- UNBAN_THREAD_INITIALIZED . store ( true , Ordering :: SeqCst ) ;
56- type SendSyncError = Box < dyn std:: error:: Error + Send + Sync > ;
57- std:: thread:: spawn ( move || -> std:: result:: Result < ( ) , SendSyncError > {
58- loop {
59- let conn = DB . get ( ) ?;
60- let to_unban = bans:: table
61- . filter (
62- bans:: unbanned
63- . eq ( false )
64- . and ( bans:: end_time. le ( SystemTime :: now ( ) ) ) ,
65- )
66- . load :: < ( i32 , String , String , bool , SystemTime , SystemTime ) > ( & conn) ?;
67-
68- for row in & to_unban {
69- let guild_id = GuildId :: from ( u64:: from_str ( & row. 2 ) ?) ;
70- info ! ( "Unbanning user {}" , & row. 1 ) ;
71- guild_id. unban ( & cx, u64:: from_str ( & row. 1 ) ?) ?;
72- }
73-
74- let mut data = cx. data . write ( ) ;
75- let history = data. get_mut :: < CommandHistory > ( ) . unwrap ( ) ;
76-
77- // always keep the last command in history
78- if history. len ( ) > 0 {
79- info ! ( "Clearing command history" ) ;
80- history. drain ( ..history. len ( ) - 1 ) ;
81- }
82-
83- drop ( data) ;
84-
85- sleep ( Duration :: new ( HOUR , 0 ) ) ;
86- }
87- } ) ;
42+
43+ let conn = DB . get ( ) ?;
44+ let to_unban = bans:: table
45+ . filter (
46+ bans:: unbanned
47+ . eq ( false )
48+ . and ( bans:: end_time. le ( SystemTime :: now ( ) ) ) ,
49+ )
50+ . load :: < ( i32 , String , String , bool , SystemTime , SystemTime ) > ( & conn) ?;
51+
52+ for row in & to_unban {
53+ let guild_id = GuildId :: from ( u64:: from_str ( & row. 2 ) ?) ;
54+ info ! ( "Unbanning user {}" , & row. 1 ) ;
55+ guild_id. unban ( & cx, u64:: from_str ( & row. 1 ) ?) ?;
8856 }
57+ Ok ( ( ) )
8958}
9059
9160/// Temporarily ban an user from the guild.
9261///
9362/// Requires the ban members permission
94- pub ( crate ) fn temp_ban ( args : Args ) -> Result < ( ) > {
63+ pub ( crate ) fn temp_ban ( args : Args ) -> Result < ( ) , Error > {
9564 let user_id = parse_username (
9665 & args
9766 . params
@@ -131,7 +100,7 @@ pub(crate) fn temp_ban(args: Args) -> Result<()> {
131100 Ok ( ( ) )
132101}
133102
134- pub ( crate ) fn help ( args : Args ) -> Result < ( ) > {
103+ pub ( crate ) fn help ( args : Args ) -> Result < ( ) , Error > {
135104 let hours = 24 ;
136105 let reason = "violating the code of conduct" ;
137106
0 commit comments