/// <summary>
        /// Sets the end time of the <see cref="CurrentPresence"/> and sends the updated presence to Discord.
        /// </summary>
        /// <param name="time">The new time for the end</param>
        /// <returns>Updated Rich Presence</returns>
        public RichPresence UpdateEndTime(DateTime time)
        {
            if (!IsInitialized)
            {
                throw new UninitializedException();
            }

            //Clone the presence
            RichPresence presence;

            lock (_sync)
            {
                if (CurrentPresence == null)
                {
                    presence = new RichPresence();
                }
                else
                {
                    presence = CurrentPresence.Clone();
                }
            }

            //Update the value
            if (presence.Timestamps == null)
            {
                presence.Timestamps = new Timestamps();
            }
            presence.Timestamps.End = time;
            SetPresence(presence);
            return(presence);
        }
        /// <summary>
        /// Sets the start and end time of <see cref="CurrentPresence"/> to null and sends it to Discord.
        /// </summary>
        /// <returns>Updated Rich Presence</returns>
        public RichPresence UpdateClearTime()
        {
            if (!IsInitialized)
            {
                throw new UninitializedException();
            }

            //Clone the presence
            RichPresence presence;

            lock (_sync)
            {
                if (CurrentPresence == null)
                {
                    presence = new RichPresence();
                }
                else
                {
                    presence = CurrentPresence.Clone();
                }
            }

            //Update the value
            presence.Timestamps = null;
            SetPresence(presence);
            return(presence);
        }
        /// <summary>
        /// Updates the <see cref="Secrets"/> of the <see cref="CurrentPresence"/> and sends the updated presence to Discord. Will override previous secret entirely.
        /// </summary>
        /// <param name="secrets">The new secret to send to discord.</param>
        /// <returns>Updated Rich Presence</returns>
        public RichPresence UpdateSecrets(Secrets secrets)
        {
            if (!IsInitialized)
            {
                throw new UninitializedException();
            }

            //Clone the presence
            RichPresence presence;

            lock (_sync)
            {
                if (CurrentPresence == null)
                {
                    presence = new RichPresence();
                }
                else
                {
                    presence = CurrentPresence.Clone();
                }
            }

            //Update the value
            presence.Secrets = secrets;
            SetPresence(presence);
            return(presence);
        }
        /// <summary>
        /// Updates the small <see cref="Assets"/> of the <see cref="CurrentPresence"/> and sends the updated presence to Discord. Both <paramref name="key"/> and <paramref name="tooltip"/> are optional and will be ignored it null.
        /// </summary>
        /// <param name="key">Optional: The new key to set the asset too</param>
        /// <param name="tooltip">Optional: The new tooltip to display on the asset</param>
        /// <returns>Updated Rich Presence</returns>
        public RichPresence UpdateSmallAsset(string key = null, string tooltip = null)
        {
            if (!IsInitialized)
            {
                throw new UninitializedException();
            }
            //Clone the presence
            RichPresence presence;

            lock (_sync)
            {
                if (CurrentPresence == null)
                {
                    presence = new RichPresence();
                }
                else
                {
                    presence = CurrentPresence.Clone();
                }
            }

            //Update the value
            if (presence.Assets == null)
            {
                presence.Assets = new Assets();
            }
            presence.Assets.SmallImageKey  = key ?? presence.Assets.SmallImageKey;
            presence.Assets.SmallImageText = tooltip ?? presence.Assets.SmallImageText;
            SetPresence(presence);
            return(presence);
        }
        /// <summary>
        /// Updates the <see cref="Party.Size"/> of the <see cref="CurrentPresence"/> and sends the update presence to Discord. Returns the newly edited Rich Presence.
        /// <para>Will return null if no presence exists and will throw a new <see cref="NullReferenceException"/> if the Party does not exist.</para>
        /// </summary>
        /// <param name="size">The new size of the party. It cannot be greater than <see cref="Party.Max"/></param>
        /// <param name="max">The new size of the party. It cannot be smaller than <see cref="Party.Size"/></param>
        /// <returns>Updated Rich Presence</returns>
        public RichPresence UpdatePartySize(int size, int max)
        {
            if (!IsInitialized)
            {
                throw new UninitializedException();
            }

            //Clone the presence
            RichPresence presence;

            lock (_sync)
            {
                if (CurrentPresence == null)
                {
                    presence = new RichPresence();
                }
                else
                {
                    presence = CurrentPresence.Clone();
                }
            }

            //Ensure it has a party
            if (presence.Party == null)
            {
                throw new BadPresenceException("Cannot set the size of the party if the party does not exist");
            }

            //Update the value
            presence.Party.Size = size;
            presence.Party.Max  = max;
            SetPresence(presence);
            return(presence);
        }
        /// <summary>
        /// Updates only the <see cref="RichPresence.Party"/> of the <see cref="CurrentPresence"/> and sends the updated presence to Discord. Returns the newly edited Rich Presence.
        /// </summary>
        /// <param name="party">The party of the Rich Presence</param>
        /// <returns>Updated Rich Presence</returns>
        public RichPresence UpdateParty(Party party)
        {
            if (!IsInitialized)
            {
                throw new UninitializedException();
            }

            //Clone the presence
            RichPresence presence;

            lock (_sync)
            {
                if (CurrentPresence == null)
                {
                    presence = new RichPresence();
                }
                else
                {
                    presence = CurrentPresence.Clone();
                }
            }

            //Update the value
            presence.Party = party;
            SetPresence(presence);
            return(presence);
        }
        /// <summary>
        /// Updates only the <see cref="RichPresence.Details"/> of the <see cref="CurrentPresence"/> and sends the updated presence to Discord. Returns the newly edited Rich Presence.
        /// </summary>
        /// <param name="details">The details of the Rich Presence</param>
        /// <returns>Updated Rich Presence</returns>
        public RichPresence UpdateDetails(string details)
        {
            if (!IsInitialized)
            {
                throw new UninitializedException();
            }

            //Clone the presence
            RichPresence presence;

            lock (_sync)
            {
                if (CurrentPresence == null)
                {
                    presence = new RichPresence();
                }
                else
                {
                    presence = CurrentPresence.Clone();
                }
            }

            //Update the value
            presence.Details = details;
            SetPresence(presence);
            return(presence);
        }
