2021-11-12 12:02:22 +00:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
|
|
2023-08-23 17:37:42 +00:00
|
|
|
use poise::{Event, FrameworkContext};
|
|
|
|
|
use poise::serenity_prelude::model::{
|
2021-11-12 12:02:22 +00:00
|
|
|
gateway::Ready,
|
|
|
|
|
id::ChannelId,
|
|
|
|
|
};
|
2023-08-24 09:27:15 +00:00
|
|
|
use poise::serenity_prelude::{Context, Message};
|
|
|
|
|
use crate::ManifoldData;
|
2023-08-23 17:37:42 +00:00
|
|
|
use crate::error::{ManifoldError, ManifoldResult};
|
|
|
|
|
use crate::models::user::UserInfo;
|
2021-11-12 12:02:22 +00:00
|
|
|
|
|
|
|
|
pub struct Handler {
|
2021-11-12 12:15:59 +00:00
|
|
|
pub timer_running: AtomicBool,
|
2021-11-12 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Handler {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Handler {
|
|
|
|
|
timer_running: AtomicBool::from(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 17:37:42 +00:00
|
|
|
pub async fn listen(ctx: &Context, framework_ctx: FrameworkContext<'_, ManifoldData, ManifoldError>, event: &Event<'_>) -> ManifoldResult<()> {
|
|
|
|
|
match event {
|
|
|
|
|
Event::Ready { data_about_bot} => Handler::standard_startup(&ctx, &framework_ctx, data_about_bot).await,
|
|
|
|
|
Event::Message { new_message } => Handler::message(&ctx, &framework_ctx, &new_message).await,
|
|
|
|
|
Event::MessageUpdate { old_if_available, new, event } => Handler::message_edited(&ctx, &framework_ctx, old_if_available, new).await,
|
|
|
|
|
_ => Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-12 12:02:22 +00:00
|
|
|
|
2023-08-23 17:37:42 +00:00
|
|
|
pub async fn standard_startup(ctx: &Context, framework_ctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, data_about_bot: &Ready) -> ManifoldResult<()> {
|
|
|
|
|
let config = &framework_ctx.user_data().await.bot_config;
|
|
|
|
|
let responses = &framework_ctx.user_data().await.responses;
|
2021-11-12 12:02:22 +00:00
|
|
|
|
|
|
|
|
let greeting = match responses.get_response(&"bot startup".to_string()) {
|
2023-08-23 17:37:42 +00:00
|
|
|
Some(g) => g.to_owned(),
|
2021-11-12 12:02:22 +00:00
|
|
|
None => "Manifold bot connected to discord and ready to begin broadcast operations.".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-17 23:25:59 +00:00
|
|
|
let bot_nickname = config.get_value(&"BotNickname".to_string()).unwrap_or("BrokenManifoldBot".to_string());
|
|
|
|
|
let channel: ChannelId = config.get_channel(&"Log".to_string()).expect("Specified log channel invalid or unavailable");
|
2023-08-23 17:37:42 +00:00
|
|
|
for guild in &data_about_bot.guilds {
|
2022-07-17 23:25:59 +00:00
|
|
|
match guild.id.edit_nickname(&ctx, Some(&*bot_nickname)).await {
|
2021-11-17 15:12:21 +00:00
|
|
|
Ok(()) => (),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("Error setting bot nickname (lack permission?): {:?}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-12 12:02:22 +00:00
|
|
|
|
|
|
|
|
channel.say(&ctx, greeting).await.expect("Couldn't message log channel!");
|
2023-08-23 17:37:42 +00:00
|
|
|
|
|
|
|
|
Ok(())
|
2021-11-12 12:02:22 +00:00
|
|
|
}
|
2021-11-19 23:37:59 +00:00
|
|
|
|
2023-08-23 17:37:42 +00:00
|
|
|
async fn message(_ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, msg: &Message) -> ManifoldResult<()> {
|
|
|
|
|
let userinfo = &mut fctx.user_data().await.user_info.lock().await;
|
|
|
|
|
|
|
|
|
|
if let Some(u) = userinfo.get_mut(&msg.author.id.as_u64()) {
|
|
|
|
|
u.last_seen = Some(chrono::Utc::now().timestamp());
|
|
|
|
|
} else {
|
|
|
|
|
let new_user = UserInfo {
|
|
|
|
|
user_id: msg.author.id.as_u64().clone() as i64,
|
|
|
|
|
username: msg.author.name.to_owned(),
|
|
|
|
|
weather_location: None,
|
|
|
|
|
weather_units: None,
|
|
|
|
|
timezone: None,
|
|
|
|
|
last_seen: Some(chrono::Utc::now().timestamp()),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
userinfo.insert(msg.author.id.as_u64().clone(), new_user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn message_edited(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, original: &Option<Message>, new_message: &Option<Message>) -> ManifoldResult<()> {
|
|
|
|
|
let log_channel = fctx.user_data().await.bot_config.get_channel(&"Log".to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
if let Some(new) = new_message.as_ref() {
|
|
|
|
|
log_channel.send_message(ctx, |f| {
|
|
|
|
|
f
|
|
|
|
|
.content("")
|
|
|
|
|
.embed(|e| {
|
|
|
|
|
e
|
|
|
|
|
.title("Message updated")
|
|
|
|
|
.author(|a| a.name(new.author.name.clone()))
|
|
|
|
|
.timestamp(new.timestamp)
|
|
|
|
|
.field("Original Content", match original.as_ref() {
|
|
|
|
|
Some(m) => m.content.clone(),
|
|
|
|
|
None => "Not available".to_string(),
|
|
|
|
|
}, false)
|
|
|
|
|
.field("New Content", new.content.clone(), false)
|
|
|
|
|
})
|
|
|
|
|
}).await?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2021-11-19 23:37:59 +00:00
|
|
|
}
|
|
|
|
|
}
|