Skip to content

Follow Up Messages

Follow-up messages are a feature that allows for responses to sent after an initial response has been already been sent.

Info

This module offers methods both in async and sync.

Methods regarding editing and sending will require a DiscordResponse.

__init__(self, bot, interaction) special

A module to handle Follow Up Messages.

Parameters:

Name Type Description Default
bot Dispike

An already initalized dispike object.

required
interaction IncomingDiscordInteraction

An incoming Discord Interaction

required
Source code in dispike/followup/main.py
def __init__(
    self,
    bot: "Dispike",
    interaction: "IncomingDiscordInteraction",
):
    """A module to handle Follow Up Messages.

    Args:
        bot (Dispike): An already initalized dispike object.
        interaction (IncomingDiscordInteraction): An incoming Discord Interaction
    """
    self._application_id = bot._application_id
    self._interaction_token = interaction.token

    self.base_url = f"https://discord.com/api/v8/webhooks/{self._application_id}/{self._interaction_token}"
    self._async_client = httpx.AsyncClient(base_url=self.base_url)
    self._sync_client = httpx.Client(base_url=self.base_url)

    self._message_id = None

async_create_follow_up_message(self, message) async

Create an initial follow up message. (Async)

Parameters:

Name Type Description Default
message DiscordResponse

An already created discord response

required

Returns:

Type Description
True

If request is successfully made. An exception will rise otherwise

Exceptions:

Type Description
DiscordAPIError

Discord returning a non-OK status status_code

TypeError

Invalid type passed.

Source code in dispike/followup/main.py
@logger.catch(reraise=True)
async def async_create_follow_up_message(self, message: DiscordResponse):
    """Create an initial follow up message. (Async)

    Args:
        message (DiscordResponse): An already created discord response

    Returns:
        True: If request is successfully made. An exception will rise otherwise

    Raises:
        DiscordAPIError: Discord returning a non-OK status status_code
        TypeError: Invalid type passed.
    """
    if not isinstance(message, DiscordResponse):
        raise TypeError("Message must be a DiscordResponse")

    message._switch_to_followup_message()
    if self._message_id is not None:
        raise TypeError("Creating a followup message can only be done once.")
    try:
        _request = await self._async_client.post(
            url=self.base_url, json=message.response
        )
        logger.info("sent request for creation of follow up to discord..")
        if _request.status_code in [200, 201]:
            _parse_request = _request.json()
            self._message_id = _parse_request["id"]
            return True
        else:
            logger.error(
                f"discord returned a bad status code: {_request.status_code} -> {_request.text} url: {_request.url}"
            )
            raise DiscordAPIError(_request.status_code, _request.text)
    except DiscordAPIError:
        raise

async_delete_follow_up_message(self) async

Deletes an already sent initial follow-up message. (Async)

Returns:

Type Description
True

If request is successfully made. An exception will rise otherwise

Exceptions:

Type Description
DiscordAPIError

Discord returning a non-OK status status_code

TypeError

Invalid type passed.

Source code in dispike/followup/main.py
@logger.catch(reraise=True)
async def async_delete_follow_up_message(self):
    """Deletes an already sent initial follow-up message. (Async)

    Returns:
        True: If request is successfully made. An exception will rise otherwise

    Raises:
        DiscordAPIError: Discord returning a non-OK status status_code
        TypeError: Invalid type passed.
    """
    if self._message_id is None:
        raise TypeError("a followup message must be sent first.")
    try:
        _request = await self._async_client.delete(f"/messages/{self._message_id}")
        logger.info(
            f"sent request for deletion of follow up to discord [{self._message_id}].."
        )
        if _request.status_code in [200, 201, 204]:
            self._message_id = None
            return True
        else:
            raise DiscordAPIError(_request.status_code, _request.text)
    except DiscordAPIError:
        raise

async_edit_follow_up_message(self, updated_message) async

Edit an already sent initial follow-up message. (Async)

Parameters:

Name Type Description Default
updated_message DiscordResponse

An already created discord response

required

Returns:

Type Description
True

If request is successfully made. An exception will rise otherwise

Exceptions:

Type Description
DiscordAPIError

Discord returning a non-OK status status_code

TypeError

Invalid type passed.

