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

Badge request: Discord online/total #4500

Open
elpiel opened this issue Jan 10, 2020 · 27 comments
Open

Badge request: Discord online/total #4500

elpiel opened this issue Jan 10, 2020 · 27 comments
Labels
good first issue New contributors, join in! service-badge New or updated service badge

Comments

@elpiel
Copy link

elpiel commented Jan 10, 2020

📋 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

🔗 Data

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

🎤 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 elpiel added the service-badge New or updated service badge label Jan 10, 2020
@calebcartwright
Copy link
Member

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
Copy link
Author

elpiel commented Jan 10, 2020

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
Copy link
Member

calebcartwright commented Jan 10, 2020

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
Copy link
Author

elpiel commented Jan 11, 2020

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 elpiel closed this as completed Jan 11, 2020
@calebcartwright calebcartwright added the needs-upstream-help Not actionable without help from a service provider label Jan 11, 2020
@calebcartwright
Copy link
Member

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

Let us know if you hear anything!

@elpiel
Copy link
Author

elpiel commented Jan 13, 2020

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 elpiel reopened this Jan 13, 2020
@calebcartwright
Copy link
Member

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

@calebcartwright
Copy link
Member

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

@elpiel
Copy link
Author

elpiel commented Jan 13, 2020

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 calebcartwright removed the needs-upstream-help Not actionable without help from a service provider label Jan 19, 2020
@calebcartwright
Copy link
Member

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 calebcartwright added the good first issue New contributors, join in! label Jan 19, 2020
@paulmelnikow paulmelnikow changed the title Discord online/total badge Badge request: Discord online/total Apr 5, 2020
@pedrofracassi
Copy link
Contributor

pedrofracassi commented Jul 29, 2020

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
Copy link
Member

PyvesB commented Aug 27, 2020

@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
Copy link

sdabhi23 commented Sep 9, 2020

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

@PyvesB
Copy link
Member

PyvesB commented Sep 9, 2020

@sdabhi23 sure, please pick it up! 👍🏻

@sdabhi23
Copy link

sdabhi23 commented Sep 9, 2020

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

@PyvesB
Copy link
Member

PyvesB commented Sep 9, 2020

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
Copy link
Author

elpiel commented Sep 9, 2020

@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
Copy link

sdabhi23 commented Sep 9, 2020

@PyvesB sure, will definitely do that!

@elpiel it is my pleasure 😃

@YHDiamond
Copy link

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
Copy link

DereC4 commented Mar 23, 2021

any luck

@YHDiamond
Copy link

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

@YHDiamond
Copy link

YHDiamond commented Mar 23, 2021

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
Copy link
Member

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
Copy link
Member

PyvesB commented May 4, 2021

I completely agree with @calebcartwright. Even if you're not familiar with JavaScript, there is an analysis aspect which I outlined in #5079 (comment). This doesn't require any programming knowledge, so do jump in, Shields.io is a community project. 😉

@zugazagoitia
Copy link

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
Copy link

huan commented Apr 16, 2024

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 added a commit to wechaty/wechaty that referenced this issue Apr 16, 2024
@DereC4

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue New contributors, join in! service-badge New or updated service badge
Projects
None yet
9 participants