1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
use std::sync::Arc;
use grate::tracing;
use serenity::prelude::*;
use songbird::{
input::YoutubeDl,
Call,
Songbird,
};
use tap::Conv;
use crate::{
bot::HttpKey,
util,
PoiseContext,
PoiseData,
CONFIG,
};
pub fn commands() -> impl IntoIterator<Item = poise::Command<PoiseData, anyhow::Error>> {
vec![play(), pause(), resume(), die(), list(), skip(), move_()]
}
pub async fn songbird(ctx: PoiseContext<'_>) -> anyhow::Result<(Arc<Songbird>, Arc<Mutex<Call>>)> {
let Some(gid) = ctx.guild_id() else {
return Err(anyhow::anyhow!("no guild id").into());
};
let sb = songbird::get(ctx.serenity_context()).await.expect("acquiring songbird handle");
let call = sb.get_or_insert(gid);
Ok((sb, call))
}
pub async fn _play(ctx: PoiseContext<'_>, url: &url::Url) -> anyhow::Result<()> {
use url::Host;
tracing::debug!(%url, "playing");
if !url.scheme().starts_with("http") {
tracing::warn!(%url, "got bad url argument");
util::reply(ctx, "bAD LiNk").await?;
return Ok(());
}
let host = url.host().and_then(|u| match u {
Host::Domain(h) => Some(h.to_owned()),
_ => None,
});
if host.is_some_and(|h| h.to_lowercase().contains("imgur")) {
tracing::info!("detected imgur link");
if ctx.author().id == 106160362109272064 {
util::reply(ctx, "fuck you conway").await?;
} else {
util::reply(ctx, "IMGUR IS BAD, YOU TRASH CAN MAN").await?;
}
return Ok(());
}
let Some(voice_channel) = util::best_voice_channel(ctx) else {
tracing::error!(?ctx, "couldn't find a relevant voice channel");
util::react(ctx, '🔇').await?;
return Ok(());
};
util::react(ctx, '🔃').await?;
let client = {
let data = ctx.serenity_context().data.read().await;
data.get::<HttpKey>().unwrap().clone()
};
{
let (_sb, call) = songbird(ctx).await?;
let mut call = call.lock().await;
if call.current_channel().is_none() {
call.join(voice_channel).await?;
}
let input =
YoutubeDl::new_ytdl_like(&crate::config::YTDL_COMMAND, client.clone(), url.to_string());
let track = input.conv::<songbird::tracks::Track>();
// TODO: store enqueueing channel so songbird handler can switch channels
call.enqueue(track).await;
}
util::react(ctx, '📣').await?;
util::unreact(ctx, '🔃').await?;
Ok(())
}
/// Move audio to the caller's voice channel.
#[poise::command(rename = "move", prefix_command, guild_only, category = "playback")]
pub async fn move_(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
let (_sb, call) = songbird(ctx).await?;
let mut call = call.lock().await;
if call.current_channel().is_none() {
tracing::debug!("no current channel");
util::reply(ctx, "let's get yer head screwed on straight").await?;
return Ok(());
}
let Some(voice_channel) = util::best_voice_channel(ctx) else {
tracing::error!(?ctx, "couldn't find a relevant voice channel");
util::react(ctx, '🔇').await?;
return Ok(());
};
call.join(voice_channel).await?;
Ok(())
}
/// Play a link.
#[poise::command(prefix_command, guild_only, category = "playback")]
pub async fn play(
ctx: PoiseContext<'_>,
#[description = "link to play (if absent, resumes playback)"] u: Option<url::Url>,
) -> anyhow::Result<()> {
let Some(u) = u else {
return _resume(ctx).await;
};
_play(ctx, &u).await
}
/// Pause audio playback.
#[poise::command(prefix_command, guild_only, category = "playback")]
pub async fn pause(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
let (_sb, call) = songbird(ctx).await?;
let call = call.lock().await;
call.queue().pause()?;
Ok(())
}
/// Resume audio playback.
#[poise::command(prefix_command, guild_only, aliases("continue"), category = "playback")]
pub async fn resume(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
_resume(ctx).await
}
async fn _resume(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
let (_sb, call) = songbird(ctx).await?;
let call = call.lock().await;
call.queue().resume()?;
Ok(())
}
/// Skip the current track in the queue.
#[poise::command(prefix_command, guild_only, category = "playback", aliases("next"))]
pub async fn skip(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
let (_sb, call) = songbird(ctx).await?;
let call = call.lock().await;
call.queue().skip()?;
Ok(())
}
/// Stop playing audio and delete the queue.
#[poise::command(
prefix_command,
guild_only,
category = "playback",
aliases("sudoku", "fuckoff", "stop", "kill")
)]
pub async fn die(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
let (_sb, call) = songbird(ctx).await?;
let mut call = call.lock().await;
call.queue().stop();
call.leave().await?;
Ok(())
}
/// List queued audio.
#[poise::command(prefix_command, guild_only, category = "playback", aliases("queue"))]
pub async fn list(ctx: PoiseContext<'_>) -> anyhow::Result<()> {
let (_sb, call) = songbird(ctx).await?;
let call = call.lock().await;
let queue = call.queue();
util::reply(ctx, "(command fix work-in-progress)").await?;
for track in queue.current_queue().into_iter() {
let info = track.get_info().await?;
let fmt = format!("track playing for {:?}", info.play_time);
util::reply(ctx, fmt).await?;
}
Ok(())
}
|