Added back in admin commands
This commit is contained in:
6 files changed
+166
-255
No files matched your search
+1
-1
@@ -4,4 +4,4 @@ pub mod insult_compliment;
|
||||
pub mod lil_fren;
|
||||
pub mod motivation;
|
||||
pub mod random;
|
||||
//pub mod task;
|
||||
pub mod task;
|
||||
+40
-63
@@ -1,14 +1,15 @@
|
||||
use crate::config::GlobalData;
|
||||
//use crate::discord::shop::restock_shop;
|
||||
use crate::discord::Context;
|
||||
use crate::error::Error;
|
||||
use crate::models::birthday::BirthdayEntry;
|
||||
use crate::models::insult_compliment::{RandomResponseTemplate, ResponseType};
|
||||
use chrono::{Days, Duration, TimeZone, Timelike, Utc};
|
||||
use j_db::database::Database;
|
||||
use j_db::model::JdbModel;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use log::{error, info};
|
||||
use poise::serenity_prelude::all::Mentionable;
|
||||
use poise::
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq)]
|
||||
pub enum TaskType {
|
||||
@@ -79,14 +80,11 @@ impl Task {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_reoccurring_tasks(ctx: &Context) -> Result<(), Error> {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
Task::add_task(&global_data.db, TaskType::CheckBirthdays, Utc::now())?;
|
||||
Task::add_task(&global_data.db, TaskType::RestockShop, Utc::now())?;
|
||||
pub async fn create_reoccurring_tasks(ctx: Context<'_>) -> Result<(), Error> {
|
||||
Task::add_task(&ctx.data().db, TaskType::CheckBirthdays, Utc::now())?;
|
||||
Task::add_task(&ctx.data().db, TaskType::RestockShop, Utc::now())?;
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
&ctx.data().db,
|
||||
TaskType::HandleReload,
|
||||
Utc::now() + Duration::hours(1),
|
||||
)?;
|
||||
@@ -94,63 +92,54 @@ impl Task {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run_tasks(ctx: &Context) -> Result<(), Error> {
|
||||
let mut data = ctx.data.write().await;
|
||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
||||
|
||||
let active_tasks: Vec<Task> = global_data
|
||||
pub async fn run_tasks(ctx: Context<'_>) -> Result<(), Error> {
|
||||
let active_tasks: Vec<Task> = ctx
|
||||
.data()
|
||||
.db
|
||||
.filter(|_, task: &Task| task.time < chrono::Utc::now())?
|
||||
.filter(|_, task: &Task| task.time < Utc::now())?
|
||||
.collect();
|
||||
|
||||
for task in active_tasks {
|
||||
match task.task_type {
|
||||
TaskType::RemoveRole { user_id, role_id } => {
|
||||
let user = global_data
|
||||
.cfg
|
||||
.guild_id
|
||||
.member(&ctx.http, user_id)
|
||||
.await
|
||||
.unwrap();
|
||||
let user = ctx.data().cfg.guild_id.member(ctx.http(), user_id).await?;
|
||||
|
||||
println!("Removing role {} from {}", role_id, user.display_name());
|
||||
user.remove_role(&ctx.http, role_id).await.unwrap();
|
||||
info!("Removing role {} from {}", role_id, user.display_name());
|
||||
user.remove_role(ctx.http(), role_id).await?;
|
||||
}
|
||||
TaskType::CheckBirthdays => {
|
||||
println!("Checking Birthdays");
|
||||
info!("Checking Birthdays");
|
||||
let todays_birthdays = BirthdayEntry::todays_birthdays(
|
||||
&global_data.db,
|
||||
&ctx.data().db,
|
||||
chrono::Utc::now().date_naive(),
|
||||
)
|
||||
.unwrap();
|
||||
)?;
|
||||
|
||||
for birth in todays_birthdays {
|
||||
if let Ok(user) = global_data
|
||||
if let Ok(user) = ctx
|
||||
.data()
|
||||
.cfg
|
||||
.guild_id
|
||||
.member(&ctx.http, birth.discord_id)
|
||||
.member(ctx.http(), birth.discord_id)
|
||||
.await
|
||||
{
|
||||
global_data
|
||||
ctx.data()
|
||||
.cfg
|
||||
.announcement_channel
|
||||
.say(&ctx.http, format!("Happy birthday {}!", user.mention()))
|
||||
.await
|
||||
.unwrap();
|
||||
.say(ctx.http(), format!("Happy birthday {}!", user.mention()))
|
||||
.await?;
|
||||
|
||||
let compliment = RandomResponseTemplate::get_random_response(
|
||||
&global_data.db,
|
||||
&ctx.data().db,
|
||||
ResponseType::Compliment,
|
||||
user.display_name(),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
global_data
|
||||
)?
|
||||
.unwrap_or("I couldn't think of anything funny tbh...".to_string());
|
||||
|
||||
ctx.data()
|
||||
.cfg
|
||||
.announcement_channel
|
||||
.say(&ctx.http, compliment)
|
||||
.await
|
||||
.unwrap();
|
||||
.say(ctx.http(), compliment)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,44 +155,32 @@ impl Task {
|
||||
.unwrap();
|
||||
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
&ctx.data().db,
|
||||
TaskType::CheckBirthdays,
|
||||
next_check.with_timezone(&Utc),
|
||||
)?;
|
||||
}
|
||||
TaskType::HandleReload => {
|
||||
println!("Reloading config...");
|
||||
let res = global_data.reload().await;
|
||||
|
||||
match res {
|
||||
Ok(_) => println!("Finished reloading config!"),
|
||||
Err(err) => println!("Error reloading config: {:?}", err),
|
||||
}
|
||||
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
TaskType::HandleReload,
|
||||
Utc::now() + Duration::hours(1),
|
||||
)?;
|
||||
// Do nothing for now
|
||||
}
|
||||
TaskType::RestockShop => {
|
||||
println!("Restocking Shop...");
|
||||
let res = restock_shop(ctx, global_data).await;
|
||||
info!("Restocking Shop...");
|
||||
//let res = restock_shop(ctx, global_data).await;
|
||||
|
||||
match res {
|
||||
Ok(_) => println!("Finished restocking shop!"),
|
||||
Err(err) => println!("Error restocking shop: {:?}", err),
|
||||
}
|
||||
//match res {
|
||||
// Ok(_) => info!("Finished restocking shop!"),
|
||||
// Err(err) => error!("Error restocking shop: {:?}", err),
|
||||
// }
|
||||
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
&ctx.data().db,
|
||||
TaskType::RestockShop,
|
||||
Utc::now() + Duration::hours(1),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
let _ = global_data.db.remove::<Task>(task.id().unwrap()).is_ok();
|
||||
let _ = ctx.data().db.remove::<Task>(task.id().unwrap()).is_ok();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user