Skip to content

Commit

Permalink
[discord] Apply upstream patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Nov 5, 2023
1 parent e15f864 commit 7c0e466
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
24 changes: 16 additions & 8 deletions lib_discord/agent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type init_arg = {
consumer : consumer;
ffmpeg_path : string;
ffmpeg_options : string list;
youtubedl_path : string;
}

type call_msg =
Expand Down Expand Up @@ -51,13 +52,10 @@ type state = {
consumer : consumer;
ffmpeg_path : string;
ffmpeg_options : string list;
youtubedl_path : string;
}

let spawn_youtubedl process_mgr ~sw ~stdout url =
let executable =
Sys.getenv_opt "YOUTUBEDL_PATH"
|> Option.value ~default:"/usr/bin/youtube-dl"
in
let spawn_youtubedl process_mgr ~sw ~stdout ~path:(executable : string) url =
Eio.Process.spawn ~sw process_mgr ~stdout ~executable
[ executable; "-f"; "bestaudio"; "-o"; "-"; "-q"; "--no-warnings"; url ]

Expand All @@ -71,13 +69,20 @@ class t =
inherit [init_arg, msg, state] Actaa.Gen_server.behaviour

method private init env ~sw
{ token; intents; consumer; ffmpeg_path; ffmpeg_options } =
{
token;
intents;
consumer;
ffmpeg_path;
ffmpeg_options;
youtubedl_path;
} =
let st = State.start env ~sw in
let gw =
Gateway.spawn env ~sw ~token ~intents ~state:st
~consumer:(self :> Gateway.consumer)
in
{ st; gw; consumer; ffmpeg_path; ffmpeg_options }
{ st; gw; consumer; ffmpeg_path; ffmpeg_options; youtubedl_path }

method! private handle_cast env ~sw ({ st; gw; consumer; _ } as state) =
function
Expand Down Expand Up @@ -111,7 +116,10 @@ class t =
| `Pipe (src : Eio.Flow.source_ty Eio.Resource.t) -> play src
| `Ytdl url ->
let src, sink = Eio.Process.pipe ~sw process_mgr in
let _p1 = spawn_youtubedl process_mgr ~sw ~stdout:sink url in
let _p1 =
spawn_youtubedl process_mgr ~sw ~stdout:sink
~path:state.youtubedl_path url
in
play src;
Eio.Flow.close src;
Eio.Flow.close sink));
Expand Down
47 changes: 42 additions & 5 deletions lib_discord/consumer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type 'user_state init_arg = {
user_handler : 'user_state user_handler;
ffmpeg_path : string;
ffmpeg_options : string list;
youtubedl_path : string;
}

type call_msg = |
Expand All @@ -33,8 +34,15 @@ class ['user_state] t =
['user_state init_arg, msg, 'user_state state] Actaa.Gen_server.behaviour

method private init env ~sw
{ token; intents; user_init; user_handler; ffmpeg_path; ffmpeg_options }
=
{
token;
intents;
user_init;
user_handler;
ffmpeg_path;
ffmpeg_options;
youtubedl_path;
} =
let agent = new Agent.t in
agent
|> Actaa.Gen_server.start env ~sw
Expand All @@ -45,6 +53,7 @@ class ['user_state] t =
consumer = (self :> consumer);
ffmpeg_path;
ffmpeg_options;
youtubedl_path;
};
let user_state = user_init () in
{ agent; user_state; user_handler }
Expand All @@ -61,10 +70,38 @@ class ['user_state] t =
`NoReply { state with user_state }
end

let start env ~sw ~token ~intents ~ffmpeg_path ~ffmpeg_options user_init
user_handler =
let default_ffmpeg_path = "/usr/bin/ffmpeg"

let default_ffmpeg_options =
[
"-i";
"pipe:0";
"-ac";
"2";
"-ar";
"48000";
"-f";
"s16le";
"-loglevel";
"quiet";
"pipe:1";
]

let default_youtubedl_path = "/usr/bin/youtube-dl"

let start env ~sw ~token ~intents ?(ffmpeg_path = default_ffmpeg_path)
?(ffmpeg_options = default_ffmpeg_options)
?(youtubedl_path = default_youtubedl_path) user_init user_handler =
let t = new t in
t
|> Actaa.Gen_server.start env ~sw
{ token; intents; user_init; user_handler; ffmpeg_path; ffmpeg_options };
{
token;
intents;
user_init;
user_handler;
ffmpeg_path;
ffmpeg_options;
youtubedl_path;
};
t

0 comments on commit 7c0e466

Please sign in to comment.