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
//! Provides types and functionality for the Discord [Overlay](https://discord.com/developers/docs/game-sdk/overlay)

pub mod events;

use crate::{Command, CommandKind, Error};
use serde::Serialize;

#[derive(Serialize)]
struct OverlayToggle {
    /// Our process id, this lets Discord know what process it should try
    /// to show the overlay in
    pid: u32,
    #[serde(rename = "locked")]
    visibility: Visibility,
}

impl OverlayToggle {
    fn new(visibility: Visibility) -> Self {
        Self {
            pid: std::process::id(),
            visibility,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Visibility {
    Visible,
    Hidden,
}

impl Serialize for Visibility {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_bool(!(*self == Self::Visible))
    }
}

impl<'de> serde::Deserialize<'de> for Visibility {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de;

        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = Visibility;

            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                formatter.write_str("a boolean")
            }

            fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(if value {
                    Visibility::Hidden
                } else {
                    Visibility::Visible
                })
            }
        }

        deserializer.deserialize_bool(Visitor)
    }
}

#[derive(Copy, Clone, PartialEq, serde_repr::Serialize_repr)]
#[repr(u8)]
pub enum InviteAction {
    Join = 1,
    Spectate = 2,
}

#[derive(Serialize)]
pub(crate) struct OverlayPidArgs {
    pid: u32,
}

impl OverlayPidArgs {
    pub(crate) fn new() -> Self {
        Self {
            pid: std::process::id(),
        }
    }
}

impl crate::Discord {
    /// Opens or closes the overlay. If the overlay is not enabled this will
    /// instead focus the Discord app itself.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/overlay#setlocked)
    pub async fn set_overlay_visibility(&self, visibility: Visibility) -> Result<(), Error> {
        let rx = self.send_rpc(
            CommandKind::SetOverlayVisibility,
            OverlayToggle::new(visibility),
        )?;

        handle_response!(rx, Command::SetOverlayVisibility => {
            Ok(())
        })
    }

    /// Opens the overlay modal for sending game invitations to users, channels,
    /// and servers.
    ///
    /// # Errors
    /// If you do not have a valid activity with all the required fields, this
    /// call will error. See
    /// [Activity Action Field Requirements](https://discord.com/developers/docs/game-sdk/activities#activity-action-field-requirements)
    /// for the fields required to have join and spectate invites function properly.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/overlay#openactivityinvite)
    pub async fn open_activity_invite(&self, action: InviteAction) -> Result<(), Error> {
        #[derive(Serialize)]
        struct OpenInviteModal {
            /// Our process id, this lets Discord know what process it should try
            /// to show the overlay in
            pid: u32,
            #[serde(rename = "type")]
            kind: InviteAction,
        }

        let rx = self.send_rpc(
            CommandKind::OpenOverlayActivityInvite,
            OpenInviteModal {
                pid: std::process::id(),
                kind: action,
            },
        )?;

        handle_response!(rx, Command::OpenOverlayActivityInvite => {
            Ok(())
        })
    }

    /// Opens the overlay modal for joining a Discord guild, given its invite code.
    /// Unlike the normal SDK, this method automatically parses the code from
    /// the provided string so you don't need to do it yourself.
    ///
    /// Note that just because the result might be [`Result::Ok`] doesn't
    /// necessarily mean the user accepted the invite.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/overlay#openguildinvite)
    pub async fn open_guild_invite(&self, code: impl AsRef<str>) -> Result<(), Error> {
        let mut code = code.as_ref();

        if let Some(rest) = code.strip_prefix("https://") {
            code = rest;
        }

        if let Some(rest) = code.strip_prefix("discord.gg/") {
            code = rest;
        } else if let Some(rest) = code.strip_prefix("discordapp.com/invite/") {
            code = rest;
        }

        #[derive(Serialize)]
        struct OpenGuildInviteModal<'stack> {
            pid: u32,
            code: &'stack str,
        }

        let rx = self.send_rpc(
            CommandKind::OpenOverlayGuildInvite,
            OpenGuildInviteModal {
                pid: std::process::id(),
                code,
            },
        )?;

        handle_response!(rx, Command::OpenOverlayGuildInvite => {
            Ok(())
        })
    }

    /// Opens the overlay widget for voice settings for the currently connected application.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/overlay#openvoicesettings)
    pub async fn open_voice_settings(&self) -> Result<(), Error> {
        let rx = self.send_rpc(CommandKind::OpenOverlayVoiceSettings, OverlayPidArgs::new())?;

        handle_response!(rx, Command::OpenOverlayVoiceSettings => {
            Ok(())
        })
    }
}