Example #8
0
        /// <summary>
        /// Processes the message, updating our internal state and then invokes the events.
        /// </summary>
        /// <param name="message"></param>
        private void ProcessMessage(IMessage message)
        {
            if (message == null)
            {
                return;
            }
            switch (message.Type)
            {
            //We got a update, so we will update our current presence
            case MessageType.PresenceUpdate:
                lock (_sync)
                {
                    PresenceMessage pm = message as PresenceMessage;
                    if (pm != null)
                    {
                        //We need to merge these presences together
                        if (CurrentPresence == null)
                        {
                            CurrentPresence = pm.Presence;
                        }
                        else if (pm.Presence == null)
                        {
                            CurrentPresence = null;
                        }
                        else
                        {
                            CurrentPresence.Merge(pm.Presence);
                        }

                        //Update the message
                        pm.Presence = CurrentPresence;
                    }
                }

                break;

            //Update our configuration
            case MessageType.Ready:
                ReadyMessage rm = message as ReadyMessage;
                if (rm != null)
                {
                    lock (_sync)
                    {
                        Configuration = rm.Configuration;
                        CurrentUser   = rm.User;
                    }

                    //Resend our presence and subscription
                    SynchronizeState();
                }
                break;

            //Update the request's CDN for the avatar helpers
            case MessageType.JoinRequest:
                if (Configuration != null)
                {
                    //Update the User object within the join request if the current Cdn
                    JoinRequestMessage jrm = message as JoinRequestMessage;
                    if (jrm != null)
                    {
                        jrm.User.SetConfiguration(Configuration);
                    }
                }
                break;

            case MessageType.Subscribe:
                lock (_sync)
                {
                    SubscribeMessage sub = message as SubscribeMessage;
                    Subscription |= sub.Event;
                }
                break;

            case MessageType.Unsubscribe:
                lock (_sync)
                {
                    UnsubscribeMessage unsub = message as UnsubscribeMessage;
                    Subscription &= ~unsub.Event;
                }
                break;

            //We got a message we dont know what to do with.
            default:
                break;
            }

            //Invoke the appropriate methods
            switch (message.Type)
            {
            case MessageType.Ready:
                if (OnReady != null)
                {
                    OnReady.Invoke(this, message as ReadyMessage);
                }
                break;

            case MessageType.Close:
                if (OnClose != null)
                {
                    OnClose.Invoke(this, message as CloseMessage);
                }
                break;

            case MessageType.Error:
                if (OnError != null)
                {
                    OnError.Invoke(this, message as ErrorMessage);
                }
                break;

            case MessageType.PresenceUpdate:
                if (OnPresenceUpdate != null)
                {
                    OnPresenceUpdate.Invoke(this, message as PresenceMessage);
                }
                break;

            case MessageType.Subscribe:
                if (OnSubscribe != null)
                {
                    OnSubscribe.Invoke(this, message as SubscribeMessage);
                }
                break;

            case MessageType.Unsubscribe:
                if (OnUnsubscribe != null)
                {
                    OnUnsubscribe.Invoke(this, message as UnsubscribeMessage);
                }
                break;

            case MessageType.Join:
                if (OnJoin != null)
                {
                    OnJoin.Invoke(this, message as JoinMessage);
                }
                break;

            case MessageType.Spectate:
                if (OnSpectate != null)
                {
                    OnSpectate.Invoke(this, message as SpectateMessage);
                }
                break;

            case MessageType.JoinRequest:
                if (OnJoinRequested != null)
                {
                    OnJoinRequested.Invoke(this, message as JoinRequestMessage);
                }
                break;

            case MessageType.ConnectionEstablished:
                if (OnConnectionEstablished != null)
                {
                    OnConnectionEstablished.Invoke(this, message as ConnectionEstablishedMessage);
                }
                break;

            case MessageType.ConnectionFailed:
                if (OnConnectionFailed != null)
                {
                    OnConnectionFailed.Invoke(this, message as ConnectionFailedMessage);
                }
                break;

            default:
                //This in theory can never happen, but its a good idea as a reminder to update this part of the library if any new messages are implemented.
                Logger.Error("Message was queued with no appropriate handle! {0}", message.Type);
                break;
            }
        }