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

user discord online status is not checking everyone #405

Open
climu opened this issue May 25, 2020 · 8 comments
Open

user discord online status is not checking everyone #405

climu opened this issue May 25, 2020 · 8 comments
Labels

Comments

@climu
Copy link
Owner

climu commented May 25, 2020

When we scrape, we update discord users online status:

#finally discord
#first we set everyone to offline
DiscordUser.objects.all().update(status='offline')
discord_users = requests.get('https://discordapp.com/api/guilds/287487891003932672/widget.json').json()['members']
for user in discord_users:
discord_user = DiscordUser.objects.filter(uid=user['id']).first()
if discord_user is not None:
discord_user.status = user['status']
discord_user.save()

Then we display it like that:
image

I just realised that this url only show first 100 users: https://discordapp.com/api/guilds/287487891003932672/widget.json

We should find another way to check who is online.

@climu climu added the bug label May 25, 2020
@climu
Copy link
Owner Author

climu commented May 26, 2020

So I read this: https://discord.com/developers/docs/resources/guild#guild-object-guild-structure.
I try:
curl -H 'Authorization: Bot MYBOTTOKEN' https://discordapp.com/api/guilds/287487891003932672/members?/
but it only returns one user.
I try
curl -H 'Authorization: Bot MYBOTTOKEN' https://discordapp.com/api/guilds/287487891003932672/presences?/
and get a 404 :(

@raylu
Copy link
Collaborator

raylu commented May 26, 2020

the question mark after the field means it's optional. those are fields in the responses to /api/guilds/:id, not the member list. I think it's optional because the websocket gateway won't send it to you unless you opt your bot into the presence privileged intent

@climu
Copy link
Owner Author

climu commented May 28, 2020

Thanks for clarification. I still couldn't manage to get the wanted information from discord api.

@lime-green
Copy link
Contributor

lime-green commented May 31, 2020

@climu @raylu

OK I looked into this today:

so I propose:

  1. Create a new API endpoint /api/discord/online_statuses/ or similar that saves a user's status if they exist in the DB
  2. In the discord bot, create a cog (https://discordpy.readthedocs.io/en/latest/ext/tasks/) that runs the following task every ~5 minutes:
  • gathers all the members in bot: bot.get_guild(guild_id).members
  • builds a status payload of the form: [{user_id: "...", status: "online|offline|etc."}] using member.status
  • POST to the endpoint

EDIT:
We could also listen to the event and only send users who’s statuses have changed as a performance improvement

@climu
Copy link
Owner Author

climu commented Jun 1, 2020

Thanks for the investigation!
I think this can work.

I have one security wonder: The API endpoint would change our database and I am reluctant to open that to everyone.

Maybe we could do a trick: The bot read a text file /etc/discord_bot_password containing a random string and pass it to the POST request. Then in the view we can open the very same file and check that it matches.

Is my security concern not relevant? Is the suggested security check ok?

@raylu
Copy link
Collaborator

raylu commented Jun 1, 2020

the bot and the db are on the same server. no need to post

I think this is pretty overkill and I would personally settle for the discord widget, though

@climu
Copy link
Owner Author

climu commented Jun 1, 2020

the bot and the db are on the same server. no need to post

You mean we could connect the bot to OSR db directly? Isn't it tricky without django ORM?

I think this is pretty overkill and I would personally settle for the discord widget, though

Well discord widget would look like : https://discord.com/widget?id=287487891003932672&theme=light

It just show a global activity count and we have this working allready:
image

The idea here is to show users online status on users links. Is it really needed? it's not clear indeed.

@raylu
Copy link
Collaborator

raylu commented Jun 1, 2020

the bot and the db are on the same server. no need to post

You mean we could connect the bot to OSR db directly? Isn't it tricky without django ORM?

just install psycopg2 in the bot's venv

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

No branches or pull requests

3 participants