Skip to content

Commit ef941f9

Browse files
committed
clean up
1 parent e23be32 commit ef941f9

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

src/api.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::commands::{Args, Result};
2-
use crate::db::DB;
3-
use crate::schema::{bans, roles};
1+
use crate::{commands::{Args, Result}, db::DB, schema::roles, ban};
42
use diesel::prelude::*;
53
use serenity::{model::prelude::*, utils::parse_username};
64

@@ -117,21 +115,8 @@ pub(crate) fn ban(args: Args) -> Result<()> {
117115

118116
if let Some(guild) = args.msg.guild(&args.cx) {
119117
info!("Banning user from guild");
120-
121118
guild.read().ban(args.cx, UserId::from(user_id), &"all")?;
122-
123-
let conn = DB.get()?;
124-
use std::time::{Duration, SystemTime};
125-
diesel::insert_into(bans::table)
126-
.values((
127-
bans::user_id.eq(format!("{}", user_id)),
128-
bans::guild_id.eq(format!("{}", guild.read().id)),
129-
bans::start_time.eq(SystemTime::now()),
130-
bans::end_time.eq(SystemTime::now()
131-
.checked_add(Duration::new(hours * 60 * 60, 0))
132-
.ok_or("out of range Duration for ban end_time")?),
133-
))
134-
.execute(&conn)?;
119+
ban::save_ban(format!("{}", user_id), format!("{}", guild.read().id), hours)?;
135120
}
136121
}
137122
Ok(())

src/ban.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use std::{
77
time::{Duration, SystemTime},
88
};
99

10+
const HOUR: u64 = 3600;
1011
static UNBAN_THREAD_INITIALIZED: AtomicBool = AtomicBool::new(false);
1112

12-
pub(crate) fn save_ban(user_id: String, guild_id: String) -> Result<()> {
13+
pub(crate) fn save_ban(user_id: String, guild_id: String, hours: u64) -> Result<()> {
1314
info!("Recording ban for user {}", &user_id);
1415
let conn = DB.get()?;
1516
diesel::insert_into(bans::table)
@@ -18,7 +19,7 @@ pub(crate) fn save_ban(user_id: String, guild_id: String) -> Result<()> {
1819
bans::guild_id.eq(guild_id),
1920
bans::start_time.eq(SystemTime::now()),
2021
bans::end_time.eq(SystemTime::now()
21-
.checked_add(Duration::new(1 * 60, 0))
22+
.checked_add(Duration::new(hours * HOUR, 0))
2223
.ok_or("out of range Duration for ban end_time")?),
2324
))
2425
.execute(&conn)?;
@@ -37,6 +38,7 @@ pub(crate) fn save_unban(user_id: String, guild_id: String) -> Result<()> {
3738
)
3839
.set(bans::unbanned.eq(true))
3940
.execute(&conn)?;
41+
4042
Ok(())
4143
}
4244

@@ -48,13 +50,13 @@ pub(crate) fn start_unban_thread(cx: Context) {
4850
UNBAN_THREAD_INITIALIZED.store(true, Ordering::SeqCst);
4951
std::thread::spawn(move || -> std::result::Result<(), SendSyncError> {
5052
loop {
51-
sleep(Duration::new(20, 0));
53+
sleep(Duration::new(HOUR, 0));
5254
let conn = DB.get()?;
5355
let to_unban = bans::table
5456
.filter(
5557
bans::unbanned
56-
.eq(false)
57-
.and(bans::end_time.le(SystemTime::now())),
58+
.eq(false)
59+
.and(bans::end_time.le(SystemTime::now())),
5860
)
5961
.load::<(i32, String, String, bool, SystemTime, SystemTime)>(&conn)?;
6062

src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,10 @@ impl RawEventHandler for Events {
154154
println!("{}", e);
155155
}
156156
}
157-
Event::GuildBanAdd(ref ev) => {
158-
if let Err(e) = ban::save_ban(format!("{}", ev.user.id), format!("{}", ev.guild_id)) {
159-
error!("{}", e);
160-
}
161-
}
162157
Event::GuildBanRemove(ref ev) => {
163-
if let Err(e) = ban::save_unban(format!("{}", ev.user.id), format!("{}", ev.guild_id)) {
158+
if let Err(e) =
159+
ban::save_unban(format!("{}", ev.user.id), format!("{}", ev.guild_id))
160+
{
164161
error!("{}", e);
165162
}
166163
}

0 commit comments

Comments
 (0)