shields icon indicating copy to clipboard operation
shields copied to clipboard

Badge request: Discord online/total

Open elpiel opened this issue 5 years ago • 27 comments

: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.

elpiel avatar Jan 10 '20 09:01 elpiel

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.

calebcartwright avatar Jan 10 '20 15:01 calebcartwright

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....

elpiel avatar Jan 10 '20 15:01 elpiel

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

calebcartwright avatar Jan 10 '20 16:01 calebcartwright

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!

elpiel avatar Jan 11 '20 10:01 elpiel

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

Let us know if you hear anything!

calebcartwright avatar Jan 11 '20 15:01 calebcartwright

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.

elpiel avatar Jan 13 '20 13:01 elpiel

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

calebcartwright avatar Jan 13 '20 14:01 calebcartwright

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

calebcartwright avatar Jan 13 '20 14:01 calebcartwright

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.

elpiel avatar Jan 13 '20 15:01 elpiel

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

calebcartwright avatar Jan 19 '20 17:01 calebcartwright

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

pedrofracassi avatar Jul 29 '20 11:07 pedrofracassi

@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?

PyvesB avatar Aug 27 '20 09:08 PyvesB

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

sdabhi23 avatar Sep 09 '20 14:09 sdabhi23

@sdabhi23 sure, please pick it up! 👍🏻

PyvesB avatar Sep 09 '20 15:09 PyvesB

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

sdabhi23 avatar Sep 09 '20 16:09 sdabhi23

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!

PyvesB avatar Sep 09 '20 17:09 PyvesB

@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 😋

elpiel avatar Sep 09 '20 17:09 elpiel

@PyvesB sure, will definitely do that!

@elpiel it is my pleasure 😃

sdabhi23 avatar Sep 09 '20 18:09 sdabhi23

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.

YHDiamond avatar Feb 22 '21 02:02 YHDiamond

any luck

DereC4 avatar Mar 23 '21 03:03 DereC4

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

YHDiamond avatar Mar 23 '21 10:03 YHDiamond

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

YHDiamond avatar Mar 23 '21 14:03 YHDiamond

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.

calebcartwright avatar Mar 24 '21 03:03 calebcartwright

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. 😉

PyvesB avatar May 04 '21 21:05 PyvesB

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)

zugazagoitia avatar Aug 13 '23 16:08 zugazagoitia

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?

huan avatar Apr 16 '24 23:04 huan

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

DereC4 avatar Apr 17 '24 07:04 DereC4