Lachee / discord-rpc-csharp

C# custom implementation for Discord Rich Presence. Not deprecated and still available!
MIT License
560 stars 94 forks source link

Buttons on C# #131

Closed decries closed 3 years ago

decries commented 3 years ago

I implemented this:

` DiscordRPC.client.SetPresence(new RichPresence() {

                            Details = "Details",
                            State = $"State",
                            Buttons = new Button()
                            {
                                Url = "https://google.com",
                                Label = "Discord Server",
                            },
                            Assets = new Assets()
                            {
                                LargeImageKey = "imakey",
                                LargeImageText = "text",

                            }

                        });`

And im getting this error: Cannot implicitly convert type 'DiscordRPC.Button' to 'DiscordRPC.Button[]'

Lachee commented 3 years ago

The error perfectly tells you what's wrong.

Buttons is an array, you are trying to assign a single object instead tho

decries commented 3 years ago

I don't understand that explanation, i just need a wait to fix it, lol.

decries commented 3 years ago

nvm, fixed lol

DeathlyBower959 commented 3 years ago

how was this fixed? I am having the same problem, and are new to c#

AndreZila01 commented 2 years ago

I have the same problem, ... NotLikeThis

Lachee commented 2 years ago

Buttons are an array. use an array of buttons.

If you do not know how arrays work, here is a Brackeys Tutorial for C# beginners.

AndreZila01 commented 2 years ago

@DeathlyBower959 I found one solution...

    DiscordRPC.Button[] btn = new DiscordRPC.Button[1];

    private void Discord(){

        client = new DiscordRpcClient(APPLICATION ID, -1);

        client.Initialize();
        string url = "https://google.com";
        string Botaria = "Botaria";
        btn[0] = JsonConvert.DeserializeObject<DiscordRPC.Button>("{\"Url\":\"" + url + "\", \"Label\":\"" + Botaria + "\"}");
        string temp = "";
        var rp = new RichPresence()
        {
            Details = "",
            State = "",
            Assets = new Assets()
            {
                SmallImageKey = "",
                LargeImageKey = "",
            },
            Buttons = btn,
        };

        rp.Timestamps = new Timestamps(start);
        client.SetPresence(rp);
    }
Lachee commented 2 years ago

that is.... one way to do it. However a better, cleaner way (that doesn't rely on JSON serialization (seriously dont do that just create the object)) would be the example provided https://github.com/Lachee/discord-rpc-csharp/blob/master/DiscordRPC.Example/Program.cs#L108-L111

            new RichPresence()
            {
                Details = "A Basic Example",
                State = "In Game",
                Timestamps = Timestamps.FromTimeSpan(10),
                Buttons = new Button[]
                {
                    new Button() { Label = "Fish", Url = "https://lachee.dev/" }
                }
            }
DeathlyBower959 commented 2 years ago

Yeah, I figured that out a while ago, there is no reason to use JSON serialization, there is no point. Lachee's solution is much better, that's what I had originally used, and I'm a little more accustomed to c# now.