Add message crosspost support (#104)

This commit is contained in:
Slipstream 2025-06-15 18:49:48 -06:00 committed by GitHub
parent 7f9647a442
commit 2586d3cd0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 8 deletions

View File

@ -656,12 +656,21 @@ class HTTPClient:
await self.request("PUT", f"/channels/{channel_id}/pins/{message_id}")
async def unpin_message(
self, channel_id: "Snowflake", message_id: "Snowflake"
) -> None:
"""Unpins a message from a channel."""
await self.request("DELETE", f"/channels/{channel_id}/pins/{message_id}")
async def unpin_message(
self, channel_id: "Snowflake", message_id: "Snowflake"
) -> None:
"""Unpins a message from a channel."""
await self.request("DELETE", f"/channels/{channel_id}/pins/{message_id}")
async def crosspost_message(
self, channel_id: "Snowflake", message_id: "Snowflake"
) -> Dict[str, Any]:
"""Crossposts a message to any following channels."""
return await self.request(
"POST", f"/channels/{channel_id}/messages/{message_id}/crosspost"
)
async def delete_channel(
self, channel_id: str, reason: Optional[str] = None

View File

@ -177,8 +177,17 @@ class Message:
HTTPException
Unpinning the message failed.
"""
await self._client._http.unpin_message(self.channel_id, self.id)
self.pinned = False
await self._client._http.unpin_message(self.channel_id, self.id)
self.pinned = False
async def crosspost(self) -> "Message":
"""|coro|
Crossposts this message to all follower channels and return the resulting message.
"""
data = await self._client._http.crosspost_message(self.channel_id, self.id)
return self._client.parse_message(data)
async def reply(
self,

View File

@ -0,0 +1,41 @@
import pytest
from types import SimpleNamespace
from unittest.mock import AsyncMock
from disagreement.http import HTTPClient
from disagreement.client import Client
from disagreement.models import Message
@pytest.mark.asyncio
async def test_http_crosspost_message_calls_request():
http = HTTPClient(token="t")
http.request = AsyncMock(return_value={"id": "m"})
data = await http.crosspost_message("c", "m")
http.request.assert_called_once_with(
"POST",
"/channels/c/messages/m/crosspost",
)
assert data == {"id": "m"}
@pytest.mark.asyncio
async def test_message_crosspost_returns_message():
payload = {
"id": "2",
"channel_id": "1",
"author": {"id": "3", "username": "u", "discriminator": "0001"},
"content": "hi",
"timestamp": "t",
}
http = SimpleNamespace(crosspost_message=AsyncMock(return_value=payload))
client = Client.__new__(Client)
client._http = http
client.parse_message = lambda d: Message(d, client_instance=client)
message = Message(payload, client_instance=client)
new_msg = await message.crosspost()
http.crosspost_message.assert_awaited_once_with("1", "2")
assert isinstance(new_msg, Message)
assert new_msg._client is client