From 3310573ce7ef882afe960f1e15fcaee1b8b92d71 Mon Sep 17 00:00:00 2001 From: kangalioo Date: Mon, 1 Feb 2021 05:32:50 +0100 Subject: [PATCH] Remove SendSyncError and uesless constants --- src/ban.rs | 8 +++----- src/command_history.rs | 6 +++--- src/jobs.rs | 6 +++--- src/main.rs | 7 ++----- src/playground.rs | 6 +++--- src/welcome.rs | 4 ++-- 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/ban.rs b/src/ban.rs index 54ed043..f6ab342 100644 --- a/src/ban.rs +++ b/src/ban.rs @@ -1,6 +1,4 @@ -use crate::{ - api, commands::Args, db::DB, schema::bans, text::ban_message, Error, SendSyncError, HOUR, -}; +use crate::{api, commands::Args, db::DB, schema::bans, text::ban_message, Error}; use diesel::prelude::*; use serenity::{model::prelude::*, prelude::*, utils::parse_username}; use std::time::{Duration, SystemTime}; @@ -14,7 +12,7 @@ pub fn save_ban(user_id: String, guild_id: String, hours: u64) -> Result<(), Err bans::guild_id.eq(guild_id), bans::start_time.eq(SystemTime::now()), bans::end_time.eq(SystemTime::now() - .checked_add(Duration::new(hours * HOUR, 0)) + .checked_add(Duration::from_secs(hours * 3600)) .ok_or("out of range Duration for ban end_time")?), )) .execute(&conn)?; @@ -37,7 +35,7 @@ pub fn save_unban(user_id: String, guild_id: String) -> Result<(), Error> { Ok(()) } -pub fn unban_users(cx: &Context) -> Result<(), SendSyncError> { +pub fn unban_users(cx: &Context) -> Result<(), Error> { let conn = DB.get()?; let to_unban = bans::table .filter( diff --git a/src/command_history.rs b/src/command_history.rs index 3aa5d1a..a9e271c 100644 --- a/src/command_history.rs +++ b/src/command_history.rs @@ -1,12 +1,12 @@ use crate::{ commands::{Commands, PREFIXES}, - Error, SendSyncError, HOUR, + Error, }; use indexmap::IndexMap; use serenity::{model::prelude::*, prelude::*, utils::CustomMessage}; use std::time::Duration; -const MESSAGE_AGE_MAX: Duration = Duration::from_secs(HOUR); +const MESSAGE_AGE_MAX: Duration = Duration::from_secs(3600); pub struct CommandHistory; @@ -40,7 +40,7 @@ pub fn replay_message(cx: Context, ev: MessageUpdateEvent, cmds: &Commands) -> R Ok(()) } -pub fn clear_command_history(cx: &Context) -> Result<(), SendSyncError> { +pub fn clear_command_history(cx: &Context) -> Result<(), Error> { let mut data = cx.data.write(); let history = data.get_mut::().unwrap(); diff --git a/src/jobs.rs b/src/jobs.rs index 4f013b3..87bde85 100644 --- a/src/jobs.rs +++ b/src/jobs.rs @@ -1,4 +1,4 @@ -use crate::{ban::unban_users, command_history::clear_command_history, SendSyncError, HOUR}; +use crate::{ban::unban_users, command_history::clear_command_history, Error}; use serenity::client::Context; use std::{ sync::atomic::{AtomicBool, Ordering}, @@ -11,12 +11,12 @@ static JOBS_THREAD_INITIALIZED: AtomicBool = AtomicBool::new(false); pub fn start_jobs(cx: Context) { if !JOBS_THREAD_INITIALIZED.load(Ordering::SeqCst) { JOBS_THREAD_INITIALIZED.store(true, Ordering::SeqCst); - std::thread::spawn(move || -> Result<(), SendSyncError> { + std::thread::spawn(move || -> Result<(), Error> { loop { unban_users(&cx)?; clear_command_history(&cx)?; - sleep(Duration::new(HOUR, 0)); + sleep(Duration::from_secs(3600)); } }); } diff --git a/src/main.rs b/src/main.rs index e90bac9..a989d05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,10 +29,7 @@ use indexmap::IndexMap; use serde::Deserialize; use serenity::{model::prelude::*, prelude::*}; -pub type Error = Box; -pub type SendSyncError = Box; - -pub const HOUR: u64 = 3600; +pub type Error = Box; #[derive(Deserialize)] struct Config { @@ -65,7 +62,7 @@ fn init_data(config: &Config) -> Result<(), Error> { let _ = conn .build_transaction() .read_write() - .run::<_, Box, _>(|| { + .run::<_, Error, _>(|| { upsert_role("mod", &config.mod_id)?; upsert_role("talk", &config.talk_id)?; diff --git a/src/playground.rs b/src/playground.rs index d466917..583b6f5 100644 --- a/src/playground.rs +++ b/src/playground.rs @@ -44,7 +44,7 @@ enum Channel { } impl FromStr for Channel { - type Err = Box; + type Err = Error; fn from_str(s: &str) -> Result { match s { @@ -65,7 +65,7 @@ enum Edition { } impl FromStr for Edition { - type Err = Box; + type Err = Error; fn from_str(s: &str) -> Result { match s { @@ -92,7 +92,7 @@ enum Mode { } impl FromStr for Mode { - type Err = Box; + type Err = Error; fn from_str(s: &str) -> Result { match s { diff --git a/src/welcome.rs b/src/welcome.rs index 89e1e5e..52f6d20 100644 --- a/src/welcome.rs +++ b/src/welcome.rs @@ -22,7 +22,7 @@ pub fn post_message(args: &Args) -> Result<(), Error> { let _ = conn .build_transaction() .read_write() - .run::<_, Box, _>(|| { + .run::<_, Error, _>(|| { let message_id = message.id.0.to_string(); let channel_id = channel_id.0.to_string(); @@ -67,7 +67,7 @@ pub fn assign_talk_role(cx: &Context, reaction: &Reaction) -> Result<(), Error> let (msg, talk_role, me) = conn .build_transaction() .read_only() - .run::<_, Box, _>(|| { + .run::<_, Error, _>(|| { let msg: Option<_> = messages::table .filter(messages::name.eq("welcome")) .first::<(i32, String, String, String)>(&conn)