-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b80ea58
commit cb672f1
Showing
8 changed files
with
298 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
use crate::{message_handler::handle_message, prisma::invite_data, structs::*}; | ||
|
||
use rand::Rng; | ||
use tokio::{join, sync::Mutex}; | ||
use twilight_gateway::Event; | ||
use twilight_http::{request::channel::reaction::RequestReactionType, Client as HttpClient}; | ||
use twilight_model::{ | ||
channel::message::AllowedMentions, | ||
gateway::presence::Status, | ||
id::{ | ||
marker::{ChannelMarker, UserMarker}, | ||
Id, | ||
}, | ||
}; | ||
use vesper::prelude::*; | ||
|
||
use std::{sync::Arc, time::Duration}; | ||
|
||
const AETHOR_ID: Id<UserMarker> = Id::new(870383692403593226); | ||
const GENERAL_ID: Id<ChannelMarker> = Id::new(748957504666599507); | ||
const TRICKED_ID: u64 = 336465356304678913; | ||
|
||
pub async fn handle_event( | ||
event: Event, | ||
http: &Arc<HttpClient>, | ||
state: &Arc<Mutex<State>>, | ||
framework: Arc<Framework<Arc<Mutex<State>>>>, | ||
) -> color_eyre::Result<()> { | ||
let mut locked_state = state.lock().await; | ||
match event { | ||
Event::PresenceUpdate(p) => { | ||
if p.user.id() == AETHOR_ID && p.status == Status::Offline { | ||
for _ in 0..10 { | ||
http.create_message(GENERAL_ID) | ||
.content("AETHOR WENT OFFLINE <@{TRICKED_ID}> <@{TRICKED_ID}>")? | ||
.allowed_mentions(Some(&AllowedMentions { | ||
users: vec![Id::new(TRICKED_ID)], | ||
..Default::default() | ||
})) | ||
.exec() | ||
.await?; | ||
} | ||
} | ||
} | ||
Event::InteractionCreate(i) => { | ||
tracing::info!("Slash Command!"); | ||
tokio::spawn(async move { | ||
let inner = i.0; | ||
framework.process(inner).await; | ||
}); | ||
} | ||
Event::InviteCreate(inv) => { | ||
locked_state.invites.push(BotInvite::from(inv)); | ||
} | ||
Event::MemberAdd(member) => { | ||
let invites_response = http.guild_invites(member.guild_id).exec().await?; | ||
let invites = invites_response.models().await?; | ||
let mut invites_iter = invites.iter(); | ||
for old_invite in locked_state.invites.iter() { | ||
if let Some(invite) = invites_iter.find(|x| x.code == old_invite.code) { | ||
if old_invite.uses < invite.uses.unwrap_or_default() { | ||
let name = locked_state.config.invites.iter().find_map(|(key, value)| { | ||
if value == &old_invite.code { | ||
Some(key.clone()) | ||
} else { | ||
None | ||
} | ||
}); | ||
http.create_message(Id::new(locked_state.config.join_channel)) | ||
.content(&format!( | ||
"{} Joined invite used {}", | ||
member.user.name, | ||
if let Some(name) = name { | ||
format!("{name} ({})", invite.code) | ||
} else { | ||
invite.code.clone() | ||
} | ||
))? | ||
.exec() | ||
.await?; | ||
locked_state.db.invite_data().create( | ||
member.user.id.get().to_string(), | ||
vec![invite_data::SetParam::SetInviteUsed(Some(invite.code.clone()))], | ||
); | ||
// Found the used invite! exit! | ||
break; | ||
} | ||
} | ||
} | ||
locked_state.invites = invites | ||
.into_iter() | ||
.map(|invite| BotInvite { | ||
code: invite.code.clone(), | ||
uses: invite.uses.unwrap_or_default(), | ||
}) | ||
.collect(); | ||
} | ||
Event::MessageCreate(msg) => { | ||
tracing::info!("Message received {}", &msg.content.replace('\n', "\\ ")); | ||
|
||
if msg.guild_id.is_none() || msg.author.bot { | ||
return Ok(()); | ||
} | ||
|
||
if let Some(today_i) = locked_state.config.today_i_channel { | ||
if msg.channel_id == Id::new(today_i) && !msg.content.clone().to_lowercase().starts_with("today i") { | ||
http.delete_message(msg.channel_id, msg.id).exec().await?; | ||
return Ok(()); | ||
} | ||
} | ||
|
||
if let Some(channel_limit_duration) = locked_state.channel_bucket.limit_duration(msg.channel_id.get()) { | ||
tracing::info!("Channel limit reached {}", channel_limit_duration.as_secs()); | ||
return Ok(()); | ||
} | ||
if let Some(user_limit_duration) = locked_state.user_bucket.limit_duration(msg.author.id.get()) { | ||
tracing::info!("User limit reached {}", user_limit_duration.as_secs()); | ||
if Duration::from_secs(5) > user_limit_duration { | ||
tokio::time::sleep(user_limit_duration).await; | ||
} else { | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
let r = handle_message(&msg, locked_state, http).await; | ||
match r { | ||
Ok(res) => { | ||
let Command { | ||
embeds, | ||
text, | ||
reaction, | ||
attachments, | ||
reply, | ||
skip, | ||
mention, | ||
} = res; | ||
if skip { | ||
return Ok(()); | ||
} else if let Some(reaction) = reaction { | ||
http.create_reaction( | ||
msg.channel_id, | ||
msg.id, | ||
&RequestReactionType::Unicode { | ||
name: &reaction.to_string(), | ||
}, | ||
) | ||
.exec() | ||
.await?; | ||
} else { | ||
let mut req = http | ||
.create_message(msg.channel_id) | ||
.embeds(&embeds)? | ||
.attachments(&attachments)?; | ||
if let Some(text) = &text { | ||
req = req.content(text)?; | ||
} | ||
|
||
if reply { | ||
req = req.reply(msg.id); | ||
} | ||
let mentions = AllowedMentions { | ||
users: vec![msg.author.id], | ||
..Default::default() | ||
}; | ||
if mention { | ||
req = req.allowed_mentions(Some(&mentions)); | ||
} | ||
|
||
req.exec().await?; | ||
} | ||
} | ||
Err(e) => { | ||
tracing::error!("Error handling message: {:?}", e); | ||
} | ||
} | ||
} | ||
Event::Ready(_) => { | ||
tracing::info!("Connected"); | ||
} | ||
Event::TypingStart(event) => { | ||
if rand::thread_rng().gen_range(0..100) != 1 { | ||
return Ok(()); | ||
} | ||
|
||
if !locked_state | ||
.config | ||
.message_indicator_channels | ||
.contains(&event.channel_id.get()) | ||
{ | ||
return Ok(()); | ||
} | ||
if event.user_id.get() == locked_state.last_typer { | ||
return Ok(()); | ||
} | ||
if let Some(mem) = event.member { | ||
let (msg, _) = join!( | ||
http.create_message(event.channel_id) | ||
.content(&format!("{} is typing", mem.user.name))? | ||
.exec(), | ||
async { | ||
if let Some(id) = locked_state.del.get(&event.channel_id) { | ||
let _ = http | ||
.delete_message(event.channel_id, Id::new(id.to_owned())) | ||
.exec() | ||
.await; | ||
} | ||
}, | ||
); | ||
let res = msg?.model().await?; | ||
locked_state.del.insert(event.channel_id, res.id.get()); | ||
locked_state.last_typer = event.user_id.get(); | ||
} | ||
} | ||
Event::GuildCreate(guild) => { | ||
tracing::info!("Active in guild {}", guild.name); | ||
let invites_response = http.guild_invites(guild.id).exec().await?; | ||
locked_state.invites.append( | ||
&mut invites_response | ||
.models() | ||
.await? | ||
.into_iter() | ||
.map(|invite| BotInvite { | ||
code: invite.code.clone(), | ||
uses: invite.uses.unwrap_or_default(), | ||
}) | ||
.collect(), | ||
); | ||
} | ||
_ => {} | ||
} | ||
Ok(()) | ||
} |
Oops, something went wrong.