Reformat, working on roll
This commit is contained in:
@@ -1,42 +1,36 @@
|
||||
use serenity::all::Interaction;
|
||||
use serenity::all::{CreateInteractionResponse, Interaction};
|
||||
use serenity::async_trait;
|
||||
use serenity::model::gateway::Ready;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::prelude::*;
|
||||
|
||||
use crate::bot::commands::chat::generate_response;
|
||||
use crate::bot::oai::OAI;
|
||||
use crate::data::guilds::GuildCache;
|
||||
use super::{commands, oai};
|
||||
use super::chat::create_response;
|
||||
use super::{commands};
|
||||
use super::chat::{create_message_response, create_modal_response};
|
||||
|
||||
pub struct Handler {
|
||||
// Open AI Config
|
||||
pub oai: Option<oai::OAI>,
|
||||
pub oai: Option<OAI>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn message(&self, ctx: Context, msg: Message) {
|
||||
// Ignore messages from bots
|
||||
// Ignore bot messages
|
||||
if msg.author.bot {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle direct messages
|
||||
if let None = msg.guild_id {
|
||||
log::trace!("Received DM from {}: {}", msg.author, msg.content);
|
||||
}
|
||||
|
||||
// Handle OAI messages
|
||||
match &self.oai {
|
||||
Some(oai) => {
|
||||
match msg.mentions_me(&ctx.http).await {
|
||||
Ok(mentioned) => {
|
||||
let bot_in_thread = match msg.channel_id.get_thread_members(&ctx.http).await {
|
||||
Ok(t) => match t.iter().find(|t| t.user_id == ctx.cache.current_user().id) {
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
},
|
||||
Err(_) => false,
|
||||
};
|
||||
if mentioned || bot_in_thread {
|
||||
commands::chat::generate_response(&ctx, &msg, oai).await;
|
||||
}
|
||||
}
|
||||
Err(why) => log::warn!("Could not check mentions: {:?}", why),
|
||||
};
|
||||
handle_oai_messages(oai, &ctx, &msg).await;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
@@ -80,7 +74,7 @@ impl EventHandler for Handler {
|
||||
Ok(c) => log::info!(
|
||||
"Registered {} commands for guild {}",
|
||||
c.len(),
|
||||
guild.id.get()
|
||||
guild.id.get(),
|
||||
),
|
||||
Err(why) => log::error!(
|
||||
"Could not register commands for guild {}: {:?}",
|
||||
@@ -92,8 +86,10 @@ impl EventHandler for Handler {
|
||||
}
|
||||
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
if let Interaction::Command(command) = interaction {
|
||||
log::trace!("Received command interaction: {command:#?}");
|
||||
if let Interaction::Ping(ping) = interaction {
|
||||
log::debug!("Received interaction ping: {:?}", ping);
|
||||
} else if let Interaction::Command(command) = interaction {
|
||||
log::debug!("Received command interaction: {command:#?}");
|
||||
match command.data.name.as_str() {
|
||||
// Match commands without returns
|
||||
"play" => commands::audio::play::run(&ctx, &command).await,
|
||||
@@ -105,15 +101,30 @@ impl EventHandler for Handler {
|
||||
"volume" => commands::audio::volume::run(&ctx, &command).await,
|
||||
"schedule" => commands::event::schedule::run(&ctx, &command).await,
|
||||
"roll" => commands::fun::roll::run(&ctx, &command).await,
|
||||
_ => {
|
||||
let content: String = match command.data.name.as_str() {
|
||||
// Match commands with string returns
|
||||
"ping" => commands::utility::ping::run(&command.data.options),
|
||||
_ => "Unknown command".to_string(),
|
||||
};
|
||||
create_response(&ctx, &command, content, false).await;
|
||||
}
|
||||
"ping" => commands::utility::ping::run(&ctx, &command).await,
|
||||
_ => {}
|
||||
}
|
||||
} else if let Interaction::Modal(modal) = interaction {
|
||||
log::debug!("Received interaction modal: {:?}", modal);
|
||||
create_modal_response(&ctx, &modal).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_oai_messages(oai: &OAI, ctx: &Context, msg: &Message) {
|
||||
match msg.mentions_me(&ctx.http).await {
|
||||
Ok(mentioned) => {
|
||||
let bot_in_thread = match msg.channel_id.get_thread_members(&ctx.http).await {
|
||||
Ok(t) => match t.iter().find(|t| t.user_id == ctx.cache.current_user().id) {
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
},
|
||||
Err(_) => false,
|
||||
};
|
||||
if mentioned || bot_in_thread {
|
||||
generate_response(&ctx, &msg, oai).await;
|
||||
}
|
||||
}
|
||||
Err(why) => log::warn!("Could not check mentions: {why}"),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user