badges / shields

Concise, consistent, and legible badges in SVG and raster format
https://shields.io
Creative Commons Zero v1.0 Universal
23.38k stars 5.49k forks source link

Badge request: Discord online/total #4500

Open elpiel opened 4 years ago

elpiel commented 4 years ago

:clipboard: Description

A discord badge as the one currently available, with the only difference, that it shows the total number of people in the server as well:

E.g. Discord: 15/50 online

:link: Data

I am not sure if there is such an endpoint with the available data in the Discord API.

:microphone: Motivation

Showing the number of online users is good, but sometimes you want to also show the number of people that have joined the server in total. It's good to show the total interest in your project/server.

calebcartwright commented 4 years ago

Much like #4257, I suspect this most likely is not going to be possible. The Discord APIs are very limited in terms of being able to provide the underlying info like this that Shields would need to be able to render such a badge.

We can leave this open for a few days in case anyone is able to find and provide details on an accessible API endpoint that includes this data, but otherwise this will be blocked due to lack of upstream data availability.

elpiel commented 4 years ago

I was able to take a look, technically it's possible to get all users and count them?! https://discordapp.com/developers/docs/resources/guild#list-guild-members Although it won't be very performant and if there are more than >1000 it needs to handle pagination....

calebcartwright commented 4 years ago

Do you know if that endpoint is available for consumption by non-members? The issues we've had in the past stem from the authorization requirements of the APIs, it's not the lack of the endpoints existing.

