Skip to content

Commit

Permalink
Merge pull request #4 from ckcr4lyf/patch/comments-and-activity
Browse files Browse the repository at this point in the history
Add soem comments and bot activity as param
  • Loading branch information
ckcr4lyf authored Jan 23, 2024
2 parents bc6b6c9 + 0ac5958 commit 3996115
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::stdin;

pub struct Handler {
pub voice_channel_id: String,
pub bot_activity: String,
pub tx: Mutex<mpsc::Sender<bool>>
}

Expand All @@ -19,7 +20,7 @@ impl EventHandler for Handler {
async fn ready(&self, context: Context, _ready: serenity::model::gateway::Ready) {
info!("discord bot is ready");
context.online().await;
context.set_activity(Activity::playing("THIS IS A TEST")).await;
context.set_activity(Activity::playing(self.bot_activity.clone())).await;

let cache_clone = context.cache.clone();
let context_clone = context.clone();
Expand Down Expand Up @@ -48,6 +49,9 @@ impl EventHandler for Handler {

info!("joined channel sucessfully!");
debug!("Telling early-stdin consumer to stop...");
// This will send a signal to the thread which is "wasting" stdin while we took time to
// connect to discord, which will then allow this guy to consume the data (audio) on stdin
// and play it via the bot
self.tx.lock().expect("fail to acquire lock on early-stdin consumer").send(true).expect("fail to signal early-stdin consumer");

// Now we have access to stdin
Expand Down
24 changes: 22 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::thread;
use std::{default, thread};

use log::{error, debug};
use songbird::SerenityInit;
Expand All @@ -14,17 +14,21 @@ mod discord;


#[derive(Default, Serialize, Deserialize)]
#[serde(default)] // we need this if we add new fields, otherwise confy will throw error on load (https://github.com/rust-cli/confy/issues/34)
struct StdinmanConfig {
bot_token: String,
voice_channel_id: String,
bot_activity: String
}

#[derive(Parser)]
struct StdinmanArgs {
#[arg(long)]
bot_token: Option<String>,
#[arg(long)]
voice_channel_id: Option<String>
voice_channel_id: Option<String>,
#[arg(long)]
bot_activity: Option<String>,
}

#[tokio::main]
Expand Down Expand Up @@ -67,8 +71,23 @@ async fn main() {
}
};

let bot_activity = match args.bot_activity {
Some(activity) => activity,
None => {
if cfg.bot_activity == "" {
String::from("stdinman")
} else {
cfg.bot_activity
}
}
};

let (tx, rx) = mpsc::channel::<bool>();

// As soon as stdinman is started, data will start to get piped to it (e.g. from ffmpeg)
// But it'll take some time to actually connect to discord and the voice channel
// (3-5s in practice). To avoid these 3-5s of audio getting buffered and then causing some weird
// behavior, we create a "dummy" stdin consumer, which reads from stdin and just discards the data
debug!("starting early-stdin consumer thread");
thread::spawn(|| stdin::early_stdin_consumer(rx));

Expand All @@ -78,6 +97,7 @@ async fn main() {
.register_songbird()
.event_handler(discord::Handler{
voice_channel_id: voice_channel_id,
bot_activity: bot_activity,
tx: tx.into(),
})
.framework(framework)
Expand Down

0 comments on commit 3996115

Please sign in to comment.