Skip to content

Commit

Permalink
Refactor message filtering logic for better readability and maintaina…
Browse files Browse the repository at this point in the history
…bility
  • Loading branch information
kasugamirai committed Aug 20, 2024
1 parent 8b4d3e9 commit e18732b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/discord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub enum Error {
ChatGPT(#[from] chatgpt::err::Error),
}

fn init_gpt_client(api_key: &str) -> Result<ChatGPT, Error> {
type Result<T> = std::result::Result<T, Error>;

fn init_gpt_client(api_key: &str) -> Result<ChatGPT> {
let config: ModelConfiguration = ModelConfigurationBuilder::default()
.engine(ChatGPTEngine::Gpt4)
.timeout(Duration::from_secs(500))
Expand All @@ -36,16 +38,16 @@ pub struct Handler {
}

impl Handler {
pub fn new(api_key: &str) -> Result<Self, Error> {
pub fn new(api_key: &str) -> Result<Self> {
let gpt_client = init_gpt_client(api_key)?;
Ok(Self { gpt_client })
}
}

#[async_trait]
impl EventHandler for Handler {
async fn message<'a>(&'a self, ctx: Context, msg: Message) {
if msg.author.bot || !msg.content.starts_with('.') {
async fn message(&self, ctx: Context, msg: Message) {
if !filter_msg(&msg) {
return;
}

Expand Down Expand Up @@ -102,3 +104,10 @@ impl EventHandler for Handler {
}
}
}

fn filter_msg(msg: &Message) -> bool {
if msg.author.bot || !msg.content.starts_with('.') {
return false;
}
true
}

0 comments on commit e18732b

Please sign in to comment.