'Python / Discord MEE6 - Use python to get a user's level on the MEE6 discord bot

I am using python and is trying to get a user's level on the well known MEE6 bot's leveling system. I can't seem to find any way

I came across mee6-py-api, but it doesn't work for me. Seems like it's outdated, when I tried this code on my terminal

from mee6_py_api import API
from asyncio import run

mee6API = API(your_guild_id) #i enter my guild id here
run(mee6API.levels.get_user_level(user_id)) #i enter my user id here, asyncio.run basically just allows me to run asynchronous functions without it being inside an asynchronous function.

it throws me the giant error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 608, in run_until_complete
    return future.result()
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\site-packages\mee6_py_api\plugins\levels.py", line 55, in get_user_xp
    user_details = await self.get_user_details(user_id, guild_id, dont_use_cache, page_count_limit)
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\site-packages\mee6_py_api\plugins\levels.py", line 42, in get_user_details
    leaderboard_pages = await self.get_all_leaderboard_pages(page_count_limit = page_count_limit, guild_id=guild_id)
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\site-packages\mee6_py_api\plugins\levels.py", line 28, in get_all_leaderboard_pages
    leaderboard_page = await self.get_leaderboard_page(i, guild_id)
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\site-packages\mee6_py_api\plugins\levels.py", line 20, in get_leaderboard_page
    result = await self.base_API.send_get_request_and_get_response(url, {'page': page})
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\site-packages\mee6_py_api\api.py", line 87, in send_get_request_and_get_response
    check_http_response_for_errors(response, resp_status, resp_headers)
  File "C:\Users\junya\AppData\Local\Programs\Python\Python38\lib\site-packages\mee6_py_api\api.py", line 27, in check_http_response_for_errors
    raise get_http_exception_for_status(resp_status)("Received an unknown bad response from server. \n"
mee6_py_api.exceptions.UnauthorizedError: Received an unknown bad response from server.
response: {'error': {'message': "The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required."}, 'status_code': 401}
status: 401
header: <CIMultiDictProxy('Date': 'Fri, 15 Apr 2022 05:05:59 GMT', 'Content-Type': 'application/json', 'Content-Length': '263', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': 'https://mee6.xyz', 'CF-Cache-Status': 'DYNAMIC', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Report-To': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=ZPihimYGk2pKQiYl9q2W9T%2FlDlnyh5D6w1hQ4yov09TbRva%2FtAW7cCi%2FHgCV%2BR81mR1mD6S%2FD6LU1JMhWc1EAu%2B4nQ9lVyvPkRiLimUBfJsqh5PqOIxj9LKA"}],"group":"cf-nel","max_age":604800}', 'NEL': '{"success_fraction":0,"report_to":"cf-nel","max_age":604800}', 'Server': 'cloudflare', 'CF-RAY': '6fc2263959f5490c-SIN')>

It'll be very nice if there is somehow a fix to this, but I do not think so. What I'm looking for is one that just works properly. Thanks,



Solution 1:[1]

I will not bother finding a fix for mee6-py-api, here is a better way to do it.

So turns out MEE6 has an api, and you can use something like the requests package to get the the json of a whole leaderboard of a guild by sending a GET request to https://mee6.xyz/api/plugins/levels/leaderboard/guild_id_here. So using the following code I can get the user level like this

import requests

r = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{your_guild_id}')
players_json = r.json()['players']
player_level = [i for i in players_json if i['id'] == str(user_id)][0]['level']

Hope it helped!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 peared yt