Source code in dispike/followup/main.py
@logger.catch(reraise=True)
async def async_edit_follow_up_message(self, updated_message: DiscordResponse):
    """Edit an already sent initial follow-up message. (Async)

    Args:
        updated_message (DiscordResponse): An already created discord response

    Returns:
        True: If request is successfully made. An exception will rise otherwise

    Raises:
        DiscordAPIError: Discord returning a non-OK status status_code
        TypeError: Invalid type passed.
    """
    if self._message_id is None:
        raise TypeError("a followup message must be sent first!")
    try:
        _request = await self._async_client.patch(
            f"/messages/{self._message_id}", json=updated_message.response
        )
        logger.info(
            f"sent request for edit of follow up to discord [{self._message_id}].."
        )
        if _request.status_code in [200, 201]:
            _parse_request = _request.json()
            self._message_id = _parse_request["id"]
            return True
        else:
            raise DiscordAPIError(_request.status_code, _request.text)
    except DiscordAPIError:
        raise

create_follow_up_message(self, message)

Create an initial follow up message. (Sync)

Parameters:

Name Type Description Default
message DiscordResponse

An already created discord response

required

Returns:

Type Description
True

If request is successfully made. An exception will rise otherwise

Exceptions:

Type Description
DiscordAPIError

Discord returning a non-OK status status_code

TypeError

Invalid type passed.

Source code in dispike/followup/main.py
@logger.catch(reraise=True)
def create_follow_up_message(self, message: DiscordResponse):
    """Create an initial follow up message. (Sync)

    Args:
        message (DiscordResponse): An already created discord response

    Returns:
        True: If request is successfully made. An exception will rise otherwise

    Raises:
        DiscordAPIError: Discord returning a non-OK status status_code
        TypeError: Invalid type passed.
    """
    if not isinstance(message, DiscordResponse):
        raise TypeError("Message must be a DiscordResponse")

    message._switch_to_followup_message()
    if self._message_id is not None:
        raise TypeError("Creating a followup message can only be done once.")
    try:
        _request = self._sync_client.post(url=self.base_url, json=message.response)
        logger.info("sent request for creation of follow up to discord..")
        if _request.status_code in [200, 201]:
            _parse_request = _request.json()
            self._message_id = _parse_request["id"]
            return True
        else:
            logger.error(
                f"discord returned a bad status code: {_request.status_code} -> {_request.text} url: {_request.url}"
            )
            raise DiscordAPIError(_request.status_code, _request.text)
    except DiscordAPIError:
        raise

delete_follow_up_message(self)

Deletes an already sent initial follow-up message. (sync)

Returns:

Type Description
True

If request is successfully made. An exception will rise otherwise

Exceptions:

Type Description
DiscordAPIError

Discord returning a non-OK status status_code

TypeError

Invalid type passed.

Source code in dispike/followup/main.py
@logger.catch(reraise=True)
def delete_follow_up_message(self):
    """Deletes an already sent initial follow-up message. (sync)

    Returns:
        True: If request is successfully made. An exception will rise otherwise

    Raises:
        DiscordAPIError: Discord returning a non-OK status status_code
        TypeError: Invalid type passed.
    """
    if self._message_id is None:
        raise TypeError("a followup message must be sent first.")
    try:
        _request = self._sync_client.delete(f"/messages/{self._message_id}")
        logger.info(
            f"sent request for deletion of follow up to discord [{self._message_id}].."
        )
        if _request.status_code in [200, 201, 204]:
            self._message_id = None
            return True
        else:
            raise DiscordAPIError(_request.status_code, _request.text)
    except DiscordAPIError:
        raise

edit_follow_up_message(self, updated_message)

Edit an already sent initial follow-up message. (Sync)

Parameters:

Name Type Description Default
updated_message DiscordResponse

An already created discord response

required

Returns:

Type Description
True

If request is successfully made. An exception will rise otherwise

Exceptions:

Type Description
DiscordAPIError

Discord returning a non-OK status status_code

TypeError

Invalid type passed.

Source code in dispike/followup/main.py
@logger.catch(reraise=True)
def edit_follow_up_message(self, updated_message: DiscordResponse):
    """Edit an already sent initial follow-up message. (Sync)

    Args:
        updated_message (DiscordResponse): An already created discord response

    Returns:
        True: If request is successfully made. An exception will rise otherwise

    Raises:
        DiscordAPIError: Discord returning a non-OK status status_code
        TypeError: Invalid type passed.
    """
    if self._message_id is None:
        raise TypeError("a followup message must be sent first!")
    try:
        _request = self._sync_client.patch(
            f"/messages/{self._message_id}", json=updated_message.response
        )
        logger.info(
            f"sent request for edit of follow up to discord [{self._message_id}].."
        )
        if _request.status_code in [200, 201]:
            _parse_request = _request.json()
            self._message_id = _parse_request["id"]
            return True
        else:
            raise DiscordAPIError(_request.status_code, _request.text)
    except DiscordAPIError:
        raise