Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
Better support for Guild, GuildChannel, Message
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeldeee committed Jul 30, 2023
1 parent 565b848 commit 6a49f0d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 13 deletions.
7 changes: 7 additions & 0 deletions pytecord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

if TYPE_CHECKING:
from pytecord.web import GatewayOutput
from pytecord.guild import Guild, GuildChannel

class Client:
def __init__(self, token: str) -> None:
Expand All @@ -20,6 +21,12 @@ async def func(data: 'GatewayOutput'):
self.webhook.add_event(name.upper(), func)

return decorator

def get_guild(self, id: int) -> 'Guild':
return self.webhook.get_guild(id)

def get_channel(self, id: int) -> 'GuildChannel':
return self.webhook.get_channel(id)

def run(self):
try:
Expand Down
2 changes: 2 additions & 0 deletions pytecord/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GATEWAY_VERSION = 10
API_VERSION = 10
144 changes: 135 additions & 9 deletions pytecord/guild.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from typing import Any

from pytecord.interfaces import Object
from pytecord.utils import rget

class Guild(Object):
def __init__(self, data: dict[str, Any]):
self.data = data

def __init__(self, data: dict[str, Any], token: str):
self.id = int(data.get('id'))
self.name = data.get('name')
self.icon = data.get('icon')
self.icon_hash = data.get('icon_hash')
self.splash = data.get('splash')
self.discovery_splash = data.get('discovery_splash')
self.owner = data.get('owner ')
self.owner = data.get('owner')
self.owner_id = data.get('owner_id')
self.permissions = data.get('permissions')
self.region = data.get('region ')
Expand Down Expand Up @@ -49,6 +48,9 @@ def __init__(self, data: dict[str, Any]):
self.stickers = data.get('stickers')
self.premium_progress_bar_enabled = data.get('premium_progress_bar_enabled')
self.safety_alerts_channel_id = int(data.get('safety_alerts_channel_id'))

self.__token = token
self.__data = data

def __int__(self) -> int:
"""
Expand All @@ -61,22 +63,32 @@ def __int__(self) -> int:
"""
return self.id

def __str__(self) -> str:
"""
Returns a guild name
```
>>> guild = Guild()
>>> str(guild)
```
"""
return self.name

def eval(self) -> dict[str, Any]:
"""
Returns a dict representation of object
Returns a dict representation of guild
```
>>> guild = Guild()
>>> guild.eval()
```
"""
return self.data
return self.__data

class GuildChannel(Object):
def __init__(self, data: dict[str, Any]):
def __init__(self, data: dict[str, Any], token: str):
self.id = int(data.get('id'))
self.type = data.get('type')
self.guild_id = int(data.get('guild_id'))
self.position = data.get('position')
self.permission_overwrites = data.get('permission_overwrites')
self.name = data.get('name')
Expand Down Expand Up @@ -110,5 +122,119 @@ def __init__(self, data: dict[str, Any]):
self.default_sort_order = data.get('default_sort_order')
self.default_forum_layout = data.get('default_forum_layout')

self.__guild_id = int(data.get('guild_id'))
self.__token = token
self.__data = data

@property
def guild(self) -> Guild:
data = rget('guild', self.__guild_id, self.__token).json()
return Guild(data, self.__token)

def __int__(self) -> int:
"""
Returns a channel id
```
>>> channel = GuildChannel()
>>> int(channel)
```
"""
return self.id

def __str__(self) -> str:
"""
Returns a channel name
```
>>> channel = GuildChannel()
>>> str(channel)
```
"""
return self.name

def eval(self) -> dict[str, Any]:
"""
Returns a dict representation of channel
```
>>> channel = GuildChannel()
>>> channel.eval()
```
"""
return self.__data

