Skip to content

Support for linking plain messages. #3012

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

Closed
wants to merge 5 commits into from

Conversation

Jerrie-Aries
Copy link
Contributor

@Jerrie-Aries Jerrie-Aries commented May 1, 2021

Regarding issue #2876

About this PR:

  • Support for linking plain messages.
  • Deleting and editing plain messages both work.
  • Now uses database to store and fetch the linked IDs.
  • Add parameter linked_ids (type: list) for method append_log in clients.py. The linked IDs will be stored when the thread message is sent whether from thread or DM.
  • No longer find linked messages matching from embed author url.
  • Add another return value (type: str) for method find_linked_messages in thread.py, which would be the type of the thread message. Expected "thread_message", "anonymous", or "system".
  • Add parameter author (type: discord.Member) for method edit_message in thread.py.

Note:

  • Linking multiple images to a single message is still not supported.

@Jerrie-Aries
Copy link
Contributor Author

Jerrie-Aries commented May 2, 2021

By the way, this still can be improved.
For example remove the mod/recipient embed color and the message type (Note, Persistent Note) checks, and do those checks using the data we get from the database instead.

So the example of code for find_linked_messages in thread.py would be:

    async def find_linked_messages(
        self,
        message_id: Optional[int] = None,
        either_direction: bool = False,
        message1: discord.Message = None,
        note: bool = True,
    ) -> Tuple[discord.Message, Optional[discord.Message], str]:
        if message1 is not None:
            if not (message1.author == self.bot.user and message1.embeds):
                raise ValueError("Malformed thread message.")

        elif message_id is not None:
            try:
                message1 = await self.channel.fetch_message(message_id)
            except discord.NotFound:
                raise ValueError("Thread message not found.")

            if not (message1.author == self.bot.user and message1.embeds):
                raise ValueError("Malformed thread message.")

        thread_logs = await self.bot.api.logs.find_one({"channel_id": str(self.channel.id)})
        if thread_logs is None:
            raise ValueError("Thread logs not found.")

        messages_logs = thread_logs["messages"]

        def unpack_data(msg_id):
            # internal function since this will be used twice
            linked_ids, type_, from_mod = next(
                (
                    (
                        data.get("linked_ids"),
                        data.get("type"),
                        data.get("author", {}).get("mod", False),
                    )
                    for data in messages_logs
                    if str(msg_id) in data.get("linked_ids", [])
                    or str(msg_id) == data.get("message_id", str())
                ),
                (None, str(), False),
            )
            return linked_ids, type_, from_mod

        if message1 is None:
            # still None, this is usually when using `delete` or `edit` command without providing ID
            # so in this case, we only search for thread message
            # Note or persistent note will not be returned if ID is not provided
            async for message1 in self.channel.history():
                linked_ids, type_, from_mod = unpack_data(message1.id)
                if from_mod and type_ not in ("system", "internal"):
                    break
            else:
                raise ValueError("Thread message not found.")
        else:
            linked_ids, type_, from_mod = unpack_data(message1.id)

        if linked_ids:
            if not from_mod and not either_direction:
                raise ValueError("Thread message not found.")
            async for msg in self.recipient.history():
                if str(msg.id) in linked_ids:
                    return message1, msg, type_
            raise ValueError("DM message not found.")

        if type_ == "system" and note:
            return message1, None, type_

        raise ValueError("Thread message not found.")

…read.

Also remove error response saying 'Plain messages are not supported' in `?delete` command.
@Jerrie-Aries
Copy link
Contributor Author

Closing this since this is now outdated and there would be merge conflicts.

@Jerrie-Aries Jerrie-Aries deleted the dev-01 branch June 28, 2021 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant