v0.2.6 Working on audio commands from api
This commit is contained in:
@@ -5,117 +5,23 @@ extern crate diesel_migrations;
|
||||
use std::env;
|
||||
use std::collections::{HashSet, HashMap};
|
||||
use std::sync::Arc;
|
||||
|
||||
use bot::commands::audio::{create_response, AudioConfig, AudioConfigs};
|
||||
|
||||
use bot::commands::audio::AudioConfig;
|
||||
use log::{error, warn, info};
|
||||
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::model::prelude::GuildId;
|
||||
use serenity::prelude::*;
|
||||
use songbird::SerenityInit;
|
||||
use songbird::{SerenityInit, Songbird};
|
||||
|
||||
use crate::bot::commands::oai::GPTModel;
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{HttpServer, App, web};
|
||||
use crate::bot::{commands::{oai::GPTModel, audio::AudioConfigs}, handler::Handler};
|
||||
|
||||
use dotenv::dotenv;
|
||||
|
||||
mod bot;
|
||||
mod db;
|
||||
|
||||
struct Handler {
|
||||
// Open AI Config
|
||||
oai: Option<bot::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) => {
|
||||
let bot_in_thread = match msg.channel_id.get_thread_members(&ctx.http).await {
|
||||
Ok(t) => {
|
||||
match t.iter().find(|t| t.user_id.unwrap().0 == ctx.cache.current_user_id().0) {
|
||||
Some(_) => true,
|
||||
None => false
|
||||
}
|
||||
}
|
||||
Err(_) => false
|
||||
};
|
||||
if mentioned || bot_in_thread {
|
||||
bot::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() {
|
||||
"play" => bot::commands::audio::play::run(&ctx, &command).await,
|
||||
"stop" => bot::commands::audio::stop::run(&ctx, &command).await,
|
||||
"pause" => bot::commands::audio::pause::run(&ctx, &command).await,
|
||||
"resume" => bot::commands::audio::resume::run(&ctx, &command).await,
|
||||
"skip" => bot::commands::audio::skip::run(&ctx, &command).await,
|
||||
"volume" => bot::commands::audio::volume::run(&ctx, &command).await,
|
||||
_ => {
|
||||
let content: String = match command.data.name.as_str() {
|
||||
"ping" => bot::commands::ping::run(&command.data.options),
|
||||
_ => "Unknown command".to_string()
|
||||
};
|
||||
|
||||
if let Err(why) = create_response(&ctx, &command, content).await {
|
||||
warn!("Cannot respond to slash command: {}", why);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
if ready.guilds.is_empty() {
|
||||
warn!("No ready guilds found");
|
||||
}
|
||||
for guild in ready.guilds {
|
||||
let audio_config_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<AudioConfigs>().expect("Expected AudioConfigs in TypeMap.").clone()
|
||||
};
|
||||
{
|
||||
let mut audio_configs = audio_config_lock.write().await;
|
||||
let _ = audio_configs.insert(guild.id, AudioConfig { volume: 1.0 });
|
||||
}
|
||||
let commands = guild.id.set_application_commands(&ctx.http, |commands| {
|
||||
commands.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { bot::commands::ping::register(command) })
|
||||
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { bot::commands::audio::play::register(command) })
|
||||
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { bot::commands::audio::stop::register(command) })
|
||||
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { bot::commands::audio::pause::register(command) })
|
||||
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { bot::commands::audio::resume::register(command) })
|
||||
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { bot::commands::audio::skip::register(command) })
|
||||
.create_application_command(|command: &mut serenity::builder::CreateApplicationCommand| { bot::commands::audio::volume::register(command) })
|
||||
}).await;
|
||||
match commands {
|
||||
Ok(c) => info!("Registered {} commands for guild {}", c.len(), guild.id.0),
|
||||
Err(why) => error!("Could not register commands for guild {}: {:?}", guild.id.0, why)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv().ok();
|
||||
@@ -168,20 +74,44 @@ async fn main() -> std::io::Result<()> {
|
||||
}
|
||||
};
|
||||
|
||||
// let songbird = Songbird::serenity_from_config(songbird::Config::default().decode_mode(songbird::driver::DecodeMode::Decode));
|
||||
let songbird = Songbird::serenity();
|
||||
|
||||
let mut client = Client::builder(token, intents)
|
||||
.event_handler(handler)
|
||||
.framework(StandardFramework::new()
|
||||
.configure(|c| c.owners(owners)))
|
||||
// .register_songbird_with(Arc::clone(&songbird))
|
||||
// .register_songbird_from_config(songbird::Config::default().decode_mode(songbird::driver::DecodeMode::Decode))
|
||||
.register_songbird()
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
||||
let audio_configs: Arc<RwLock<HashMap<GuildId, AudioConfig>>> = Arc::new(RwLock::new(HashMap::default()));
|
||||
|
||||
{
|
||||
let mut data = client.data.write().await;
|
||||
data.insert::<AudioConfigs>(Arc::new(RwLock::new(HashMap::default())));
|
||||
data.insert::<AudioConfigs>(Arc::clone(&audio_configs));
|
||||
}
|
||||
|
||||
let bot_http = Arc::clone(&client.cache_and_http.http);
|
||||
let http = Arc::clone(&client.cache_and_http.http);
|
||||
// let cache_http = Arc::clone(&client.cache_and_http.clone());
|
||||
// let data = Arc::clone(&client.data.clone());
|
||||
// let t = songbird::Config::default().decode_mode(songbird::driver::DecodeMode::Decode);
|
||||
|
||||
let app_data = Arc::new(AppState {
|
||||
http,
|
||||
songbird: Arc::clone(&songbird),
|
||||
audio_configs
|
||||
});
|
||||
|
||||
|
||||
let shard_manager = Arc::clone(&client.shard_manager);
|
||||
|
||||
tokio::spawn(async move {
|
||||
tokio::signal::ctrl_c().await.expect("Could not register ctrl+c handler");
|
||||
shard_manager.lock().await.shutdown_all().await;
|
||||
});
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(why) = client.start_autosharded().await {
|
||||
@@ -199,7 +129,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.allow_any_header()
|
||||
.max_age(3600);
|
||||
App::new()
|
||||
.app_data(web::Data::new(Arc::clone(&bot_http)))
|
||||
// .app_data(web::Data::new(Arc::clone(&http)))
|
||||
.app_data(web::Data::new(Arc::clone(&app_data)))
|
||||
.configure(crate::db::messages::init_routes)
|
||||
.configure(crate::db::spells::init_routes)
|
||||
.configure(crate::bot::api::init_routes)
|
||||
@@ -219,3 +150,9 @@ async fn main() -> std::io::Result<()> {
|
||||
server.run()
|
||||
.await
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub http: Arc<Http>,
|
||||
pub songbird: Arc<Songbird>,
|
||||
pub audio_configs: Arc<RwLock<HashMap<GuildId, AudioConfig>>>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user