class Message:
...
def __init__(self, data: dict[str, Any], token: str) -> None:
self.id = int(data.get('id'))
self.author = data.get('author')
self.content = data.get('content')
self.timestamp = data.get('timestamp')
self.edited_timestamp = data.get('edited_timestamp')
self.tts = data.get('tts')
self.mention_everyone = data.get('mention_everyone')
self.mentions = data.get('mentions')
self.mention_roles = data.get('mention_roles')
self.mention_channels = data.get('mention_channels')
self.attachments = data.get('attachments')
self.embeds = data.get('embeds')
self.reactions = data.get('reactions')
self.nonce = data.get('nonce')
self.pinned = data.get('pinned')
self.webhook_id = int(data.get('webhook_id'))
self.type = data.get('type')
self.activity = data.get('activity')
self.application = data.get('application')
self.application_id = int(data.get('application_id'))
self.message_reference = data.get('message_reference')
self.flags = data.get('flags')
self.referenced_message = data.get('referenced_message')
self.interaction = data.get('interaction')
self.thread = data.get('thread')
self.components = data.get('components')
self.sticker_items = data.get('sticker_items')
self.stickers = data.get('stickers')
self.position = data.get('position')
self.role_subscription_data = data.get('role_subscription_data')

self.__channel_id = data.get('channel_id')
self.__token = token
self.__data = data

@property
def channel(self) -> GuildChannel:
data = rget('channel', self.__channel_id, self.__token).json()
return GuildChannel(data, self.__token)

def __int__(self) -> int:
"""
Returns a message id
```
>>> message = Message()
>>> int(message)
```
"""
return self.id

def __str__(self) -> str:
"""
Returns a message content
```
>>> message = Message()
>>> str(message)
```
"""
return self.content

def eval(self) -> dict[str, Any]:
"""
Returns a dict representation of channel
```
>>> message = Message()
>>> message.eval()
```
"""
return self.__data
13 changes: 13 additions & 0 deletions pytecord/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from requests import get, post

from typing import Literal, Any

from pytecord.config import API_VERSION

def get_headers(token: str):
return {'Authorization': f'Bot {token}'}}

def rget(what_to_get: Literal['channel', 'guild', 'user'], id: int, token: str = None, headers: dict[str, Any] = None):
if token:
headers = get_headers(token)
return get(f'https://discord.com/api/v{API_VERSION}/{what_to_get}s/{id}', headers=headers)
24 changes: 20 additions & 4 deletions pytecord/web.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from pytecord.interfaces import BaseDataStreamListener

from typing import Callable, Coroutine, Any
from typing import Callable, Coroutine, Any, TYPE_CHECKING
from aiohttp import ClientSession
from datetime import datetime
from time import mktime
from asyncio import gather, create_task
from asyncio import sleep as asleep
from pytecord.config import GATEWAY_VERSION
from pytecord.utils import rget

if TYPE_CHECKING:
from pytecord.guild import Guild, GuildChannel

class ApiRequest:
...
Expand Down Expand Up @@ -63,7 +68,6 @@ class DataStream:
def __init__(
self,
listener: BaseDataStreamListener,
gateway_version: int,
headers: dict,
token: str,
intents: int = 0,
Expand All @@ -73,7 +77,7 @@ def __init__(
) -> None:
self.listener = listener
self._ws = None
self.gateway_version = gateway_version
self.gateway_version = GATEWAY_VERSION
self.headers = headers

self.running = False
Expand Down Expand Up @@ -154,11 +158,23 @@ def __init__(self, token: str) -> None:
self.headers = {'Authorization': f'Bot {self.token}',}

self.listener = DataStreamListener()
self.stream = DataStream(self.listener, 10, self.headers, self.token)
self.stream = DataStream(self.listener, self.headers, self.token)
self.api_connector = ApiConnector(10)

def add_event(self, event_type:str, function: Callable):
self.listener.events[event_type] = function

async def run(self):
await self.stream.run()

def get_guild(self, id: int) -> 'Guild':
from pytecord.guild import Guild

data = rget('guild', id, self.token).json()
return Guild(data, self.token)

def get_channel(self, id: int) -> 'GuildChannel':
from pytecord.guild import GuildChannel

data = rget('channel', id, self.token).json()
return GuildChannel(data, self.token)

0 comments on commit 6a49f0d

Please sign in to comment.