diff options
-rw-r--r-- | discord/channels.py | 167 | ||||
-rw-r--r-- | discord/client.py | 10 | ||||
-rw-r--r-- | discord/guild.py | 23 |
3 files changed, 192 insertions, 8 deletions
diff --git a/discord/channels.py b/discord/channels.py new file mode 100644 index 0000000..3d3f3fd --- /dev/null +++ b/discord/channels.py @@ -0,0 +1,167 @@ +from __future__ import annotations +from enum import IntEnum +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from discord.guild import Guild + +class ChannelType(IntEnum): + GUILD_TEXT = 0 + DM = 1 + GUILD_VOICE = 2 + GROUP_DM = 3 + GUILD_CATEGORY = 4 + GUILD_NEWS = 5 + GUILD_NEWS_THREAD = 10 + GUILD_PUBLIC_THREAD = 11 + GUILD_PRIVATE_THREAD = 12 + GUILD_STAGE_VOICE = 13 + GUILD_DIRECTORY = 14 + +class GuildChannel: + _id: str + _guild: Guild + _name: str + _type: ChannelType + _position: int + _nsfw: bool + _permission_overwrites: list + _parent_id: str | None + _flags: int + + def __init__(self, data: dict, guild: Guild): + self._guild = guild + for key, value in data.items(): + setattr(self, f"_{key}", value) + + def __repr__(self) -> str: + return f"<GuildChannel id={self._id} name={self._name}>" + + @property + def id(self) -> str: + return self._id + + @property + def guild(self) -> Guild: + return self._guild + + @property + def name(self) -> str: + return self._name + + @property + def type(self) -> ChannelType: + return self._type.name + + @property + def position(self) -> int: + return self._position + + @property + def nsfw(self) -> bool: + return self._nsfw + + @property + def permission_overwrites(self) -> list: + return self._permission_overwrites + + @property + def parent_id(self) -> str: + return self._parent_id + + @property + def flags(self) -> int: + return self._flags + +class CategoryChannel(GuildChannel): + + def __init__(self, data: dict, guild: Guild): + super().__init__(data, guild) + + def __repr__(self) -> str: + return f"<CategoryChannel id={self._id} name={self._name}>" + + +class TextChannel(GuildChannel): + _rate_limit_per_user: int + _topic: str + _last_message_id: str + _default_auto_archive_duration: int + + @property + def rate_limit_per_user(self) -> int: + return self._rate_limit_per_user + + @property + def topic(self) -> str: + return self._topic + + @property + def last_message_id(self) -> str: + return self._last_message_id + + @property + def default_auto_archive_duration(self) -> int: + return self._default_auto_archive_duration + + def __init__(self, data: dict, guild: Guild): + super().__init__(data, guild) + for key, value in data.items(): + setattr(self, f"_{key}", value) + + def __repr__(self) -> str: + return f"<TextChannel id={self._id} name={self._name}>" + +class VoiceChannel(GuildChannel): + _bitrate: int + _user_limit: int + _rtc_region: str + + @property + def bitrate(self) -> int: + return self._bitrate + + @property + def user_limit(self) -> int: + return self._user_limit + + @property + def rtc_region(self) -> str: + return self._rtc_region + + def __init__(self, data: dict, guild: Guild): + super().__init__(data, guild) + for key, value in data.items(): + setattr(self, f"_{key}", value) + + def __repr__(self) -> str: + return f"<VoiceChannel id={self._id} name={self._name}>" + + +class StageChannel(GuildChannel): + pass + +class AnnouncementChannel(GuildChannel): + _topic: str + _last_message_id: str + _default_auto_archive_duration: int + + @property + def topic(self) -> str: + return self._topic + + @property + def last_message_id(self) -> str: + return self._last_message_id + + @property + def default_auto_archive_duration(self) -> int: + return self._default_auto_archive_duration + + def __init__(self, data: dict, guild: Guild): + super().__init__(data, guild) + for key, value in data.items(): + setattr(self, f"_{key}", value) + + def __repr__(self) -> str: + return f"<AnnouncementChannel id={self._id} name={self._name}>" \ No newline at end of file diff --git a/discord/client.py b/discord/client.py index 8901238..6da6aad 100644 --- a/discord/client.py +++ b/discord/client.py @@ -173,19 +173,19 @@ class Client: data = msg['d'] sequence = msg['s'] - if opcode != GatewayEvents.DISPATCH.value: - if opcode == GatewayEvents.RECONNECT.value: + if opcode != GatewayEvents.DISPATCH: + if opcode == GatewayEvents.RECONNECT: return await self.close() - if opcode == GatewayEvents.HELLO.value: + if opcode == GatewayEvents.HELLO: self.heartbeat_interval = data['heartbeat_interval'] asyncio.run_coroutine_threadsafe(self.heartbeat(self.heartbeat_interval), self.loop) return await self.identify() - if opcode == GatewayEvents.HEARTBEAT_ACK.value: + if opcode == GatewayEvents.HEARTBEAT_ACK: return await self.heartbeat(self.heartbeat_interval) - if opcode == GatewayEvents.HEARTBEAT.value: + if opcode == GatewayEvents.HEARTBEAT: return await self.heartbeat(self.heartbeat_interval) event = msg['t'] diff --git a/discord/guild.py b/discord/guild.py index bf6b826..1359c58 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -1,4 +1,5 @@ from discord.user import User +from .channels import AnnouncementChannel, CategoryChannel, ChannelType, GuildChannel, TextChannel, VoiceChannel class GuildMember: """ @@ -56,7 +57,6 @@ class GuildRole: def __repr__(self) -> str: return f"<GuildRole id={self._id} name={self._name}>" - class GuildEmoji: _id: str _name: str @@ -78,6 +78,9 @@ class GuildEmoji: def __repr__(self) -> str: return f"<GuildEmoji id={self._id} name={self._name}>" +class GuildSticker: + pass + class Guild: _id: str _name: str @@ -93,7 +96,7 @@ class Guild: _region: str _voice_states: list _members: list[GuildMember] - _channels: list + _channels: list[GuildChannel] _threads: list _afk_channel_id: str _afk_timeout: int @@ -147,7 +150,7 @@ class Guild: return self._roles @property - def channels(self) -> list: + def channels(self) -> list[GuildChannel]: return self._channels @property @@ -167,6 +170,20 @@ class Guild: self._emojis = [GuildEmoji(emoji) for emoji in value] case 'members': self._members = [GuildMember(member) for member in value] + case 'channels': + channels = [] + for channel in value: + if channel['type'] == ChannelType.GUILD_TEXT: + channels.append(TextChannel(channel, self)) + elif channel['type'] == ChannelType.GUILD_VOICE: + channels.append(VoiceChannel(channel, self)) + elif channel['type'] == ChannelType.GUILD_CATEGORY: + channels.append(CategoryChannel(channel, self)) + elif channel['type'] == ChannelType.GUILD_NEWS: + channels.append(AnnouncementChannel(channel, self)) + else: + channels.append(GuildChannel(channel, self)) + self._channels = channels case 'welcome_screen': self._welcome_screen = value case 'stickers': |