Esempio n. 1
0
        public async static void Init()
        {
            /*
             * Create a discord client
             * NOTE:    If you are using Unity3D, you must use the full constructor and define
             *       the pipe connection.
             */
            client = new DiscordRpcClient(GlobalVar.Web.Discord.id)
            {
                //Set the logger
                // Logger = new ConsoleLogger() { Level = LogLevel.Warning }
            };


            //Subscribe to events
            client.OnReady += (sender, e) =>
            {
                //  MessageBox.Show("Received Ready from user {0}", e.User.Username);
                // Passes data onto the globalvar string values so the client can utilize it
                GlobalVar.Web.Discord.user = e.User.Username;
                User.AvatarSize size = new User.AvatarSize();
                GlobalVar.Web.Discord.avatar = e.User.GetAvatarURL(User.AvatarFormat.PNG, size);
            };


            client.OnPresenceUpdate += (sender, e) =>
            {
            };

            //Connect to the RPC
            client.Initialize();
            //Set the rich presence
            //Call this as many times as you want and anywhere in your code.

            await Task.Delay(2000);

            if (client.IsInitialized)
            {
                client.SetPresence(new RichPresence()
                {
                    Details = $"User: {GlobalVar.Web.Discord.user}",
                    State   = GlobalVar.Web.Discord.state,
                    Assets  = new Assets()
                    {
                        LargeImageKey  = GlobalVar.Web.Discord.largeImg,
                        LargeImageText = Theme.Client.Name,
                        SmallImageKey  = ""
                    }
                });
            }
        }
Esempio n. 2
0
        public string GetAvatarURL(User.AvatarFormat format, User.AvatarSize size = User.AvatarSize.x128)
        {
            string str = "/avatars/" + (object)this.ID + "/" + this.Avatar;

            if (string.IsNullOrEmpty(this.Avatar))
            {
                if (format != User.AvatarFormat.PNG)
                {
                    throw new BadImageFormatException("The user has no avatar and the requested format " + format.ToString() + " is not supported. (Only supports PNG).");
                }
                str = "/embed/avatars/" + (object)(this.Discriminator % 5);
            }
            return(string.Format("https://{0}{1}{2}?size={3}", (object)this.CdnEndpoint, (object)str, (object)this.GetAvatarExtension(format), (object)(int)size));
        }
    private static IEnumerator EnumerateAvatarTexture(this User user, User.AvatarSize size = User.AvatarSize.x128, TextureFormat format = TextureFormat.RGBA32, bool isJPEG = false, AvatarDownloadCallback callback = null)
    {
        if (string.IsNullOrEmpty(user.Avatar))
        {
            yield return(GetDefaultAvatarTexture(user, size, callback));
        }
        else
        {
            //Generate the path name
            string filename = string.Format("{0}-{1}{2}.{3}", user.Discriminator, user.Avatar, size.ToString(), isJPEG ? "jpeg" : "png");
            string path     = Path.Combine(_cache, filename);

            //The holder texture is null, so we should create new one
            Texture2D holder = new Texture2D((int)size, (int)size, format, false);

            //Check if the file exists
            if (File.Exists(path))
            {
                //Load the image
                var bytes = File.ReadAllBytes(path);
                holder.LoadImage(bytes);
            }
            else
            {
                using (WWW www = new WWW(user.GetAvatarURL(isJPEG ? User.AvatarFormat.JPEG : User.AvatarFormat.PNG, size)))
                {
                    //Download the texture
                    yield return(www);

                    //Update the holder
                    www.LoadImageIntoTexture(holder);

                    //Save the texture, making a cache for later
                    if (!Directory.Exists(_cache))
                    {
                        Directory.CreateDirectory(_cache);
                    }
                    var bytes = isJPEG ? holder.EncodeToJPG(JpegQuality) : holder.EncodeToPNG();
                    File.WriteAllBytes(path, bytes);
                }
            }

            //Execute the callback (if any)
            if (callback != null)
            {
                callback.Invoke(user, holder);
            }
        }
    }
    private static IEnumerator GetDefaultAvatarTexture(this User user, User.AvatarSize size = User.AvatarSize.x128, AvatarDownloadCallback callback = null)
    {
        int discrim = user.Discriminator % 5;

        string filename = string.Format("default-{0}{1}.png", discrim, size.ToString());
        string path     = Path.Combine(_cache, filename);

        Texture2D texture;

        //Check if the file exists
        if (File.Exists(path))
        {
            //Load the image
            var bytes = File.ReadAllBytes(path);
            texture = new Texture2D((int)size, (int)size, TextureFormat.RGBA32, false);
            texture.LoadImage(bytes);
        }
        else
        {
            string url = string.Format("https://{0}/embed/avatars/{1}.png?size={2}", user.CdnEndpoint, discrim, size);
            using (WWW www = new WWW(url))
            {
                //Download the texture
                yield return(www);

                //Set the texture
                texture = www.texture;

                //Save the texture, making a cache for later
                if (!Directory.Exists(_cache))
                {
                    Directory.CreateDirectory(_cache);
                }
                var bytes = texture.EncodeToPNG();
                File.WriteAllBytes(path, bytes);
            }
        }

        //Execute the callback (if any)
        if (callback != null)
        {
            callback.Invoke(user, texture);
        }
    }
 private static void GetAvatarTexture(this User user, MonoBehaviour reference, User.AvatarSize size = User.AvatarSize.x128, TextureFormat format = TextureFormat.RGBA32, bool isJPEG = false, AvatarDownloadCallback callback = null)
 {
     reference.StartCoroutine(EnumerateAvatarTexture(user, size, format, isJPEG, callback));
 }