Esempio n. 1
0
    /// <summary>
    /// Gets the user avatar as a Texture2D and starts it with the supplied monobehaviour. It will first check the cache if the image exists, if it does it will return the image. Otherwise it will download the image from Discord and store it in the cache, calling the callback once done.
    /// <para>An alias of <see cref="DiscordUser.CacheAvatar(MonoBehaviour, DiscordAvatarSize, AvatarDownloadCallback)"/> and will return the new <see cref="DiscordUser"/> instance.</para>
    /// </summary>
    /// <param name="coroutineCaller">The target object that will start the coroutine</param>
    /// <param name="size">The target size of the avatar. Default is 128x128</param>
    /// <param name="callback">The callback for when the texture completes. Default is no-callback, but its highly recommended to use a callback</param>
    /// <returns>Returns the generated <see cref="DiscordUser"/> for this <see cref="DiscordRPC.User"/> object.</returns>
    public static DiscordUser GetAvatar(this DiscordRPC.User user, MonoBehaviour coroutineCaller, DiscordAvatarSize size = DiscordAvatarSize.x128, DiscordUser.AvatarDownloadCallback callback = null)
    {
        var du = new DiscordUser(user);

        du.GetAvatar(coroutineCaller, size, callback);
        return(du);
    }
Esempio n. 2
0
    public void SetMessage(DiscordRPC.Message.JoinRequestMessage message)
    {
        this.message = message;
        this.user    = message.User;

        Debug.Log("Invite Received for " + message.User);

        //Update the username
        username.text = message.User.Username + "#" + message.User.Discriminator;

        //Update the avatar
        //user.CacheAvatar(this, size: DiscordAvatarSize.x128, callback: (u, texture) =>	//Old depreciated way of doing this
        user.GetAvatar(this, size: DiscordAvatarSize.x128, callback: (u, texture) =>                    //New way of doing this, with clearer function names
        {
            Debug.Log("Downloaded Texture for Invite");
            avatar.texture = texture;
        });

        //Set our ignore start time
        _ignoreStartTime      = Time.time;
        _incrementIgnoreTimer = true;
    }
