/discord-role-api

A discord bot that exposes an API to get users' roles.

Primary LanguagePython

Discord Role API

A discord bot that exposes an API to get users' roles for a specific server.

Setup

  1. Get a bot token for Discord (instructions here).
  2. Get the ID of the server you want to set the bot up for (instructions here).
  3. Create a file called config.json that looks like this:
    {
        "bot_token": "YOUR-VERY-LONG-BOT-TOKEN",
        "guild_id": 123456789012345
    }
  4. Make sure you have Python installed (must be version 3 and at least 3.9). You can download it here, and on Linux you may be able to install it with your package manager (some Linux distros come with older versions of Python, so if you're using a built in Python installation check the version number).
  5. Install the dependencies from PyPI. The command you run to do this depends on how you installed Python, but it will be something like python3 -m pip install -Ur requirements.txt. If that doesn't work, try replacing python3 with python, python3.9, py, py3 or py3.9.
  6. Run the server: python3 -m uvicorn app:server. You can specify the port/address to bind to with the --host and --port options, but beware that this service is only intended to run on localhost. You should make sure to include some authentication if you expose it to the web.

API

The API currently exposes only one endpoint:

GET /users/{user_id}/roles

{user_id}, of course, should be replaced with the ID of the user whose roles you wish to get. It will return a JSON object with one key, roles, which maps to a list of role objects. A role object has two keys, name and id, mapping to the name and ID of the role respectively.

Example response:

{
    "roles": [
        {
            "name": "Administrator",
            "id": 123456789012345
        },
        {
            "name": "Member",
            "id": 423491978162852
        }
    ]
}

If the user is not found, a 404 status code will be returned.