|
11 | 11 | import discord
|
12 | 12 | import httpx
|
13 | 13 |
|
14 |
| -from app.setup import bot |
| 14 | +from app.setup import bot, config |
15 | 15 | from app.utils.message_data import MessageData
|
16 | 16 |
|
17 | 17 | if TYPE_CHECKING:
|
@@ -504,6 +504,43 @@ def format_or_file(
|
504 | 504 | return full_message, None
|
505 | 505 |
|
506 | 506 |
|
| 507 | +async def get_moved_message(message: discord.Message) -> discord.WebhookMessage | None: |
| 508 | + """ |
| 509 | + Returns None if it could not be acquired, and discord.utils.MISSING if the |
| 510 | + provided message is not a moved message. |
| 511 | + """ |
| 512 | + if message.webhook_id is None or isinstance( |
| 513 | + message.channel, |
| 514 | + # These types can't even have a webhook. |
| 515 | + discord.DMChannel | discord.GroupChannel | discord.PartialMessageable, |
| 516 | + ): |
| 517 | + return discord.utils.MISSING |
| 518 | + |
| 519 | + if isinstance(message.channel, discord.Thread): |
| 520 | + thread = message.channel |
| 521 | + if (channel := message.channel.parent) is None: |
| 522 | + return None |
| 523 | + else: |
| 524 | + channel = message.channel |
| 525 | + thread = discord.utils.MISSING |
| 526 | + |
| 527 | + for webhook in await channel.webhooks(): |
| 528 | + if webhook.id == message.webhook_id: |
| 529 | + break |
| 530 | + else: |
| 531 | + return discord.utils.MISSING |
| 532 | + if webhook.name != config.BOT_WEBHOOK_NAME: |
| 533 | + # More heuristics to determine if a webhook message is a moved message. |
| 534 | + return discord.utils.MISSING |
| 535 | + |
| 536 | + try: |
| 537 | + return await webhook.fetch_message(message.id, thread=thread) |
| 538 | + except discord.Forbidden: |
| 539 | + return None |
| 540 | + except discord.NotFound: |
| 541 | + return discord.utils.MISSING |
| 542 | + |
| 543 | + |
507 | 544 | def _find_snowflake(
|
508 | 545 | content: str, type_: str, *, substring_start: int = 0
|
509 | 546 | ) -> tuple[int | None, int | None]:
|
|
0 commit comments