Add guild widget support (#59)
This commit is contained in:
parent
39162b6543
commit
afeb86a395
@ -1430,6 +1430,24 @@ class Client:
|
||||
|
||||
await self._http.delete_guild_template(guild_id, template_code)
|
||||
|
||||
async def fetch_widget(self, guild_id: Snowflake) -> Dict[str, Any]:
|
||||
"""|coro| Fetch a guild's widget settings."""
|
||||
|
||||
if self._closed:
|
||||
raise DisagreementException("Client is closed.")
|
||||
|
||||
return await self._http.get_guild_widget(guild_id)
|
||||
|
||||
async def edit_widget(
|
||||
self, guild_id: Snowflake, payload: Dict[str, Any]
|
||||
) -> Dict[str, Any]:
|
||||
"""|coro| Edit a guild's widget settings."""
|
||||
|
||||
if self._closed:
|
||||
raise DisagreementException("Client is closed.")
|
||||
|
||||
return await self._http.edit_guild_widget(guild_id, payload)
|
||||
|
||||
async def fetch_scheduled_events(
|
||||
self, guild_id: Snowflake
|
||||
) -> List["ScheduledEvent"]:
|
||||
|
@ -678,6 +678,20 @@ class HTTPClient:
|
||||
"""Fetches a guild object for a given guild ID."""
|
||||
return await self.request("GET", f"/guilds/{guild_id}")
|
||||
|
||||
async def get_guild_widget(self, guild_id: "Snowflake") -> Dict[str, Any]:
|
||||
"""Fetches the guild widget settings."""
|
||||
|
||||
return await self.request("GET", f"/guilds/{guild_id}/widget")
|
||||
|
||||
async def edit_guild_widget(
|
||||
self, guild_id: "Snowflake", payload: Dict[str, Any]
|
||||
) -> Dict[str, Any]:
|
||||
"""Edits the guild widget settings."""
|
||||
|
||||
return await self.request(
|
||||
"PATCH", f"/guilds/{guild_id}/widget", payload=payload
|
||||
)
|
||||
|
||||
async def get_guild_templates(self, guild_id: "Snowflake") -> List[Dict[str, Any]]:
|
||||
"""Fetches all templates for the given guild."""
|
||||
return await self.request("GET", f"/guilds/{guild_id}/templates")
|
||||
|
@ -1131,6 +1131,16 @@ class Guild:
|
||||
def __repr__(self) -> str:
|
||||
return f"<Guild id='{self.id}' name='{self.name}'>"
|
||||
|
||||
async def fetch_widget(self) -> Dict[str, Any]:
|
||||
"""|coro| Fetch this guild's widget settings."""
|
||||
|
||||
return await self._client.fetch_widget(self.id)
|
||||
|
||||
async def edit_widget(self, payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""|coro| Edit this guild's widget settings."""
|
||||
|
||||
return await self._client.edit_widget(self.id, payload)
|
||||
|
||||
async def fetch_members(self, *, limit: Optional[int] = None) -> List["Member"]:
|
||||
"""|coro|
|
||||
|
||||
|
50
tests/test_widget.py
Normal file
50
tests/test_widget.py
Normal file
@ -0,0 +1,50 @@
|
||||
import pytest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from disagreement.http import HTTPClient
|
||||
from disagreement.client import Client
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_guild_widget_calls_request():
|
||||
http = HTTPClient(token="t")
|
||||
http.request = AsyncMock(return_value={})
|
||||
await http.get_guild_widget("1")
|
||||
http.request.assert_called_once_with("GET", "/guilds/1/widget")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_edit_guild_widget_calls_request():
|
||||
http = HTTPClient(token="t")
|
||||
http.request = AsyncMock(return_value={})
|
||||
payload = {"enabled": True}
|
||||
await http.edit_guild_widget("1", payload)
|
||||
http.request.assert_called_once_with("PATCH", "/guilds/1/widget", payload=payload)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_client_fetch_widget_returns_data():
|
||||
http = SimpleNamespace(get_guild_widget=AsyncMock(return_value={"enabled": True}))
|
||||
client = Client.__new__(Client)
|
||||
client._http = http
|
||||
client._closed = False
|
||||
|
||||
data = await client.fetch_widget("1")
|
||||
|
||||
http.get_guild_widget.assert_awaited_once_with("1")
|
||||
assert data == {"enabled": True}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_client_edit_widget_returns_data():
|
||||
http = SimpleNamespace(edit_guild_widget=AsyncMock(return_value={"enabled": False}))
|
||||
client = Client.__new__(Client)
|
||||
client._http = http
|
||||
client._closed = False
|
||||
|
||||
payload = {"enabled": False}
|
||||
data = await client.edit_widget("1", payload)
|
||||
|
||||
http.edit_guild_widget.assert_awaited_once_with("1", payload)
|
||||
assert data == {"enabled": False}
|
Loading…
x
Reference in New Issue
Block a user