Esempio n. 3
0
    /// <summary>
    /// Initializes the discord client if able to. Wont initialize if <see cref="active"/> is false, we are not in playmode, we already have a instance or we already have a client.
    /// <para>This function is empty unless UNITY_WSA || UNITY_WSA_10_0 || UNITY_STANDALONE) && !DISABLE_DISCORD is meet.</para>
    /// </summary>
    public void Initialize()
    {
#if (UNITY_WSA || UNITY_WSA_10_0 || UNITY_STANDALONE) && !DISABLE_DISCORD
        if (!active)
        {
            return;                         //Are we allowed to be active?
        }
        if (!Application.isPlaying)
        {
            return;                         //We are not allowed to initialize while in the editor.
        }
        //This has a instance already that isn't us
        if (_instance != null && _instance != this)
        {
            Debug.LogWarning("[DAPI] Multiple DiscordManagers exist already. Destroying self.", _instance);
            Destroy(this);
            return;
        }

        //Make sure the client doesnt already exit
        if (_client != null)
        {
            Debug.LogError("[DAPI] Cannot initialize a new client when one is already initialized.");
            return;
        }

        //Assign the instance
        _instance = this;
        DontDestroyOnLoad(this);

        //Prepare the logger
        DiscordRPC.Logging.ILogger logger = null;

        //Update the logger to the unity logger
        if (Debug.isDebugBuild)
        {
            logger = new DiscordRPC.Logging.FileLogger("discordrpc.log")
            {
                Level = logLevel
            }
        }
        ;
        if (Application.isEditor)
        {
            logger = new DiscordRPC.Unity.UnityLogger()
            {
                Level = logLevel
            }
        }
        ;

        //We are starting the client. Below is a break down of the parameters.
        Debug.Log("[DRP] Starting Discord Rich Presence");
        _client = new DiscordRPC.DiscordRpcClient(
            applicationID,                                  //The Discord Application ID
            pipe: (int)targetPipe,                          //The target pipe to connect too
            logger: logger,                                 //The logger,
            autoEvents: false,                              //WE will manually invoke events
            client: new DiscordRPC.Unity.UnityNamedPipe()   //The client for the pipe to use. Unity MUST use a NativeNamedPipeClient since its managed client is broken.
            );

        if (registerUriScheme)
        {
            client.RegisterUriScheme(steamID);
        }

        //Subscribe to some initial events
        #region Event Registration
        client.OnError         += (s, args) => Debug.LogError("[DRP] Error Occured within the Discord IPC: (" + args.Code + ") " + args.Message);
        client.OnJoinRequested += (s, args) => Debug.Log("[DRP] Join Requested");

        client.OnReady += (s, args) =>
        {
            //We have connected to the Discord IPC. We should send our rich presence just incase it lost it.
            Debug.Log("[DRP] Connection established and received READY from Discord IPC. Sending our previous Rich Presence and Subscription.");


            //Set the user and cache their avatars
            _currentUser = args.User;
            _currentUser.GetAvatar(this, DiscordAvatarSize.x128);
        };
        client.OnPresenceUpdate += (s, args) =>
        {
            Debug.Log("[DRP] Our Rich Presence has been updated. Applied changes to local store.");
            Debug.Log(args.Presence.State);
            _currentPresence = (DiscordPresence)args.Presence;
        };
        client.OnSubscribe += (s, a) =>
        {
            Debug.Log("[DRP] New Subscription. Updating local store.");
            _currentSubscription = client.Subscription.ToUnity();
        };
        client.OnUnsubscribe += (s, a) =>
        {
            Debug.Log("[DRP] Removed Subscription. Updating local store.");
            _currentSubscription = client.Subscription.ToUnity();
        };

        //Register the unity events
        events.RegisterEvents(client);
        #endregion

        //Set initial presence and sub. (This will enqueue it)
        SetSubscription(_currentSubscription);
        SetPresence(_currentPresence);

        //Start the client
        _client.Initialize();
        Debug.Log("[DRP] Discord Rich Presence intialized and connecting...");
#endif
    }
    private void OnEnable()
    {
        //Make sure we are allowed to be active.
        if (!active || !gameObject.activeSelf)
        {
            return;
        }
        if (!Application.isPlaying)
        {
            return;
        }

        //This has a instance already that isn't us
        if (_instance != null && _instance != this)
        {
            Destroy(this);
            return;
        }

        //Assign the instance
        _instance = this;
        DontDestroyOnLoad(this);

        //We are starting the client. Below is a break down of the parameters.
        Debug.Log("[DRP] Starting Discord Rich Presence");
        _client = new DiscordRpcClient(
            applicationID,                                              //The Discord Application ID
            steamID,                                                    //The Steam App. This can be null or empty string to disable steam intergration.
            registerUriScheme,                                          //Should the client register a custom URI Scheme? This must be true for endpoints
            (int )targetPipe,                                           //The target pipe to connect too
            new NativeNamedPipeClient()                                 //The client for the pipe to use. Unity MUST use a NativeNamedPipeClient since its managed client is broken.
            );

        //Update the logger to the unity logger
        _client.Logger = new UnityLogger()
        {
            Level = logLevel
        };

        //Subscribe to some initial events
        #region Event Registration
        client.OnReady += (s, args) =>
        {
            //We have connected to the Discord IPC. We should send our rich presence just incase it lost it.
            Debug.Log("[DRP] Connection established and received READY from Discord IPC. Sending our previous Rich Presence and Subscription.");

            //Set the user and cache their avatars
            _currentUser = args.User;
            _currentUser.GetAvatar(this, DiscordAvatarSize.x128);
        };
        client.OnPresenceUpdate += (s, args) =>
        {
            Debug.Log("[DRP] Our Rich Presence has been updated. Applied changes to local store.");
            _currentPresence = (DiscordPresence)args.Presence;
        };
        client.OnSubscribe += (s, a) =>
        {
            Debug.Log("[DRP] New Subscription. Updating local store.");
            _currentSubscription = client.Subscription.ToUnity();
        };
        client.OnUnsubscribe += (s, a) =>
        {
            Debug.Log("[DRP] Removed Subscription. Updating local store.");
            _currentSubscription = client.Subscription.ToUnity();
        };
        client.OnError += (s, args) =>
        {
            //Something bad happened while we tried to send a event. We will just log this for clarity.
            Debug.LogError("[DRP] Error Occured within the Discord IPC: (" + args.Code + ") " + args.Message);
        };

        client.OnJoinRequested += (s, args) =>
        {
            Debug.Log("[DRP] Join Requested");
        };

        events.RegisterEvents(client);
        #endregion

        //Start the client
        _client.Initialize();
        Debug.Log("[DRP] Discord Rich Presence intialized and connecting...");

        //Set initial presence and sub. (This will enqueue it)
        SetSubscription(_currentSubscription);
        SetPresence(_currentPresence);
    }