(assuming you aren't a member of the below server and authenticated) you'll get 401 errors on all the guild api endpoints https://discordapp.com/api/guilds/102860784329052160 https://discordapp.com/api/guilds/102860784329052160/members

We have to use the https://discordapp.com/api/guilds/<<server id>>/widget.json endpoint like https://discordapp.com/api/guilds/102860784329052160/widget.json so for this to be possible the data would most likely need to exist there.

There is a presence_count field in that response, though I'm not sure what exactly that represents

elpiel commented 4 years ago

presence_count is the online count. Sadly I didn't think about the authentication part. I think discord doesn't have an endpoint for such a thing... I might write to them and maybe(?) reopen this issue once they provide an endpoint.

Thank you!

calebcartwright commented 4 years ago

I might write to them and maybe(?) reopen this issue once they provide an endpoint.

Let us know if you hear anything!

elpiel commented 4 years ago

I've asked in their discord api server and got this response:

if you have an invite for the guild, you can get /invites/{invite.code} with the url param with_counts set to true to get an approx member count. https://discordapp.com/developers/docs/resources/invite#get-invite

I guess this will need special impl. for that specific badge? But at least it is possible to get an approx. of the users.

calebcartwright commented 4 years ago

Thanks @elpiel - any idea how "approximate" that count is?

calebcartwright commented 4 years ago

Also are the invites specific to a server or inidvidual channels (or both)?

elpiel commented 4 years ago

I about the approximation - I have no idea... I didn't ask as well. As for the invite I will assume, you can do both, give the Delete endpoint:

Delete an invite. Requires the MANAGE_CHANNELS permission on the channel this invite belongs to, or MANAGE_GUILD to remove any invite across the guild. Returns an invite object on success. ( I interpret this as channel and guild invites )

I haven't worked with their API so I can't be sure.

calebcartwright commented 4 years ago

Removing the needs-upstream-help label as it seems, at least for the moment, that we can use the invite API to get the data.

AFAICT the data seems reasonable to use, and the value in the response for our invite code is the same as what I see on our Shields server

https://discordapp.com/api/invites/HjJCwm5?with_counts=true

{
  "code": "HjJCwm5",
  "guild": {
    "id": "308323056592486420",
    "name": "shields",
    "splash": null,
    "banner": null,
    "description": null,
    "icon": "11a50197f2858fa14b74f41ceaacc4b6",
    "features": [],
    "verification_level": 1,
    "vanity_url_code": null
  },
  "channel": {
    "id": "308323056592486420",
    "name": "contributing",
    "type": 0
  },
  "inviter": {
    "id": "306496561301684224",
    "username": "paulmelnikow",
    "avatar": "7667ed8ea335982730a320c36102fc32",
    "discriminator": "9778"
  },
  "approximate_member_count": 336,
  "approximate_presence_count": 22
}

image

pedrofracassi commented 4 years ago

The widget endpoint can also be used to pull the presence count. It requires the server admins to have enabled it, but it's permanent, while invites can expire after some time or be deleted by accident if someone doesn't know it's being used by shields.io.

Widgets can be enabled through Server Settings > Widgets, and the json endpoint can be copied there too. That endpoint requires no authentication.

Here's an example: https://discordapp.com/api/guilds/445203868624748555/widget.json

PyvesB commented 4 years ago

@elpiel and @pedrofracassi there's an open pull request for this, #5079, unfortunately its author is no longer responding. Would one of you be interested in picking things up again and helping land the pull request?

sdabhi23 commented 4 years ago

@PyvesB I would like to help with #5079 if any one is not currently working on it

PyvesB commented 4 years ago

@sdabhi23 sure, please pick it up! 👍🏻

sdabhi23 commented 4 years ago

Do you mind if I raise a new PR as there seems to be a merge conflict on the old one?

PyvesB commented 4 years ago

Do you mind if I raise a new PR as there seems to be a merge conflict on the old one?

If it's easier for you, that's fine. My only suggestion would be to include the Co-authored-by tag in the commit message (https://github.blog/2018-01-29-commit-together-with-co-authors) so that the original author is given attribution as well!

elpiel commented 4 years ago

@PyvesB I would like to help with #5079 if any one is not currently working on it

Ohhh cool! Thanks for taking the time to work on it 😋

sdabhi23 commented 4 years ago

@PyvesB sure, will definitely do that!

@elpiel it is my pleasure 😃

YHDiamond commented 3 years ago

I think discord invites show member count, but that might be member count when the invite was created. If it contains a dynamic member count then we can have it take a permanent discord invite theoretically.

DereC4 commented 3 years ago

any luck

YHDiamond commented 3 years ago

If this was in Java i'd probably be able to do it but i only know js 😅

YHDiamond commented 3 years ago

I figured out how you can do this, if you query https://discord.com/api/invites/{invitecode}?with_counts=true it'll return a json object that contains "approximate_member_count":1332, "approximate_presence_count":429 approximate member count is the member count, approximate presence count is the number of online members. if we ask for a permanent invite code we can use that code and query the API to get the online members

EDIT: this was already said oops

thx @jackson-57 for helping me with this

calebcartwright commented 3 years ago

Gentle reminder to folks that the ping-like messages are not particularly helpful, especially given the presence of the open PR in #5079 which has some outstanding issues.

If this is a badge you'd really like to see, then we'd encourage you to try to pick up #5079 and carry it over the finish line. If you are interested in this badge but don't have the bandwidth to work on it, then please subscribe to this issue (and perhaps #5079).

Updates will be posted as and when there's an update to share.

PyvesB commented 3 years ago

I completely agree with @calebcartwright. Even if you're not familiar with JavaScript, there is an analysis aspect which I outlined in https://github.com/badges/shields/pull/5079#issuecomment-690733584. This doesn't require any programming knowledge, so do jump in, Shields.io is a community project. 😉

zugazagoitia commented 1 year ago

For anyone looking at this, you can use the Dynamic JSON Badge using https://discord.com/api/invites/INVITE_ID?with_counts=true asurl and $.approximate_presence_count as query.

Just add a discord logo and a label.

Example with the Javalin discord server: Dynamic JSON Badge ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fdiscord.com%2Fapi%2Finvites%2Fsgak4e5NKv%3Fwith_counts%3Dtrue&query=%24.approximate_presence_count&logo=discord&logoColor=white&label=users%20online&color=green)

huan commented 4 months ago

To get a total member number, we need to use approximate_member_count instead of approximate_presence_count :-D

Like my Wechaty:

Wechaty Discord

![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?
url=https%3A%2F%2Fdiscord.com%2Fapi%2Finvites%2F7q8NBZbQzt%3Fwith_counts%3Dtrue&query=%24.approximate_member_count&logo=discord&logoColor=white&label=Users%20Total&color=green)

It would be great if we could use some string concatenate in the query like ${%24.approximate_presence_count}/${%24.approximate_member_count}, anyone familiar with this syntax?

DereC4 commented 4 months ago

I can't believe I was in high school back when I was on this thread for the first time