aboutsummaryrefslogtreecommitdiff
path: root/src/commands/today/mod.rs
blob: 22e006c1c2f7849e979f1b354e70b1643d6f9d00 (plain) (blame)
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
use chrono::Duration;
use grate::tracing;
use lazy_static::lazy_static;
use rand::{
    seq::SliceRandom,
    thread_rng,
};
use songbird::input::{
    Compose,
    YoutubeDl,
};
use tap::Conv;

use crate::{
    bot::{
        HttpKey,
        PlaybackKey,
    },
    commands::{
        playback,
        playback::{
            songbird,
            InvokeInfo,
        },
    },
    util,
    PoiseContext,
};

mod prelude;

mod nov_5;
mod sept_21;

mod france;
mod halloween;
mod putin;
mod shrek;
mod ussr;

mod thursday;
mod tomorrow;
mod wednesday;

mod pianoman;

pub type TodayIter = Box<dyn Iterator<Item = TodayArgs>>;

#[derive(Clone, Debug, Hash, Default)]
pub struct TodayArgs {
    pub url:   &'static str,
    pub start: Option<Duration>,
    pub end:   Option<Duration>,
}

lazy_static! {
    static ref ALL: Vec<fn(chrono::NaiveDateTime) -> TodayIter> = vec![
        sept_21::sept_21,
        nov_5::nov_5,
        halloween::halloween,
        ussr::ussr,
        france::france,
        shrek::shrek,
        putin::putin,
        wednesday::wednesday,
        thursday::thursday,
        tomorrow::tomorrow,
        pianoman::pianoman,
    ];
}

#[poise::command(prefix_command, guild_only)]
pub async fn today(ctx: PoiseContext<'_>, #[rest] _rest: Option<String>) -> anyhow::Result<()> {
    let today = {
        #[allow(unused_mut)]
        let mut result = chrono::Local::now().naive_local();

        #[cfg(debug_assertions)]
        {
            if let Some(rest) = _rest {
                let dt = rest.parse::<chrono::NaiveDateTime>().or_else(|_| {
                    rest.parse::<chrono::NaiveDate>().map(|date| {
                        let time = chrono::NaiveTime::from_hms_opt(12, 0, 0).unwrap();
                        date.and_time(time)
                    })
                });

                match dt {
                    Ok(dt) => {
                        tracing::debug!("overriding with datetime: {dt}");
                        result = dt;
                    },
                    Err(e) => {
                        tracing::debug!("parsing datetime: {e:?}");
                    },
                };
            }
        }

        result
    };

    let options: Vec<TodayArgs> = ALL.iter().flat_map(|f| f(today)).collect();

    tracing::debug!(option_count = options.len(), %today);

    let play_args = options.choose(&mut thread_rng());

    if let Some(play_args) = play_args {
        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(());
        };

        let volume = util::volume(ctx).await;
        tracing::debug!(volume);

        let (_sb, call) = songbird(ctx).await?;
        let mut call = call.lock().await;

        if call.current_channel().is_none() {
            call.join(voice_channel).await?;
        }

        let client = {
            let data = ctx.serenity_context().data.read().await;
            data.get::<HttpKey>().unwrap().clone()
        };

        let mut input =
            YoutubeDl::new_ytdl_like("yt-dlp", client.clone(), play_args.url.conv::<String>());

        let meta = input.aux_metadata().await?;

        let handle = call.enqueue_input(input.into()).await;
        handle.set_volume(volume as _)?;

        let playback = {
            let data = ctx.serenity_context().data.read().await;
            data.get::<PlaybackKey>().unwrap().clone()
        };

        playback.insert(handle.uuid(), playback::Metadata {
            invoker:     ctx.author().id,
            invoke_info: InvokeInfo::Ytdl {
                metadata: meta,
            },
        });

        let q = call.queue();
        q.pause()?;
        q.modify_queue(move |q| {
            let last = q.pop_back();

            if let Some(last) = last {
                q.push_front(last);
            }
        });
        q.resume()?;
    } else {
        util::reply(ctx, "no").await?;
        util::reply(ctx, ":angry:").await?;
    }

    Ok(())
}