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",
|
"async-std",
|
||||||
"native-tls",
|
"native-tls",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
"tokio",
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -1425,6 +1426,7 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"memchr",
|
"memchr",
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
|
"tokio",
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -1437,10 +1439,12 @@ dependencies = [
|
|||||||
"async-std",
|
"async-std",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"futures",
|
"futures",
|
||||||
|
"lazy_static",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"sqlx-core",
|
"sqlx-core",
|
||||||
"syn",
|
"syn",
|
||||||
|
"tokio",
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -6,5 +6,5 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serenity = "0.8"
|
serenity = "0.8"
|
||||||
sqlx = "0.3"
|
sqlx = {version = "0.3", features = ["runtime-tokio", "macros"]}
|
||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
|
116
src/main.rs
116
src/main.rs
@ -1,8 +1,15 @@
|
|||||||
use serenity::{
|
use serenity::{
|
||||||
client::Client,
|
client::{
|
||||||
framework::StandardFramework,
|
Client, Context
|
||||||
|
},
|
||||||
|
framework::standard::{
|
||||||
|
Args, StandardFramework
|
||||||
|
},
|
||||||
|
model::{
|
||||||
|
channel::Message
|
||||||
|
},
|
||||||
prelude::{
|
prelude::{
|
||||||
EventHandler, Context, TypeMapKey
|
EventHandler, TypeMapKey, Mutex
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -13,7 +20,7 @@ use sqlx::{
|
|||||||
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
|
||||||
use std::env;
|
use std::{env, sync::Arc};
|
||||||
|
|
||||||
struct SQLPool;
|
struct SQLPool;
|
||||||
|
|
||||||
@ -21,9 +28,21 @@ impl TypeMapKey for SQLPool {
|
|||||||
type Value = Pool<MySqlConnection>;
|
type Value = Pool<MySqlConnection>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct VoiceManager;
|
||||||
|
|
||||||
|
impl TypeMapKey for VoiceManager {
|
||||||
|
type Value = Arc<Mutex<ClientVoiceManager>>;
|
||||||
|
}
|
||||||
|
|
||||||
#[group]
|
#[group]
|
||||||
#[commands()]
|
#[commands()]
|
||||||
struct Commands;
|
struct General;
|
||||||
|
|
||||||
|
struct Sound {
|
||||||
|
name: String,
|
||||||
|
id: u32,
|
||||||
|
src: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
// create event handler for bot
|
// create event handler for bot
|
||||||
struct Handler;
|
struct Handler;
|
||||||
@ -31,27 +50,98 @@ struct Handler;
|
|||||||
impl EventHandler for Handler {}
|
impl EventHandler for Handler {}
|
||||||
|
|
||||||
// entry point
|
// entry point
|
||||||
|
#[tokio::main]
|
||||||
fn main() {
|
fn main() {
|
||||||
dotenv();
|
dotenv();
|
||||||
|
|
||||||
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing token from environment"), Handler).expect("Failed to create client");
|
let framework = StandardFramework::new()
|
||||||
|
|
||||||
client.with_framework(StandardFramework::new()
|
|
||||||
.configure(|c| c.prefix("?"))
|
.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 pool = MySqlPool::new(env::var("DATABASE_URL"));
|
||||||
|
|
||||||
|
let mut data = client.data.write().await;
|
||||||
data.insert::<SQLPool>(pool);
|
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]
|
#[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