Skip to content

JSON

Bases: Deserializer[K_contra]

deserialize(source, key, callables, events)

Method to deserialize a source into a Display object

Parameters:

  • source (str) –

    The source text to deserialize

  • key (K) –

    The key of the element to deserialize

  • callables (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (EventCallbacks) –

    A dictionary containing the event callbacks.

Returns (ReturnType): All possible deserialized objects.

deserialize_element(document, element, callables, events)

Method to deserialize an element into a Display object

Parameters:

  • document (Document) –

    The document to deserialize

  • element (Elements) –

    The element to deserialize

  • callables (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (EventCallbacks) –

    A dictionary containing the event callbacks.

Returns (ReturnType): All possible deserialized objects.

deserialize_expansive(message_tree, callbacks, events)

Method to deserialize a source into a list of Display objects

Parameters:

  • message_tree (ExpansiveMessage) –

    The ExpansiveMessage of the message_tree

  • callbacks (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (EventCallbacks) –

    A dictionary containing the event callbacks.

Returns (List[Display]): A list of Display objects

deserialize_expansive_into_menu(message_tree, callbacks, events)

Method to deserialize an expansive message into a Menu

Parameters:

  • message_tree (ExpansiveMessage) –

    The ExpansiveMessage of the message_tree

  • callbacks (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (EventCallbacks) –

    A dictionary containing the event callbacks.

Returns (Menu): A Menu object

deserialize_menu(menu, callables, events, *, document)

Method to deserialize a menu into a list of Display objects

Parameters:

  • menu (MenuMessage) –

    The Menu Dictionary to deserialize into a List of Messages

  • callables (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (EventCallbacks) –

    A dictionary containing the event callbacks.

  • document (Document) –

    the original document containing all the keys.

Returns (List[Message]): A list of Display objects

deserialize_message(message_tree, callables, events, **overrides)

Method to deserialize an embed into a Display NamedTuple containing the embed and the view

Parameters:

  • message_tree (Dict[str, Any]) –

    The embed to deserialize

  • callables (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (EventCallbacks) –

    A dictionary containing the event callbacks.

  • **overrides (Any, default: {} ) –

    keyword arguments that override the messages

Returns (Display): A Display NamedTuple containing the embed and the view

deserialize_modal(tree, callables, events)

Method to deserialize a modal into a discord.ui.Modal object

Parameters:

  • tree (Modal) –

    The Modal Dictionary to deserialize into a discord.ui.Modal object

  • callables (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (Dict[ModalEvents, ModalEventsCallbacks]) –

    A dictionary containing the event callbacks.

Returns (discord.ui.Modal): A discord.ui.Modal object

deserialize_page(document, raw_page, callables, events)

Method to deserialize a page into a Display object

Parameters:

  • document (Document) –

    the original document containing all the keys.

  • raw_page (Page) –

    The page to deserialize

  • callables (Dict[str, Callback]) –

    A dictionary containing the callables to use for the buttons

  • events (EventCallbacks) –

    A dictionary containing the event callbacks.

Returns (List[Message]): List of pages

render_component(component, callback)

Renders a component from the given component's template

Parameters:

  • component (Components) –

    the component's template

  • callback (Optional[Callback]) –

    the callback to be called when the user interacts with the component

Returns (ui.Item): the rendered component

render_components(raw_components, callables)

Renders the components specified by the identifier

Parameters:

  • raw_components (Components) –

    the dictionary containing the view component.

  • callables (Dict[str, Callback]) –

    the callbacks to be called when the user interacts with the components

Returns (List[ui.Item]): the rendered components


🧰 Working with JSON

This is the alternative way to use 🃏 Qalib.You use the keys to the embed as the

You can then have multiple embeds which are each contained in a JSON object, and it's key needs to uniquely identifies them among the other embed keys, and is written as {"test_key": { ... }, "test_key2": { ... }}

To render values dynamically, simply put them in between braces, and the renderer will format it when you use the context's rendered_send() method, as seen in the next section.

It is safe to skip any non-mandatory fields that an embed would not require, they will simply use their default values.

🧩 Sample

{
  "test_key": {
    "type": "message",
    "embed": {
      "title": "Test",
      "description": "Test Description",
      "type": "rich",
      "colour": "55,55,55",
      "timestamp": {
        "date": "{todays_date}"
      },
      "url": "https://www.discord.com",
      "fields": [
        {
          "name": "Test Field",
          "text": "Test Text"
        }
      ],
      "footer": {
        "text": "Test Footer",
        "icon": "https://cdn.discordapp.com/embed/avatars/0.png"
      },
      "thumbnail": "https://cdn.discordapp.com/embed/avatars/0.png",
      "image": "https://cdn.discordapp.com/embed/avatars/0.png",
      "author": {
        "name": "{author_name}",
        "icon": "https://cdn.discordapp.com/embed/avatars/0.png",
        "url": "https://discordapp.com"
      }
    }
  },
  "test_key2": {
    "type": "message",
    "embed": {
      "title": "Test",
      "colour": "magenta",
      "fields": [
        {
          "name": "Test Field",
          "text": "Test Text"
        }
      ]
    }
  }
}

🖌️ Views

The main components are rendered and instantiate the mapped component/item in discord.py. The limit to the number of components/items that you can use in one embed is capped at 25.

For each example we will write how the component should look like. Components/Items should be written in the view section, where the comment is.

{
  "test_key2": {
    "type": "message",
    "embed": {
      "title": "Test2",
      "description": "Test Description",
      "colour": "magenta",
      "fields": [
        {
          "name": "Test Field",
          "text": "Test Text"
        }
      ],
      "view": {
        "components": []
      }
    }
  }
}

🆗 Button

Rendering a Button in .json.

"button_key": {
  "type": "button",
  "label": "Click Me!",
  "style": "success",
  "custom_id": "{custom_id}",
  "disabled": true,
  "url": "https://github.com/YousefEZ/discord-qalib",
  "emoji": {
    "name": "joy"
  }
}

🏴 Select

Rendering a Select in .json

"select_key": {
  "placeholder": "Select An Option",
  "custom_id": "{custom_id}",
  "min_values": 1,
  "max_values": 3,
  "disabled": false
  "options": [
    {
      "label": "Amman"
      "value": 0,
      "description": "The capital city of Jordan",
      "emoji": {
        "name": "Petra",
        "id": 217348923789,
        "animated": false
      }
    },
    {
      "label": "Baghdad"
    },
    {
      "label": "Cairo"
    },
    {
      "label": "Damascus"
    }
  ]
}

📣 Channel Select

Rendering a Channel Select in .json

"channel_key": {
  "type": "channel_select",
  "placeholder": "Select A Channel",
  "channel_types": ["text", "private"],
  "min_values": 1,
  "max_values": 2,
  "disabled": false,
}

🏷️ Mentionable Select

Rendering a Mentionable Select in .json

"mentionable_key": {
  "type": "mentionable_select",
  "placeholder": "Select Something to Mention",
  "min_values": 1,
  "max_values": 2,
  "disabled": false,
}

🥷 User Select

Rendering a User Select in .json

"user_key": {
  "type": "user_select",
  "placeholder": "Select A User",
  "min_values": 1,
  "max_values": 2,
  "disabled": false,
}

🎭 Role Select

Rendering a Role Select in .json

"role_key": {
  "type": "role_select",
  "placeholder": "Select A Role",
  "min_values": 1,
  "max_values": 2,
  "disabled": false,
}

💬 Text Input

Rendering a Text Input in .json

"text_select": {
  "label": "What do you think?",
  "style": "short",
  "placeholder": "Write your response...",
  "default": "N/A",
  "min_length": 0,
  "max_length": 150
}

📝 Modal

Modals can be rendered, simply by using the key to Modal as the key to JSON object. They also need to be passed the methods using their method names as their keys. A Sample document containing Modals can be seen here

{
  "modal1": {
    "type": "modal",
    "title": "Modal 1",
    "components": {
      "name": {
        "type": "text_input",
        "label": "What is your name?",
        "placeholder": "Enter your name"
      },
      "age": {
        "type": "text_input",
        "label": "What is your age?",
        "placeholder": "Enter your age"
      }
    }
  }
}