mod ignore;
mod not_registered;
mod register;
mod un_ignore;

use diesel::prelude::*;
use serenity::framework::standard::{macros::group, CommandResult};
use serenity::{
    framework::standard::{macros::command, Args, CommandError},
    model::channel::Message,
    prelude::*,
    utils::MessageBuilder,
};

pub use self::ignore::*;
pub use self::not_registered::*;
pub use self::register::*;
pub use self::un_ignore::*;
use internal::get_user_from_discord_user;
use models::User;

#[group]
#[description = "Group for general commands"]
#[commands(ignore, register, not_registered, un_ignore)]
struct General;

fn update_ignore(conn: &MysqlConnection, msg: &Message, ignore_value: bool) -> CommandResult {
    use schema::user::dsl::ignored;

    let users: Vec<User> = msg
        .mentions
        .iter()
        .filter_map(|discord_user| get_user_from_discord_user(discord_user, conn).ok())
        .collect();

    for user in users {
        diesel::update(&user)
            .set(ignored.eq(ignore_value))
            .execute(conn)?;
    }

    Ok(())
}
