From e5554ce086efa686ddb8ca39d812a184c29e1887 Mon Sep 17 00:00:00 2001 From: mjk134 <57556877+mjk134@users.noreply.github.com> Date: Thu, 14 Jul 2022 10:20:42 +0000 Subject: feat: Add channels for guilds --- discord/channels.py | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ discord/client.py | 10 ++-- discord/guild.py | 23 +++++++- 3 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 discord/channels.py (limited to 'discord') 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"" + + @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"" + + +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"" + +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"" + + +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"" \ 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"" - class GuildEmoji: _id: str _name: str @@ -78,6 +78,9 @@ class GuildEmoji: def __repr__(self) -> str: return f"" +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': -- cgit 1.4.1-2-gfad0