async stuff for async serenity
This commit is contained in:
parent
7b8198aac0
commit
8347f340d5
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -15,6 +15,7 @@ dependencies = [
|
||||
"async-std",
|
||||
"native-tls",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"url",
|
||||
]
|
||||
|
||||
@ -1425,6 +1426,7 @@ dependencies = [
|
||||
"log",
|
||||
"memchr",
|
||||
"percent-encoding",
|
||||
"tokio",
|
||||
"url",
|
||||
]
|
||||
|
||||
@ -1437,10 +1439,12 @@ dependencies = [
|
||||
"async-std",
|
||||
"dotenv",
|
||||
"futures",
|
||||
"lazy_static",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"sqlx-core",
|
||||
"syn",
|
||||
"tokio",
|
||||
"url",
|
||||
]
|
||||
|
||||
|
@ -6,5 +6,5 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
serenity = "0.8"
|
||||
sqlx = "0.3"
|
||||
sqlx = {version = "0.3", features = ["runtime-tokio", "macros"]}
|
||||
dotenv = "0.15"
|
||||
|
116
src/main.rs
116
src/main.rs
@ -1,8 +1,15 @@
|
||||
use serenity::{
|
||||
client::Client,
|
||||
framework::StandardFramework,
|
||||
client::{
|
||||
Client, Context
|
||||
},
|
||||
framework::standard::{
|
||||
Args, StandardFramework
|
||||
},
|
||||
model::{
|
||||
channel::Message
|
||||
},
|
||||
prelude::{
|
||||
EventHandler, Context, TypeMapKey
|
||||
EventHandler, TypeMapKey, Mutex
|
||||
}
|
||||
};
|
||||
|
||||
@ -13,7 +20,7 @@ use sqlx::{
|
||||
|
||||
use dotenv::dotenv;
|
||||
|
||||
use std::env;
|
||||
use std::{env, sync::Arc};
|
||||
|
||||
struct SQLPool;
|
||||
|
||||
@ -21,9 +28,21 @@ impl TypeMapKey for SQLPool {
|
||||
type Value = Pool<MySqlConnection>;
|
||||
}
|
||||
|
||||
struct VoiceManager;
|
||||
|
||||
impl TypeMapKey for VoiceManager {
|
||||
type Value = Arc<Mutex<ClientVoiceManager>>;
|
||||
}
|
||||
|
||||
#[group]
|
||||
#[commands()]
|
||||
struct Commands;
|
||||
struct General;
|
||||
|
||||
struct Sound {
|
||||
name: String,
|
||||
id: u32,
|
||||
src: Vec<u8>,
|
||||
}
|
||||
|
||||
// create event handler for bot
|
||||
struct Handler;
|
||||
@ -31,27 +50,98 @@ struct Handler;
|
||||
impl EventHandler for Handler {}
|
||||
|
||||
// entry point
|
||||
#[tokio::main]
|
||||
fn main() {
|
||||
dotenv();
|
||||
|
||||
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing token from environment"), Handler).expect("Failed to create client");
|
||||
|
||||
client.with_framework(StandardFramework::new()
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c.prefix("?"))
|
||||
.group(&GENERAL_GROUP));
|
||||
.group(&GENERAL_GROUP);
|
||||
|
||||
let mut client = Client::new_with_framework(&env::var("DISCORD_TOKEN").expect("Missing token from environment"), Handler, framework)
|
||||
.await
|
||||
.expect("Error occurred creating client");
|
||||
|
||||
{
|
||||
let mut data = client.data.write();
|
||||
|
||||
let pool = MySqlPool::new(env::var("DATABASE_URL"));
|
||||
|
||||
let mut data = client.data.write().await;
|
||||
data.insert::<SQLPool>(pool);
|
||||
|
||||
data.insert::<VoiceManager>(Arc::clone(&client.voice_manager));
|
||||
}
|
||||
|
||||
client.start().expect("Failed to start client");
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[command]
|
||||
fn play() {
|
||||
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;
|
||||
|
||||
match sound_res {
|
||||
Ok(sound) => {
|
||||
let source = sound.src;
|
||||
|
||||
|
||||
}
|
||||
|
||||
Err(reason) => {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user