Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rethink New function API #649

Open
UlisseMini opened this issue May 26, 2019 · 7 comments
Open

Rethink New function API #649

UlisseMini opened this issue May 26, 2019 · 7 comments

Comments

@UlisseMini
Copy link

UlisseMini commented May 26, 2019

Proof of concept implementation in #651

Problem

The New function does not express its intent in its type. it is also very unintuitive to use.

func New(args ...interface{}) (s *Session, err error)

The type signature tells the user nothing, i think this API was picked because of different ways to authenticate. I think this is a bad idea and it is more intuitive to use a struct.

Solution

Add a new more intuitive struct based API. something along the lines of this

dg, err := discordgo.WithAuth {
    Email: "foo@bar.com",
    Pass: "foobar",
    Token: "abc123",
    Bot: false,
}.Verify()

I'm using a method here to prevent me from having to type indiscordgo again. you could also do it like this

dg, err := discordgo.New(discordgo.WithAuth{
    Email: "foo@bar.com",
    Pass: "foobar",
    Token: "abc123",
    Bot: false,
})

In my opinion both are better then the current API, and could be added while remaining backwards compatible.

If this gets approved and i have time i'll implement this (it should not be that hard).

@iopred
Copy link
Collaborator

iopred commented May 26, 2019 via email

@diamondburned
Copy link

-1, You don't always need an email, a password and a token. I think the current system is fine as it is.

Luckily, since the current New function takes in an interface{}, you can add this without breaking, and I'm all up for that.

@UlisseMini
Copy link
Author

@diamondburned If you don't specify a field then it will be set to its zero value, exactly what we want.

for example, not specifying the Bot field would leave it to a default of false
not specifying an email and password would leave them to "" and we could check for that in the New / Verify functions

@bwmarrin
Copy link
Owner

bwmarrin commented May 28, 2019

Personally, if we're to break the current API on everyone - I'm more in favor of dropping the Email/Pass entirely and just accepting a single token string that we pass along to Discord. It's a closer match to the goal of just being a wrapper around Discord's API and it's simple and easy to use.

The only reason this hasn't been done is because of not wanting to break the API on everyone for such a minor thing. Now with modules and versions and such, I do feel better about making breaking changes to help clean up this library.

@UlisseMini
Copy link
Author

I agree with @bwmarrin, it would also make the API much simpler, something like

func New(token string) (*Session, error)

would be desirable

@ravener
Copy link

ravener commented Dec 13, 2019

You could also remove the error return value entirely in that case, since currently it's only used for paramater validation and fetching token from email/pass, if it takes only token no error would be returned.

@diamondburned
Copy link

My PR should add something like this without any breaking changes: #725

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants