soundfx-bot/src/main.rs

148 lines
2.7 KiB
Rust
Raw Normal View History

2020-04-13 23:22:31 +00:00
use serenity::{
2020-04-26 00:51:51 +00:00
client::{
Client, Context
},
framework::standard::{
Args, StandardFramework
},
model::{
channel::Message
},
2020-04-13 23:22:31 +00:00
prelude::{
2020-04-26 00:51:51 +00:00
EventHandler, TypeMapKey, Mutex
2020-04-13 23:22:31 +00:00
}
};
use sqlx::{
Pool,
mysql::MySqlPool
};
use dotenv::dotenv;
2020-04-26 00:51:51 +00:00
use std::{env, sync::Arc};
2020-04-13 23:22:31 +00:00
struct SQLPool;
impl TypeMapKey for SQLPool {
type Value = Pool<MySqlConnection>;
}
2020-04-26 00:51:51 +00:00
struct VoiceManager;
impl TypeMapKey for VoiceManager {
type Value = Arc<Mutex<ClientVoiceManager>>;
}
2020-04-13 23:22:31 +00:00
#[group]
#[commands()]
2020-04-26 00:51:51 +00:00
struct General;
struct Sound {
name: String,
id: u32,
src: Vec<u8>,
}
2020-04-13 23:22:31 +00:00
// create event handler for bot
struct Handler;
impl EventHandler for Handler {}
// entry point
2020-04-26 00:51:51 +00:00
#[tokio::main]
2020-04-13 23:22:31 +00:00
fn main() {
dotenv();
2020-04-26 00:51:51 +00:00
let framework = StandardFramework::new()
2020-04-13 23:22:31 +00:00
.configure(|c| c.prefix("?"))
2020-04-26 00:51:51 +00:00
.group(&GENERAL_GROUP);
2020-04-13 23:22:31 +00:00
2020-04-26 00:51:51 +00:00
let mut client = Client::new_with_framework(&env::var("DISCORD_TOKEN").expect("Missing token from environment"), Handler, framework)
.await
.expect("Error occurred creating client");
2020-04-13 23:22:31 +00:00
2020-04-26 00:51:51 +00:00
{
2020-04-13 23:22:31 +00:00
let pool = MySqlPool::new(env::var("DATABASE_URL"));
2020-04-26 00:51:51 +00:00
let mut data = client.data.write().await;
2020-04-13 23:22:31 +00:00
data.insert::<SQLPool>(pool);
2020-04-26 00:51:51 +00:00
data.insert::<VoiceManager>(Arc::clone(&client.voice_manager));
2020-04-13 23:22:31 +00:00
}
2020-04-26 00:51:51 +00:00
let _ = client.start().await.map_err(|reason| println!("Failed to start client: {:?}", reason));
}
async fn search_for_sound(query: String, db_connector: &MySqlConnection) -> Result<Sound, Box<dyn std::error::Error>> {
if query.to_lowercase().starts_with("id:") {
let id = query[3..].parse::<u32>()?;
let sound = sqlx::query!(
"
SELECT name, src
FROM sounds
WHERE id = ?
LIMIT 1
",
id
)
.fetch_one(&db_connector)
.await?;
Ok(Sound {
name: sound.name,
id,
src: sound.src,
})
}
else {
let name = query;
let sound = sqlx::query!(
"
SELECT id, src
FROM sounds
ORDER BY rand()
WHERE name = ?
LIMIT 1
",
name
)
.fetch_one(&db_connector)
.await?;
Ok(Sound {
name,
id: sound.id,
src: sound.src,
})
}
2020-04-13 23:22:31 +00:00
}
2020-04-26 00:51:51 +00:00
2020-04-13 23:22:31 +00:00
#[command]
2020-04-26 00:51:51 +00:00
async fn play(ctx: &mut Context, msg: &Message, args: Args) {
let search_term = args.collect().join(" ");
let pool_lock = ctx.data.read().await
.get::<SQLPool>().expect("Could not get SQL Pool out of data");
let mut pool = pool_lock.lock().await;
let sound_res = search_for_sound(search_term, pool).await;
2020-04-13 23:22:31 +00:00
2020-04-26 00:51:51 +00:00
match sound_res {
Ok(sound) => {
let source = sound.src;
}
Err(reason) => {
}
}
2020-04-13 23:22:31 +00:00
}