aboutsummaryrefslogtreecommitdiff
path: root/src/modules/commands/general/voice.rs
blob: 1849f783bdae466f7417371906015cce623e12e4 (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
// use crate::core::model::ApiClient;
// use crate::core::consts::*;
use crate::core::model::*;
use serenity::framework::standard::{
    Args,
    Command,
    CommandError,
    CommandOptions
};
use serenity::model::channel::Message;
use serenity::prelude::Context;
use std::sync::Arc;

pub struct Join;
impl Command for Join {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            // desc: Some("I see your a individual of culture...".to_string()),
            // usage: Some("[tags]".to_string()),
            // example: Some("minecraft".to_string()),
            guild_only: true,
            owner_privileges: false,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, ctx: &mut Context, message: &Message, _args: Args) -> Result<(), CommandError> {
        // if not deafened, ask to please deeafen
        let channel_id = message.guild().unwrap().read()
            .voice_states.get(&message.author.id)
            .and_then(|voice_state| voice_state.channel_id);

        let connect_to = match channel_id {
            Some(channel) => channel,
            None => {
                message.channel_id.say("You aren't in a voice channel...")?;

                return Ok(())
            }
        };

        let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned()
            .expect("Expected VoiceManager in ShareMap."); // mut
        let mut manager = manager_lock.lock();
        let has_handler = manager.get(message.guild().unwrap().read().id).is_some();

        if has_handler {
            message.channel_id.say(format!("Sorry, but I'm already in <#{}>.", connect_to))?;
        } else {
            if manager.join(message.guild().unwrap().read().id, connect_to).is_some() {
                message.channel_id.say(format!("I'm now in <#{}>.", connect_to))?;
            } else {
                message.channel_id.say("I've encountered an error while trying to join your voice channel.")?;
            }
        }

        Ok(())
    }
}

pub struct Leave;
impl Command for Leave {
    fn options(&self) -> Arc<CommandOptions> {
        let default = CommandOptions::default();
        let options = CommandOptions {
            // desc: Some("I see your a individual of culture...".to_string()),
            // usage: Some("[tags]".to_string()),
            // example: Some("minecraft".to_string()),
            guild_only: true,
            owner_privileges: false,
            ..default
        };
        Arc::new(options)
    }

    fn execute(&self, ctx: &mut Context, message: &Message, _args: Args) -> Result<(), CommandError> {
        let channel_id = message.guild().unwrap().read()
            .voice_states.get(&message.author.id)
            .and_then(|voice_state| voice_state.channel_id);

        let connect_to = match channel_id {
            Some(channel) => channel,
            None => {
                message.channel_id.say("You aren't in a voice channel... Only the people within a given voice channel can make me leave.")?;

                return Ok(())
            }
        };

        let manager_lock = ctx.data.lock().get::<VoiceManager>().cloned()
            .expect("Expected VoiceManager in ShareMap."); // mut
        let mut manager = manager_lock.lock();
        let has_handler = manager.get(message.guild().unwrap().read().id).is_some();

        if has_handler {
            manager.remove(message.guild().unwrap().read().id);

            message.channel_id.say(format!("I've left <#{:?}>.",
                    manager.get(message.guild().unwrap().read().id)))?;
        } else {
            message.channel_id.say("Sorry, but I'm not in a voice channel right now.")?;
        }

        Ok(())
    }
}