Skip to content

Commit be16052

Browse files
committed
Redo the ban infra and revert the extra change I added earlier
1 parent f635d3f commit be16052

File tree

3 files changed

+81
-48
lines changed

3 files changed

+81
-48
lines changed

src/ban.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use crate::{commands::Result, db::DB, schema::bans};
2+
use diesel::prelude::*;
3+
use serenity::{model::prelude::*, prelude::*};
4+
use std::{
5+
sync::atomic::{AtomicBool, Ordering},
6+
thread::sleep,
7+
time::{Duration, SystemTime},
8+
};
9+
10+
static UNBAN_THREAD_INITIALIZED: AtomicBool = AtomicBool::new(false);
11+
12+
pub(crate) fn save_ban(user_id: String, guild_id: String) -> Result<()> {
13+
info!("Recording ban for user {}", &user_id);
14+
let conn = DB.get()?;
15+
diesel::insert_into(bans::table)
16+
.values((
17+
bans::user_id.eq(user_id),
18+
bans::guild_id.eq(guild_id),
19+
bans::start_time.eq(SystemTime::now()),
20+
bans::end_time.eq(SystemTime::now()
21+
.checked_add(Duration::new(1 * 60, 0))
22+
.ok_or("out of range Duration for ban end_time")?),
23+
))
24+
.execute(&conn)?;
25+
26+
Ok(())
27+
}
28+
29+
pub(crate) fn save_unban(user_id: String, guild_id: String) -> Result<()> {
30+
info!("Recording unban for user {}", &user_id);
31+
let conn = DB.get()?;
32+
diesel::update(bans::table)
33+
.filter(
34+
bans::user_id
35+
.eq(user_id)
36+
.and(bans::guild_id.eq(guild_id).and(bans::unbanned.eq(false))),
37+
)
38+
.set(bans::unbanned.eq(true))
39+
.execute(&conn)?;
40+
Ok(())
41+
}
42+
43+
type SendSyncError = Box<dyn std::error::Error + Send + Sync>;
44+
45+
pub(crate) fn start_unban_thread(cx: Context) {
46+
use std::str::FromStr;
47+
if !UNBAN_THREAD_INITIALIZED.load(Ordering::SeqCst) {
48+
UNBAN_THREAD_INITIALIZED.store(true, Ordering::SeqCst);
49+
std::thread::spawn(move || -> std::result::Result<(), SendSyncError> {
50+
loop {
51+
sleep(Duration::new(20, 0));
52+
let conn = DB.get()?;
53+
let to_unban = bans::table
54+
.filter(
55+
bans::unbanned
56+
.eq(false)
57+
.and(bans::end_time.le(SystemTime::now())),
58+
)
59+
.load::<(i32, String, String, bool, SystemTime, SystemTime)>(&conn)?;
60+
61+
for row in &to_unban {
62+
let guild_id = GuildId::from(u64::from_str(&row.2)?);
63+
info!("Unbanning user {}", &row.1);
64+
guild_id.unban(&cx, u64::from_str(&row.1)?)?;
65+
}
66+
}
67+
});
68+
}
69+
}

src/commands.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ impl Commands {
100100
fn add_space(state_machine: &mut StateMachine, mut state: usize, i: usize) -> usize {
101101
if i > 0 {
102102
state = state_machine.add(state, CharacterSet::from_char(' '));
103-
state_machine.add_next_state(state, state);
104103
}
105104
state
106105
}

src/main.rs

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate diesel_migrations;
88
extern crate log;
99

1010
mod api;
11+
mod ban;
1112
mod commands;
1213
mod crates;
1314
mod db;
@@ -110,9 +111,6 @@ fn app() -> Result {
110111
// Kick
111112
cmds.add("?kick {user}", api::kick);
112113

113-
// Ban
114-
cmds.add("?ban {user} {hours}", api::ban);
115-
116114
// Post the welcome message to the welcome channel.
117115
cmds.add("?CoC {channel}", welcome::post_message);
118116

@@ -153,6 +151,16 @@ impl RawEventHandler for Events {
153151
println!("{}", e);
154152
}
155153
}
154+
Event::GuildBanAdd(ref ev) => {
155+
if let Err(e) = ban::save_ban(format!("{}", ev.user.id), format!("{}", ev.guild_id)) {
156+
error!("{}", e);
157+
}
158+
}
159+
Event::GuildBanRemove(ref ev) => {
160+
if let Err(e) = ban::save_unban(format!("{}", ev.user.id), format!("{}", ev.guild_id)) {
161+
error!("{}", e);
162+
}
163+
}
156164
_ => (),
157165
}
158166
}
@@ -169,49 +177,6 @@ impl EventHandler for Messages {
169177

170178
fn ready(&self, context: Context, ready: Ready) {
171179
info!("{} connected to discord", ready.user.name);
172-
std::thread::spawn(
173-
move || -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
174-
use std::{
175-
thread::sleep,
176-
time::{Duration, SystemTime},
177-
};
178-
let cx = context;
179-
180-
use crate::schema::bans;
181-
use std::str::FromStr;
182-
183-
let conn = DB.get()?;
184-
loop {
185-
sleep(Duration::new(3600, 0));
186-
let _ = conn.build_transaction().read_write().run::<_, Box<
187-
dyn std::error::Error + Send + Sync,
188-
>, _>(
189-
|| {
190-
let to_unban = bans::table
191-
.filter(
192-
bans::unbanned
193-
.eq(false)
194-
.and(bans::end_time.le(SystemTime::now())),
195-
)
196-
.load::<(i32, String, String, bool, SystemTime, SystemTime)>(
197-
&conn,
198-
)?;
199-
200-
for row in &to_unban {
201-
let guild_id = GuildId::from(u64::from_str(&row.2)?);
202-
guild_id.unban(&cx, u64::from_str(&row.1)?)?;
203-
diesel::update(bans::table)
204-
.filter(bans::id.eq(&row.0))
205-
.set(bans::unbanned.eq(true))
206-
.execute(&conn)?;
207-
}
208-
209-
dbg!(to_unban);
210-
Ok(())
211-
},
212-
)?;
213-
}
214-
},
215-
);
180+
ban::start_unban_thread(context);
216181
}
217182
}

0 commit comments

Comments
 (0)