diff --git a/src/main.rs b/src/main.rs index a719fdf..93f87d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,17 @@ +//! Discord bot for vocodes +//! Copyright 2020 Brandon Thomas +//! +//! To recreate, set these permissions in Discord: +//! account type: bot +//! general = view channels +//! text = send messages, send tts messages, embed links, attach files, read message history +//! voice = connect, speak, use voice activity, priority speaker +//! +//! To add it to a server, use: +//! https://discord.com/api/oauth2/authorize?client_id=754149708565315685&permissions=36822272&scope=bot + +pub mod vocodes_api; + use hyper::Body; use hyper::Client as HyperClient; use hyper::Request; @@ -20,53 +34,16 @@ use serenity::framework::standard::{ } }; -use log::{error, info}; -use std::sync::Arc; +use crate::vocodes_api::fetch; +use log::{error, info, warn}; use serenity::http::AttachmentType; use std::borrow::Cow; use std::env; +use std::sync::Arc; -pub async fn fetch(text: &str, speaker: &str) -> Result, Box> { - let url = "http://mumble.stream/speak"; - let mut request = format!("{{\"speaker\": \"{}\", \"text\": \"{}\"}}", speaker, text); - - println!("Req: {}", request); - - let https = HttpsConnector::new(); - let client = HyperClient::builder() - .build::<_, hyper::Body>(https); - - let req = Request::builder() - .method("POST") - .header("Origin", "https://vo.codes") - .header("User-Agent", "Vocodes Discord Bot") - .header("Connection", "keep-alive") - .header("Content-Type", "application/json") - .uri(url) - .body(Body::from(request)) - .unwrap(); - - let resp = client.request(req).await?; - - println!("Status: {}", resp.status()); - - let bytes = hyper::body::to_bytes(resp.into_body()).await?; - let result = bytes.to_vec(); - println!("Length: {}", result.len()); - - Ok(result) -} - -/* - // bot | - // general = view channels - // text = send messages, send tts messages, embed links, attach files, read message history - // voice = connect, speak, use voice activity, priority speaker - let oauth_url = "https://discord.com/api/oauth2/authorize?client_id=754149708565315685&permissions=36822272&scope=bot"; - */ +const SPEAKER_NAME : &'static str = "david-attenborough"; #[group] -#[commands(test)] struct General; struct Handler; @@ -84,24 +61,30 @@ impl EventHandler for Handler { async fn message(&self, ctx: Context, new_message: Message) { info!("Message: {:?}", new_message); - if new_message.author.name == "vocodes" - || !new_message.content.starts_with("vocode ") { + if new_message.author.name == "vocodes" { return; } - let replaced_message = new_message.content.replacen("vocode ", "", 1); + let replaced_message = if new_message.content.starts_with("vocode ") { + new_message.content.replacen("vocode ", "", 1) + } else if new_message.content.starts_with("!vocode ") { + new_message.content.replacen("!vocode ", "", 1) + } else { + return; + }; info!("Replaced message: {}", replaced_message); - let mut response = fetch(&replaced_message, "david-attenborough").await.unwrap(); + if let Ok(response) = fetch(&replaced_message, SPEAKER_NAME).await { + let msg = new_message.channel_id.send_message(&ctx.http, |m| { + m.content(replaced_message); + m.add_file(AttachmentType::Bytes { data: Cow::from(&response), filename: "a wild sound appeared.wav".to_string() }); + m + }).await; - let msg = new_message.channel_id.send_message(&ctx.http, |m| { - m.add_file(AttachmentType::Bytes { data: Cow::from(&response), filename: "filename.wav".to_string() }); - m - }).await; - - if let Err(why) = msg { - println!("Error sending message: {:?}", why); + if let Err(reason) = msg { + warn!("Error sending message: {:?}", reason); + } } } } @@ -112,13 +95,12 @@ impl TypeMapKey for ShardManagerContainer { type Value = Arc>; } - #[tokio::main] async fn main() { env_logger::init(); let framework = StandardFramework::new() - .configure(|c| c.prefix("~")) // set the bot's prefix to "~" + .configure(|c| c.prefix("~")) // set the bot prefix to "~" .group(&GENERAL_GROUP); let secret_token = env::var("DISCORD_TOKEN").expect("token"); @@ -138,11 +120,3 @@ async fn main() { error!("An error occurred while running the client: {:?}", reason); } } - -#[command] -async fn test(ctx: &Context, msg: &Message) -> CommandResult { - println!("Event"); - msg.reply(ctx, "test!").await?; - - Ok(()) -}