yuntongzhang / test

MIT License
0 stars 2 forks source link

Add a method for fetching a preview of a discoverable guild #4

Open yuntongzhang opened 1 month ago

yuntongzhang commented 1 month ago

Summary

Discord has an API for fetching a preview of a discoverable guild, which is currently not used by this library.

What is the feature request for?

The core library

The Problem

Discord has an API endpoint for getting a preview of a discoverable guild. There is a type for the response in the library, but no way to actually perform the request at present. This means that bots can't see preview information of discoverable guilds.

The Ideal Solution

I would like a new method on Client called fetch_guild_preview(id: int) which returns a GuildPreview that exposes all the fields of the response.

The Current Solution

The request must be made manually and the response parsed by hand.

Additional Context

No response

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I'm invoked! Going to create a solution for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I'm invoked! Going to create a solution for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I'm invoked! Going to create a solution for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

yuntongzhang commented 1 month ago

@code-rover-bot

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I could not generate a patch for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

yuntongzhang commented 1 month ago

@code-rover-bot

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I could not generate a patch for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I could not generate a patch for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I could not generate a patch for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I could not generate a patch for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I could not generate a patch for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago

I could not generate a patch for this issue.

yuntongzhang commented 1 month ago

@code-rover-bot

code-rover-bot[bot] commented 1 month ago
diff --git a/discord/client.py b/discord/client.py
index a91be716..f8caf7bd 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -2086,6 +2086,31 @@ class Client:

     # Guild stuff

+    async def fetch_guild_preview(self, guild_id: int) -> GuildPreview:
+        """|coro|
+
+        Fetches a preview of a discoverable guild.
+
+        Parameters
+        -----------
+        guild_id: :class:`int`
+            The ID of the guild to fetch the preview for.
+
+        Raises
+        ------
+        NotFound
+            The guild is not found or is not discoverable.
+        HTTPException
+            Fetching the guild preview failed.
+
+        Returns
+        --------
+        :class:`.GuildPreview`
+            The preview of the guild.
+        """
+        data = await self.http.get_guild_preview(guild_id)
+        return GuildPreview(data=data)
+
     async def fetch_guilds(
         self,
         *,
diff --git a/discord/http.py b/discord/http.py
index 608595fe..bd19de88 100644
--- a/discord/http.py
+++ b/discord/http.py
@@ -1987,7 +1987,19 @@ class HTTPClient:
     def get_scheduled_events(
         self, guild_id: Snowflake, with_user_count: Literal[True]
     ) -> Response[List[scheduled_event.GuildScheduledEventWithUserCount]]:
-        ...
+        import aiohttp
+        import asyncio
+        import logging
+        import json
+        from typing import Any, Dict, List, Optional, Union
+    
+        class HTTPClient:
+            ...
+            
+            async def get_guild_preview(self, guild_id: int) -> Any:
+                """Fetches the preview of a discoverable guild."""
+                route = Route('GET', '/guilds/{guild_id}/preview', guild_id=guild_id)
+                return await self.request(route)

     @overload
     def get_scheduled_events(
yuntongzhang commented 1 month ago

@code-rover-bot

github-actions[bot] commented 1 month ago
diff --git a/discord/types/guild.py b/discord/types/guild.py
index ba43fbf..896b956 100644
--- a/discord/types/guild.py
+++ b/discord/types/guild.py
@@ -111,7 +111,16 @@ class _GuildPreviewUnique(TypedDict):

 class GuildPreview(_BaseGuildPreview, _GuildPreviewUnique):
-    ...
+    def __init__(self, data):
+        self.id = int(data['id'])
+        self.name = data['name']
+        self.icon = data['icon']
+        self.splash = data['splash']
+        self.discovery_splash = data['discovery_splash']
+        self.emojis = [Emoji(emoji) for emoji in data['emojis']]
+        self.features = data['features']
+        self.approximate_member_count = data['approximate_member_count']
+        self.approximate_presence_count = data['approximate_presence_count']

 class Guild(_BaseGuildPreview):