Added basic Open AI chat
This commit is contained in:
44
src/main.rs
44
src/main.rs
@@ -8,15 +8,40 @@ use serenity::async_trait;
|
||||
use serenity::framework::StandardFramework;
|
||||
use serenity::model::application::interaction::Interaction;
|
||||
use serenity::model::gateway::Ready;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::http::Http;
|
||||
use serenity::prelude::*;
|
||||
use songbird::SerenityInit;
|
||||
|
||||
mod commands;
|
||||
struct Handler;
|
||||
struct Handler {
|
||||
// Open AI Config
|
||||
oai: Option<commands::oai::OAI>
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn message(&self, ctx: Context, msg: Message) {
|
||||
// Ignore messages from bots
|
||||
if msg.author.bot {
|
||||
return;
|
||||
}
|
||||
match &self.oai {
|
||||
Some(oai) => {
|
||||
match msg.mentions_me(&ctx.http).await {
|
||||
Ok(mentioned) => {
|
||||
if mentioned {
|
||||
commands::oai::generate_response(&ctx, &msg, oai).await;
|
||||
}
|
||||
}
|
||||
Err(why) => warn!("Could not check mentions: {:?}", why)
|
||||
};
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
if let Interaction::ApplicationCommand(command) = interaction {
|
||||
match command.data.name.as_str() {
|
||||
@@ -63,7 +88,7 @@ impl EventHandler for Handler {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
env_logger::init();
|
||||
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,siren=info"));
|
||||
|
||||
let token: String = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||
let intents: GatewayIntents = GatewayIntents::all();
|
||||
@@ -90,9 +115,22 @@ async fn main() {
|
||||
.owners(owners)
|
||||
.prefix("!")
|
||||
);
|
||||
|
||||
let handler = match env::var("OPENAI_API_KEY") {
|
||||
Ok(token) => {
|
||||
info!("Loaded OpenAI token");
|
||||
Handler {
|
||||
oai: Some(commands::oai::OAI { client: reqwest::Client::new(), base_url: "https://api.openai.com/v1".to_string(), max_attempts: 5, token })
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("Could not load OpenAI token: {}", err);
|
||||
Handler { oai: None }
|
||||
}
|
||||
};
|
||||
|
||||
let mut client = Client::builder(token, intents)
|
||||
.event_handler(Handler)
|
||||
.event_handler(handler)
|
||||
.framework(framework)
|
||||
.register_songbird()
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user