1 Commits

Author SHA1 Message Date
Zomatree b61bb49949 inital voice support 2021-12-31 04:52:56 +00:00
47 changed files with 685 additions and 1404 deletions
-1
View File
@@ -7,4 +7,3 @@ docs/_build
.vscode
.env
.mypy_cache
build
+2 -2
View File
@@ -12,10 +12,10 @@ venv:
test:
python test.py
build:
build: venv
python -m build
upload:
upload: venv
python -m twine upload dist/* -u $PYPI_USERNAME -p $PYPI_PASSWORD
lint:
-3
View File
@@ -1,3 +0,0 @@
include requirements.txt
include LICENSE
include README.md
+5 -15
View File
@@ -1,26 +1,16 @@
# Revolt.py
An async library to interact with the https://revolt.chat API. This library will **only support bots** and I will **not** implement anything exclusively for user accounts.
An async library to interact with the https://revolt.chat api.
You can join the support server [here](https://rvlt.gg/FDXER6hr) and find the library's documentation [here](https://revoltpy.readthedocs.io/en/latest/).
This library will be focused on making bots and i will not implement anything only for user accounts.
## Installing
Support server: https://app.revolt.chat/invite/FDXER6hr
You can use `pip` to install revolt.py. It differs slightly depending on what OS/Distro you use.
On Windows
```
py -m pip install -U revolt.py # -U to update
```
On macOS and Linux
```
python3 -m pip install -U revolt.py
```
Documentation is [here](https://revoltpy.readthedocs.io/en/latest/)
## Example
More examples can be found in the [examples folder](https://github.com/revoltchat/revolt.py/blob/master/examples).
More examples in the [examples folder](https://github.com/zomatree/revolt.py/blob/master/examples)
```py
import revolt
-9
View File
@@ -220,12 +220,3 @@ All enums subclass `aenum.Enum`.
.. attribute:: user
That user is yourself
Utils
======
.. currentmodule:: revolt.utils
A collection a utility functions and classes to aid in making your bot
.. autofunction:: get
-15
View File
@@ -88,18 +88,3 @@ BadBoolArgument
~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.BadBoolArgument
:members:
CategoryConverterError
~~~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.CategoryConverterError
:members:
UserConverterError
~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.UserConverterError
:members:
MemberConverterError
~~~~~~~~~~~~~~~~~~~~~
.. autoexception:: revolt.ext.commands.MemberConverterError
:members:
+2 -2
View File
@@ -1,6 +1,6 @@
Sphinx==4.3.2
Sphinx==3.5.4
sphinx-nameko-theme==0.0.3
aiohttp==3.7.4.post0
ulid-py==1.1.0
sphinx-toolbox==2.15.2
sphinx-toolbox==2.13.0
aenum==3.1.0
+2 -1
View File
@@ -1,4 +1,5 @@
aiohttp==3.7.4.post0
ulid-py==1.1.0
aenum==3.1.0
typing_extensions==4.1.1
typing_extensions
pynacl==1.4.0
+19 -18
View File
@@ -1,19 +1,20 @@
from . import utils
from .asset import *
from .category import *
from .channel import *
from .client import *
from .embed import *
from .enums import *
from .errors import *
from .file import *
from .flags import *
from .member import *
from .message import *
from .messageable import *
from .permissions import *
from .role import *
from .server import *
from .user import *
from .asset import Asset
from .category import Category
from .channel import (Channel, DMChannel, GroupDMChannel, SavedMessageChannel,
TextChannel, VoiceChannel)
from .client import Client
from .embed import Embed
from .enums import (AssetType, ChannelType, PresenceType, RelationshipType,
SortType)
from .errors import HTTPError, RevoltError, ServerError
from .file import File
from .flags import UserBadges
from .member import Member
from .message import Masquerade, Message, MessageReply
from .messageable import Messageable
from .permissions import ChannelPermissions, ServerPermissions
from .role import Role
from .server import Server, SystemMessages
from .user import Relation, Status, User
__version__ = (0, 1, 6)
__version__ = (0, 1, 1)
+44 -133
View File
@@ -1,14 +1,13 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Literal, Optional, Union
from typing import TYPE_CHECKING, Optional, cast
import asyncio
from revolt.utils import Missing
from .asset import Asset
from .enums import ChannelType
from .messageable import Messageable
from .permissions import ChannelPermissions
from .utils import Missing
from .voice_client import VoiceClient
from .errors import FeatureDisabled
if TYPE_CHECKING:
from .message import Message
@@ -17,28 +16,13 @@ if TYPE_CHECKING:
from .state import State
from .types import Channel as ChannelPayload
from .types import DMChannel as DMChannelPayload
from .types import GroupDMChannel as GroupDMChannelPayload
from .types import Group as GroupDMChannelPayload
from .types import SavedMessages as SavedMessagesPayload
from .types import TextChannel as TextChannelPayload
from .types import VoiceChannel as VoiceChannelPayload
from .user import User
__all__ = ("DMChannel", "GroupDMChannel", "SavedMessageChannel", "TextChannel", "VoiceChannel", "Channel")
class EditableChannel:
__slots__ = ()
state: State
id: str
async def edit(self, **kwargs):
remove = []
if kwargs.get("icon", Missing) == None:
remove.append("Icon")
elif kwargs.get("description", Missing) == None:
remove.append("Description")
await self.state.http.edit_channel(self.id, remove, kwargs)
__all__ = ("Channel",)
class Channel:
"""Base class for all channels
@@ -49,6 +33,8 @@ class Channel:
The id of the channel
channel_type: ChannelType
The type of the channel
server: Optional[:class:`Server`]
The server the channel is part of
"""
__slots__ = ("state", "id", "channel_type", "server_id")
@@ -58,21 +44,13 @@ class Channel:
self.channel_type = ChannelType(data["channel_type"])
self.server_id = ""
async def _get_channel_id(self) -> str:
return self.id
@property
def server(self) -> Optional[Server]:
return self.state.get_server(self.server_id) if self.server_id else None
def _update(self):
pass
async def delete(self):
"""Deletes or closes the channel"""
await self.state.http.close_channel(self.id)
@property
def server(self) -> Server:
""":class:`Server` The server this voice channel belongs too"""
return self.state.get_server(self.server_id)
class SavedMessageChannel(Channel, Messageable):
"""The Saved Message Channel"""
def __init__(self, data: SavedMessagesPayload, state: State):
@@ -83,54 +61,26 @@ class DMChannel(Channel, Messageable):
def __init__(self, data: DMChannelPayload, state: State):
super().__init__(data, state)
class GroupDMChannel(Channel, Messageable, EditableChannel):
"""A group DM channel
Attributes
-----------
recipients: list[:class:`User`]
The recipients of the group dm channel
name: :class:`str`
The name of the group dm channel
owner: :class:`User`
The user who created the group dm channel
icon: Optional[:class:`Asset`]
The icon of the group dm channel
permissions: :class:`ChannelPermissions`
The permissions of the users inside the group dm channel
description: Optional[:class:`str`]
The description of the channel, if any
"""
__slots__ = ("recipients", "name", "owner", "permissions", "icon", "description")
class GroupDMChannel(Channel, Messageable):
__slots__ = ("recipients", "name", "owner", "permissions")
"""A group DM channel"""
def __init__(self, data: GroupDMChannelPayload, state: State):
super().__init__(data, state)
self.recipients = [state.get_user(user_id) for user_id in data["recipients"]]
self.name = data["name"]
self.owner = state.get_user(data["owner"])
self.description = data.get("description")
if icon := data.get("icon"):
self.icon = Asset(icon, state)
else:
self.icon = None
if perms := data.get("permissions"):
self.permissions = ChannelPermissions._from_value(perms)
else:
self.permissions = ChannelPermissions._from_value(0)
def _update(self, *, name: Optional[str] = None, recipients: Optional[list[str]] = None, description: Optional[str] = None):
def _update(self, *, name: Optional[str] = None, recipients: Optional[list[str]] = None):
if name:
self.name = name
if recipients:
self.recipients = [self.state.get_user(user_id) for user_id in recipients]
if description:
self.description = description
async def set_default_permissions(self, permissions: ChannelPermissions) -> None:
"""Sets the default permissions for a group.
Parameters
@@ -140,28 +90,10 @@ class GroupDMChannel(Channel, Messageable, EditableChannel):
"""
await self.state.http.set_channel_default_permissions(self.id, permissions.value)
class TextChannel(Channel, Messageable, EditableChannel):
__slots__ = ("name", "description", "last_message_id", "server_id", "default_permissions", "role_permissions", "icon")
class TextChannel(Channel, Messageable):
__slots__ = ("name", "description", "last_message_id", "server_id", "default_permissions", "role_permissions")
"""A text channel
Attributes
-----------
name: :class:`str`
The name of the text channel
server_id: :class:`str`
The id of the server this text channel belongs to
last_message_id: Optional[:class:`str`]
The id of the last message in this channel, if any
default_permissions: :class:`ChannelPermissions`
The default permissions for all users in the text channel
role_permissions: dict[:class:`str`, :class:`ChannelPermissions`]
A dictionary of role id's to the permissions of that role in the text channel
icon: Optional[:class:`Asset`]
The icon of the text channel, if any
description: Optional[:class:`str`]
The description of the channel, if any
"""
"""A text channel"""
def __init__(self, data: TextChannelPayload, state: State):
super().__init__(data, state)
@@ -172,29 +104,17 @@ class TextChannel(Channel, Messageable, EditableChannel):
last_message_id = data.get("last_message")
self.last_message_id = last_message_id
self.default_permissions = ChannelPermissions._from_value(data.get("default_permissions", 0))
self.role_permissions = {role_id: ChannelPermissions._from_value(perms) for role_id, perms in data.get("role_permissions", {}).items()}
if perms := data.get("default_permissions"):
self.default_permissions = ChannelPermissions._from_value(perms)
if icon := data.get("icon"):
self.icon = Asset(icon, state)
else:
self.icon = None
if role_perms := data.get("role_permissions"):
self.role_permissions = {role_id: ChannelPermissions._from_value(perms) for role_id, perms in role_perms.items()}
def _get_channel_id(self) -> str:
return self.id
@property
def last_message(self) -> Optional[Message]:
"""Gets the last message from the channel, shorthand for `client.get_message(channel.last_message_id)`
Returns
--------
:class:`Message` the last message in the channel
"""
if not self.last_message_id:
return
def last_message(self) -> Message:
return self.state.get_message(self.last_message_id)
def _update(self, *, name: Optional[str] = None, description: Optional[str] = None):
@@ -222,26 +142,8 @@ class TextChannel(Channel, Messageable, EditableChannel):
"""
await self.state.http.set_channel_role_permissions(self.id, role.id, permissions.value)
class VoiceChannel(Channel, EditableChannel):
"""A voice channel
Attributes
-----------
name: :class:`str`
The name of the voice channel
server_id: :class:`str`
The id of the server this voice channel belongs to
last_message_id: Optional[:class:`str`]
The id of the last message in this channel, if any
default_permissions: :class:`ChannelPermissions`
The default permissions for all users in the voice channel
role_permissions: dict[:class:`str`, :class:`ChannelPermissions`]
A dictionary of role id's to the permissions of that role in the voice channel
icon: Optional[:class:`Asset`]
The icon of the voice channel, if any
description: Optional[:class:`str`]
The description of the channel, if any
"""
class VoiceChannel(Channel):
"""A voice channel"""
def __init__(self, data: VoiceChannelPayload, state: State):
super().__init__(data, state)
@@ -251,18 +153,9 @@ class VoiceChannel(Channel, EditableChannel):
if perms := data.get("default_permissions"):
self.default_permissions = ChannelPermissions._from_value(perms)
else:
self.default_permissions = ChannelPermissions._from_value(0)
if role_perms := data.get("role_permissions"):
self.role_permissions = {role_id: ChannelPermissions._from_value(perms) for role_id, perms in role_perms.items()}
else:
self.role_permissions = {}
if icon := data.get("icon"):
self.icon = Asset(icon, state)
else:
self.icon = None
def _update(self, *, name: Optional[str] = None, description: Optional[str] = None):
if name:
@@ -289,7 +182,25 @@ class VoiceChannel(Channel, EditableChannel):
"""
await self.state.http.set_channel_role_permissions(self.id, role.id, permissions.value)
def channel_factory(data: ChannelPayload, state: State) -> Union[DMChannel, GroupDMChannel, SavedMessageChannel, TextChannel, VoiceChannel]:
async def connect(self):
token = (await self.state.http.connect_to_voice(self.id))["token"]
voso = self.state.api_info["features"]["voso"]
if not voso["enabled"]:
raise FeatureDisabled("vortex is disabled.")
voice_client = VoiceClient(self.state.http.session, voso["ws"], self.id, token, self.state)
await voice_client.start()
await voice_client.send_authenticate()
print("auth")
await voice_client.send_initialize_transport()
print("init")
await voice_client.send_connect_transport()
print("connect")
return voice_client
def channel_factory(data: ChannelPayload, state: State) -> Channel:
if data["channel_type"] == "SavedMessage":
return SavedMessageChannel(data, state)
elif data["channel_type"] == "DirectMessage":
+11 -123
View File
@@ -2,15 +2,11 @@ from __future__ import annotations
import asyncio
import logging
from typing import TYPE_CHECKING, Any, Callable, Optional, Union, cast
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Optional, TypeVar
import aiohttp
from .channel import (DMChannel, GroupDMChannel, SavedMessageChannel,
TextChannel, VoiceChannel, channel_factory)
from .http import HttpClient
from .invite import Invite
from .message import Message
from .state import State
from .websocket import WebsocketHandler
@@ -91,11 +87,11 @@ class Client:
self.api_info = api_info
self.http = HttpClient(self.session, self.token, self.api_url, self.api_info)
self.state = State(self.http, api_info, self.max_messages)
self.state = State(self.http, api_info, self.max_messages, self.dispatch)
self.websocket = WebsocketHandler(self.session, self.token, api_info["ws"], self.dispatch, self.state)
await self.websocket.start()
def get_user(self, id: str) -> User:
def get_user(self, id: str) -> Optional[User]:
"""Gets a user from the cache
Parameters
@@ -105,12 +101,12 @@ class Client:
Returns
--------
:class:`User`
The user
Optional[:class:`User`]
The user if found
"""
return self.state.get_user(id)
def get_channel(self, id: str) -> Channel:
def get_channel(self, id: str) -> Optional[Channel]:
"""Gets a channel from the cache
Parameters
@@ -120,12 +116,12 @@ class Client:
Returns
--------
:class:`Channel`
The channel
Optional[:class:`Channel`]
The channel if found
"""
return self.state.get_channel(id)
def get_server(self, id: str) -> Server:
def get_server(self, id: str) -> Optional[Server]:
"""Gets a server from the cache
Parameters
@@ -135,8 +131,8 @@ class Client:
Returns
--------
:class:`Server`
The server
Optional[:class:`Server`]
The server if found
"""
return self.state.get_server(id)
@@ -172,115 +168,7 @@ class Client:
@property
def user(self) -> User:
""":class:`User` the user corrasponding to the client"""
user = self.websocket.user
assert user
return user
@property
def users(self) -> list[User]:
"""list[:class:`User`] All users the client can see"""
return list(self.state.users.values())
async def fetch_user(self, user_id: str) -> User:
"""Fetchs a user
Parameters
-----------
user_id: :class:`str`
The id of the user you are fetching
Returns
--------
:class:`User`
The user with the matching id
"""
payload = await self.http.fetch_user(user_id)
return User(payload, self.state)
async def fetch_dm_channels(self) -> list[Union[DMChannel, GroupDMChannel]]:
"""Fetchs all dm channels the client has made
Returns
--------
list[Union[:class:`DMChanel`, :class:`GroupDMChannel`]]
A list of :class:`DMChannel` or :class`GroupDMChannel`
"""
channel_payloads = await self.http.fetch_dm_channels()
return cast(list[Union[DMChannel, GroupDMChannel]], [channel_factory(payload, self.state) for payload in channel_payloads])
async def fetch_channel(self, channel_id: str) -> Union[DMChannel, GroupDMChannel, SavedMessageChannel, TextChannel, VoiceChannel]:
"""Fetches a channel
Parameters
-----------
channel_id: :class:`str`
The id of the channel
Returns
--------
Union[:class:`DMChannel`, :class:`GroupDMChannel`, :class:`SavedMessageChannel`, :class:`TextChannel`, :class:`VoiceChannel`]
The channel with the matching id
"""
payload = await self.http.fetch_channel(channel_id)
return channel_factory(payload, self.state)
async def fetch_server(self, server_id: str) -> Server:
"""Fetchs a server
Parameters
-----------
server_id: :class:`str`
The id of the server you are fetching
Returns
--------
:class:`Server`
The server with the matching id
"""
payload = await self.http.fetch_server(server_id)
return Server(payload, self.state)
async def fetch_invite(self, code: str) -> Invite:
"""Fetchs an invite
Parameters
-----------
code: :class:`str`
The code of the invite you are fetching
Returns
--------
:class:`Invite`
The invite with the matching code
"""
payload = await self.http.fetch_invite(code)
return Invite(payload, code, self.state)
def get_message(self, message_id: str) -> Message:
"""Gets a message from the cache
Parameters
-----------
message_id: :class:`str`
The id of the message you are getting
Returns
--------
:class:`Message`
The message with the matching id
Raises
-------
LookupError
This raises if the message is not found in the cache
"""
for message in self.state.messages:
if message.id == message_id:
return message
raise LookupError
+8 -97
View File
@@ -1,106 +1,17 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Optional, Union
from .enums import EmbedType
from .asset import Asset
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .state import State
from .types import Embed as EmbedPayload
from .types import SendableEmbed as SendableEmbedPayload
from .types import WebsiteEmbed as WebsiteEmbedPayload
from .types import ImageEmbed as ImageEmbedPayload
from .types import TextEmbed as TextEmbedPayload
from .types import NoneEmbed as NoneEmbedPayload
__all__ = ("Embed", "WebsiteEmbed", "ImageEmbed", "TextEmbed", "NoneEmbed", "to_embed", "SendableEmbed")
class WebsiteEmbed:
type = EmbedType.website
__all__ = ("Embed",)
def __init__(self, embed: WebsiteEmbedPayload):
self.url = embed.get("url")
self.special = embed.get("special")
self.title = embed.get("title")
self.description = embed.get("description")
self.image = embed.get("image")
self.video = embed.get("video")
self.site_name = embed.get("site_name")
self.icon_url = embed.get("icon_url")
self.colour = embed.get("colour")
class Embed:
@classmethod
def from_dict(cls, data: EmbedPayload):
...
class ImageEmbed:
type = EmbedType.image
def __init__(self, image: ImageEmbedPayload):
self.url = image.get("url")
self.width = image.get("width")
self.height = image.get("height")
self.size = image.get("size")
class TextEmbed:
type = EmbedType.text
def __init__(self, embed: TextEmbedPayload, state: State):
self.icon_url = embed.get("icon_url")
self.url = embed.get("url")
self.title = embed.get("title")
self.description = embed.get("description")
if media := embed.get("media"):
self.media = Asset(media, state)
else:
self.media = None
self.colour = embed.get("colour")
class NoneEmbed:
type = EmbedType.none
Embed = Union[WebsiteEmbed, ImageEmbed, TextEmbed, NoneEmbed]
def to_embed(payload: EmbedPayload, state: State) -> Embed:
if payload["type"] == "Website":
return WebsiteEmbed(payload)
elif payload["type"] == "Image":
return ImageEmbed(payload)
elif payload["type"] == "Text":
return TextEmbed(payload, state)
else:
return NoneEmbed()
class SendableEmbed:
def __init__(self, **attrs):
self.title: Optional[str] = None
self.description: Optional[str] = None
self.media: Optional[str] = None
self.icon_url: Optional[str] = None
self.colour: Optional[str] = None
self.url: Optional[str] = None
for key, value in attrs.items():
setattr(self, key, value)
def to_dict(self) -> SendableEmbedPayload:
output: SendableEmbedPayload = {"type": "Text"}
if title := self.title:
output["title"] = title
if description := self.description:
output["description"] = description
if media := self.media:
output["media"] = media
if icon_url := self.icon_url:
output["icon_url"] = icon_url
if colour := self.colour:
output["colour"] = colour
if url := self.url:
output["url"] = url
return output
def to_dict(self) -> EmbedPayload:
...
-7
View File
@@ -14,7 +14,6 @@ __all__ = (
"RelationshipType",
"AssetType",
"SortType",
"EmbedType"
)
class ChannelType(enum.Enum):
@@ -50,9 +49,3 @@ class SortType(enum.Enum):
latest = "Latest"
oldest = "Oldest"
relevance = "Relevance"
class EmbedType(enum.Enum):
website = "Website"
image = "Image"
text = "Text"
none = "None"
-3
View File
@@ -2,7 +2,4 @@ from .checks import *
from .client import *
from .command import *
from .context import *
from .converters import *
from .errors import *
from .group import *
from .cog import *
+1 -1
View File
@@ -29,7 +29,7 @@ def check(check: Check):
else:
checks = getattr(func, "_checks", [])
checks.append(check)
func._checks = checks # type: ignore
func._checks = checks
return func
+10 -46
View File
@@ -1,28 +1,24 @@
from __future__ import annotations
import re
import traceback
import sys
from typing import Any, Union, Protocol, runtime_checkable
from importlib import import_module
from typing import Any, Callable, Union
import revolt
from .command import Command
from .context import Context
from .errors import CheckError, CommandNotFound, MissingSetup
from .errors import CheckError, CommandNotFound
from .view import StringView
from .cog import Cog
__all__ = (
"CommandsMeta",
"CommandsClient"
)
@runtime_checkable
class ExtensionProtocol(Protocol):
@staticmethod
def setup(client: CommandsClient):
raise NotImplementedError
quote_regex = re.compile(r"[\"']")
chunk_regex = re.compile(r"\S+")
class CommandsMeta(type):
_commands: list[Command]
@@ -30,6 +26,7 @@ class CommandsMeta(type):
def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]):
commands: list[Command] = []
self = super().__new__(cls, name, bases, attrs)
for base in reversed(self.__mro__):
for value in base.__dict__.values():
if isinstance(value, Command):
@@ -37,6 +34,9 @@ class CommandsMeta(type):
self._commands = commands
for command in commands:
command._client = self # type: ignore
return self
class CommandsClient(revolt.Client, metaclass=CommandsMeta):
@@ -46,8 +46,6 @@ class CommandsClient(revolt.Client, metaclass=CommandsMeta):
def __init__(self, *args, **kwargs):
self.all_commands: dict[str, Command] = {}
self.cogs: dict[str, Cog] = {}
self.extensions: dict[str, ExtensionProtocol] = {}
for command in self._commands:
self.all_commands[command.name] = command
@@ -103,9 +101,6 @@ class CommandsClient(revolt.Client, metaclass=CommandsMeta):
"""
self.all_commands[name] = command
for alias in command.aliases:
self.all_commands[alias] = command
def get_view(self, message: revolt.Message) -> type[StringView]:
return StringView
@@ -176,7 +171,6 @@ class CommandsClient(revolt.Client, metaclass=CommandsMeta):
return output
except Exception as e:
await command._error_handler(command.cog or self, context, e)
self.dispatch("command_error", context, e)
@staticmethod
@@ -199,33 +193,3 @@ class CommandsClient(revolt.Client, metaclass=CommandsMeta):
"""
return True
def add_cog(self, cog: Cog):
cog._inject(self)
def remove_cog(self, cog_name: str) -> Cog:
cog = self.cogs.pop(cog_name)
cog._uninject(self)
return cog
def load_extension(self, name: str):
extension = import_module(name)
if not isinstance(extension, ExtensionProtocol):
raise MissingSetup(f"'{extension}' is missing a setup function")
self.extensions[name] = extension
extension.setup(self)
def unload_extension(self, name: str):
extension = self.extensions.pop(name)
del sys.modules[name]
if teardown := getattr(extension, "teardown", None):
teardown(self)
def reload_extension(self, name: str):
self.unload_extension(name)
self.load_extension(name)
-43
View File
@@ -1,43 +0,0 @@
from __future__ import annotations
from distutils import command
from typing import TYPE_CHECKING, Any
from .command import Command
if TYPE_CHECKING:
from .client import CommandsClient
__all__ = ("Cog", "CogMeta")
class CogMeta(type):
_commands: list[Command]
def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]):
commands: list[Command] = []
self = super().__new__(cls, name, bases, attrs)
for base in reversed(self.__mro__):
for value in base.__dict__.values():
if isinstance(value, Command):
value.cog = self # type: ignore
commands.append(value)
self._commands = commands
return self
class Cog(metaclass=CogMeta):
_commands: list[Command]
def _inject(self, client: CommandsClient):
client.cogs[type(self).__name__] = self
for command in self._commands:
client.add_command(command.name, command)
def _uninject(self, client: CommandsClient):
for name, command in client.all_commands.copy().items():
if command in self._commands:
del client.all_commands[name]
+25 -31
View File
@@ -4,26 +4,22 @@ import inspect
import traceback
from contextlib import suppress
from typing import (TYPE_CHECKING, Annotated, Any, Callable, Coroutine,
Literal, Optional, Union, cast, get_args, get_origin)
Literal, Optional, Union, get_args, get_origin)
import revolt
from revolt.utils import copy_doc, maybe_coroutine
from .errors import InvalidLiteralArgument, UnionConverterError
from .errors import InvalidLiteralArgument
if TYPE_CHECKING:
from .checks import Check
from .context import Context
from .group import Group
__all__ = (
"Command",
"command"
)
NoneType = type(None)
class Command:
"""Class for holding info about a command.
@@ -35,10 +31,10 @@ class Command:
The name of the command
aliases: list[:class:`str`]
The aliases of the command
parent: Optional[:class:`Group`]
The parent of the command if this command is a subcommand
"""
__slots__ = ("callback", "name", "aliases", "signature", "checks", "parent", "_error_handler", "cog")
__slots__ = ("callback", "name", "aliases", "_client", "signature", "checks")
_client: revolt.Client
def __init__(self, callback: Callable[..., Coroutine[Any, Any, Any]], name: str, aliases: list[str]):
self.callback = callback
@@ -46,9 +42,6 @@ class Command:
self.aliases = aliases
self.signature = inspect.signature(self.callback)
self.checks: list[Check] = getattr(callback, "_checks", [])
self.parent: Optional[Group] = None
self.cog = None
self._error_handler: Callable[[Any, Context, Exception], Coroutine[Any, Any, Any]] = type(self)._default_error_handler
async def invoke(self, context: Context, *args, **kwargs) -> Any:
"""Runs the command and calls the error handler if the command errors.
@@ -61,9 +54,9 @@ class Command:
The arguments for the command
"""
try:
return await self.callback(self.cog or context.client, context, *args, **kwargs)
return await self.callback(self._client, context, *args, **kwargs)
except Exception as err:
return await self._error_handler(self.cog or context.client, context, err)
return await self._error_handler(context, err)
@copy_doc(invoke)
def __call__(self, context: Context, *args, **kwargs) -> Any:
@@ -86,10 +79,11 @@ class Command:
await ctx.send(str(error))
"""
self._error_handler = func
self._error_handler = func # type: ignore
return func
async def _default_error_handler(self, ctx: Context, error: Exception):
@staticmethod
async def _error_handler(ctx: Context, error: Exception):
traceback.print_exception(type(error), error, error.__traceback__)
@staticmethod
@@ -101,49 +95,49 @@ class Command:
return t
@classmethod
async def convert_argument(cls, arg: str, annotation: Any, context: Context):
if annotation is not inspect._empty:
if annotation is str: # no converting is needed - its already a string
async def convert_argument(cls, arg: str, parameter: inspect.Parameter, context: Context):
if annot := parameter.annotation:
if annot is str: # no converting is needed - its already a string
return arg
if origin := get_origin(annotation):
if origin := get_origin(annot):
if origin is Union:
for converter in get_args(annotation):
for converter in get_args(annot):
try:
return await cls.convert_argument(arg, converter, context)
return await maybe_coroutine(converter, arg, context)
except:
if converter is NoneType:
if converter is None:
context.view.undo()
return None
raise UnionConverterError(arg)
elif origin is Annotated:
converter: Callable[[str, Context], Any] = get_args(annotation)[1] # the typehint affects the other if statement somehow
converter: Callable[[str, Context], Any] = get_args(annot)[1] # the typehint affects the other if statement somehow
return await maybe_coroutine(converter, arg, context)
elif origin is Literal:
if arg in get_args(annotation):
if arg in get_args(annot):
return arg
else:
raise InvalidLiteralArgument(arg)
else:
return await maybe_coroutine(cast(Callable, annotation), arg, context)
annot: Callable[..., Any]
return await maybe_coroutine(annot, arg, context)
else:
return arg
async def parse_arguments(self, context: Context):
for name, parameter in list(self.signature.parameters.items())[2:]:
if parameter.kind == parameter.KEYWORD_ONLY:
context.kwargs[name] = await self.convert_argument(context.view.get_rest(), parameter.annotation, context)
context.kwargs[name] = await self.convert_argument(context.view.get_rest(), parameter, context)
elif parameter.kind == parameter.VAR_POSITIONAL:
with suppress(StopIteration):
while True:
context.args.append(await self.convert_argument(context.view.get_next_word(), parameter.annotation, context))
context.args.append(await self.convert_argument(context.view.get_next_word(), parameter, context))
elif parameter.kind == parameter.POSITIONAL_OR_KEYWORD:
context.args.append(await self.convert_argument(context.view.get_next_word(), parameter.annotation, context))
context.args.append(await self.convert_argument(context.view.get_next_word(), parameter, context))
def __repr__(self) -> str:
return f"<Command name=\"{self.name}\">"
+5 -19
View File
@@ -6,7 +6,6 @@ import revolt
from revolt.utils import maybe_coroutine
from .command import Command
from .group import Group
if TYPE_CHECKING:
from .client import CommandsClient
@@ -42,7 +41,7 @@ class Context(revolt.Messageable):
"""
__slots__ = ("command", "invoked_with", "args", "message", "server", "channel", "author", "view", "kwargs", "state", "client")
async def _get_channel_id(self) -> str:
def _get_channel_id(self) -> str:
return self.channel.id
def __init__(self, command: Optional[Command], invoked_with: str, view: StringView, message: revolt.Message, client: CommandsClient):
@@ -59,7 +58,7 @@ class Context(revolt.Messageable):
self.state = message.state
async def invoke(self) -> Any:
"""Invokes the command.
"""Invokes the command, this is equal to `await command.invoke(context, context.args)`.
.. note:: If the command is `None`, this function will do nothing.
@@ -70,24 +69,11 @@ class Context(revolt.Messageable):
"""
if command := self.command:
if isinstance(command, Group):
try:
subcommand_name = self.view.get_next_word()
except StopIteration:
pass
else:
if subcommand := command.subcommands.get(subcommand_name):
self.command = command = subcommand
return await self.invoke()
self.view.undo()
await command.parse_arguments(self)
return await command.invoke(self, *self.args, **self.kwargs)
async def can_run(self, command: Optional[Command] = None) -> bool:
async def can_run(self) -> bool:
"""Runs all of the commands checks, and returns true if all of them pass"""
command = command or self.command
return all([await maybe_coroutine(check, self) for check in (self.command.checks if self.command else [])])
return all([await maybe_coroutine(check, self) for check in (command.checks if command else [])])
+2 -70
View File
@@ -1,17 +1,8 @@
import re
from typing import Annotated
from revolt import Category, Channel, Member, User, utils
from .errors import BadBoolArgument
from .context import Context
from .errors import (BadBoolArgument, CategoryConverterError,
ChannelConverterError, MemberConverterError, ServerOnly,
UserConverterError)
__all__ = ("bool_converter", "category_converter", "channel_converter", "user_converter", "member_converter", "IntConverter", "BoolConverter", "CategoryConverter", "UserConverter", "MemberConverter", "ChannelConverter")
channel_regex = re.compile("<#([A-z0-9]{26})>")
user_regex = re.compile("<@([A-z0-9]{26})>")
IntConverter = Annotated[int, lambda arg, _: int(arg)]
def bool_converter(arg: str, _):
lowered = arg.lower()
@@ -22,63 +13,4 @@ def bool_converter(arg: str, _):
else:
raise BadBoolArgument(lowered)
def category_converter(arg: str, context: Context) -> Category:
if not (server := context.server):
raise ServerOnly
try:
return server.get_category(arg)
except KeyError:
try:
return utils.get(server.categories, name=arg)
except LookupError:
raise CategoryConverterError(arg)
def channel_converter(arg: str, context: Context) -> Channel:
if not (server := context.server):
raise ServerOnly
if (match := channel_regex.match(arg)):
arg = match.group(1)
try:
return server.get_channel(arg)
except KeyError:
try:
return utils.get(server.channels, name=arg)
except LookupError:
raise ChannelConverterError(arg)
def user_converter(arg: str, context: Context) -> User:
if (match := user_regex.match(arg)):
arg = match.group(1)
try:
return context.client.get_user(arg)
except KeyError:
try:
return utils.get(context.client.users, name=arg)
except LookupError:
raise UserConverterError(arg)
def member_converter(arg: str, context: Context) -> Member:
if not (server := context.server):
raise ServerOnly
if (match := user_regex.match(arg)):
arg = match.group(1)
try:
return server.get_member(arg)
except KeyError:
try:
return utils.get(server.members, name=arg)
except LookupError:
raise MemberConverterError(arg)
IntConverter = Annotated[int, lambda arg, _: int(arg)]
BoolConverter = Annotated[bool, bool_converter]
CategoryConverter = Annotated[Category, category_converter]
UserConverter = Annotated[User, user_converter]
MemberConverter = Annotated[Member, member_converter]
ChannelConverter = Annotated[Channel, channel_converter]
+1 -34
View File
@@ -10,12 +10,7 @@ __all__ = (
"ServerOnly",
"ConverterError",
"InvalidLiteralArgument",
"BadBoolArgument",
"CategoryConverterError",
"ChannelConverterError",
"UserConverterError",
"MemberConverterError",
"MissingSetup",
"BadBoolArgument"
)
class CommandError(RevoltError):
@@ -57,31 +52,3 @@ class InvalidLiteralArgument(ConverterError):
class BadBoolArgument(ConverterError):
"""Raised when the bool converter fails"""
class CategoryConverterError(ConverterError):
"""Raised when the Category conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class ChannelConverterError(ConverterError):
"""Raised when the Channel conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class UserConverterError(ConverterError):
"""Raised when the Category conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class MemberConverterError(ConverterError):
"""Raised when the Category conveter fails"""
def __init__(self, argument: str):
self.argument = argument
class UnionConverterError(ConverterError):
"""Raised when all converters in a union fails"""
def __init__(self, argument: str):
self.argument = argument
class MissingSetup(CommandError):
"""Raised when an extension is missing the `setup` function"""
-113
View File
@@ -1,113 +0,0 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Optional, Callable, Coroutine, Any
from .command import Command
if TYPE_CHECKING:
from .context import Context
__all__ = (
"Group",
"group"
)
class Group(Command):
"""Class for holding info about a group command.
Parameters
-----------
callback: Callable[..., Coroutine[Any, Any, Any]]
The callback for the group command
name: :class:`str`
The name of the command
aliases: list[:class:`str`]
The aliases of the group command
subcommands: dict[:class:`str`, :class:`Command`]
The group's subcommands.
"""
__slots__ = ("subcommands",)
def __init__(self, callback: Callable[..., Coroutine[Any, Any, Any]], name: str, aliases: list[str]):
self.subcommands: dict[str, Command] = {}
super().__init__(callback, name, aliases)
def command(self, *, name: Optional[str] = None, aliases: Optional[list[str]] = None, cls: type[Command] = Command):
"""A decorator that turns a function into a :class:`Command` and registers the command as a subcommand.
Parameters
-----------
name: Optional[:class:`str`]
The name of the command, this defaults to the functions name
aliases: Optional[list[:class:`str`]]
The aliases of the command, defaults to no aliases
cls: type[:class:`Command`]
The class used for creating the command, this defaults to :class:`Command` but can be used to use a custom command subclass
Returns
--------
Callable[Callable[..., Coroutine], :class:`Command`]
A function that takes the command callback and returns a :class:`Command`
"""
def inner(func: Callable[..., Coroutine[Any, Any, Any]]):
command = cls(func, name or func.__name__, aliases or [])
command.parent = self
self.subcommands[command.name] = command
return command
return inner
def group(self, *, name: Optional[str] = None, aliases: Optional[list[str]] = None, cls: Optional[type["Group"]] = None):
"""A decorator that turns a function into a :class:`Group` and registers the command as a subcommand
Parameters
-----------
name: Optional[:class:`str`]
The name of the group command, this defaults to the functions name
aliases: Optional[list[:class:`str`]]
The aliases of the group command, defaults to no aliases
cls: type[:class:`Group`]
The class used for creating the command, this defaults to :class:`Group` but can be used to use a custom group subclass
Returns
--------
Callable[Callable[..., Coroutine], :class:`Group`]
A function that takes the command callback and returns a :class:`Group`
"""
cls = cls or type(self)
def inner(func: Callable[..., Coroutine[Any, Any, Any]]):
command = cls(func, name or func.__name__, aliases or [])
command.parent = self
self.subcommands[command.name] = command
return command
return inner
def __repr__(self) -> str:
return f"<Group name=\"{self.name}\">"
def group(*, name: Optional[str] = None, aliases: Optional[list[str]] = None, cls: type[Group] = Group):
"""A decorator that turns a function into a :class:`Group`
Parameters
-----------
name: Optional[:class:`str`]
The name of the group command, this defaults to the functions name
aliases: Optional[list[:class:`str`]]
The aliases of the group command, defaults to no aliases
cls: type[:class:`Group`]
The class used for creating the command, this defaults to :class:`Group` but can be used to use a custom group subclass
Returns
--------
Callable[Callable[..., Coroutine], :class:`Group`]
A function that takes the command callback and returns a :class:`Group`
"""
def inner(func: Callable[..., Coroutine[Any, Any, Any]]):
return cls(func, name or func.__name__, aliases or [])
return inner
+1 -4
View File
@@ -14,9 +14,6 @@ class StringView:
return next(self.value)
def get_rest(self) -> str:
if self.should_undo:
return f"{self.temp} {''.join(self.value)}"
return "".join(self.value)
def get_next_word(self) -> str:
@@ -41,7 +38,7 @@ class StringView:
else:
temp.append(char)
try:
while (char := self.next_char()) not in " \n":
while (char := self.next_char()) != " ":
temp.append(char)
except StopIteration:
pass
+13 -35
View File
@@ -23,14 +23,15 @@ if TYPE_CHECKING:
from .types import Autumn as AutumnPayload
from .types import Channel, DMChannel
from .types import Embed as EmbedPayload
from .types import GetServerMembers, Invite
from .types import GetServerMembers
from .types import Masquerade as MasqueradePayload
from .types import Member
from .types import Message as MessagePayload
from .types import (MessageReplyPayload, MessageWithUserData,
PartialInvite, Role, Server, ServerBans, TextChannel)
from .types import (MessageReplyPayload, MessageWithUserData, Role, Server,
ServerBans, ServerInvite, TextChannel, JoinCallResponse)
from .types import User as UserPayload
from .types import UserProfile, VoiceChannel, GroupDMChannel, Member
from .types import SendableEmbed as SendableEmbedPayload
from .types import UserProfile, VoiceChannel
__all__ = ("HttpClient",)
@@ -52,7 +53,7 @@ class HttpClient:
kwargs = {}
headers = {
"User-Agent": "Revolt.py (https://github.com/revoltchat/revolt.py)",
"User-Agent": "Revolt.py https://github.com/Zomatree/revolt.py",
"x-bot-token": self.token
}
@@ -87,7 +88,7 @@ class HttpClient:
url = f"{self.api_info['features']['autumn']['url']}/{tag}"
headers = {
"User-Agent": "Revolt.py (https://github.com/revoltchat/revolt.py)"
"User-Agent": "Revolt.py https://github.com/Zomatree/revolt.py"
}
form = aiohttp.FormData()
@@ -105,7 +106,7 @@ class HttpClient:
else:
return response
async def send_message(self, channel: str, content: Optional[str], embeds: Optional[list[SendableEmbedPayload]], attachments: Optional[list[File]], replies: Optional[list[MessageReplyPayload]], masquerade: Optional[MasqueradePayload]) -> MessagePayload:
async def send_message(self, channel: str, content: Optional[str], embeds: Optional[list[EmbedPayload]], attachments: Optional[list[File]], replies: Optional[list[MessageReplyPayload]], masquerade: Optional[MasqueradePayload]) -> MessagePayload:
json: dict[str, Any] = {}
if content:
@@ -266,7 +267,7 @@ class HttpClient:
def fetch_default_avatar(self, user_id: str) -> Request[bytes]:
return self.request_file(f"{self.api_url}/users/{user_id}/default_avatar")
def fetch_dm_channels(self) -> Request[list[Union[DMChannel, GroupDMChannel]]]:
def fetch_dm_channels(self) -> Request[list[Channel]]:
return self.request("GET", "/users/dms")
def open_dm(self, user_id: str) -> Request[DMChannel]:
@@ -292,14 +293,6 @@ class HttpClient:
def delete_leave_server(self, server_id: str) -> Request[None]:
return self.request("DELETE", f"/servers/{server_id}")
@overload
def create_channel(self, server_id: str, channel_type: Literal["Text"], name: str, description: Optional[str]) -> Request[TextChannel]:
...
@overload
def create_channel(self, server_id: str, channel_type: Literal["Voice"], name: str, description: Optional[str]) -> Request[VoiceChannel]:
...
def create_channel(self, server_id: str, channel_type: Literal["Text", "Voice"], name: str, description: Optional[str]) -> Request[Union[TextChannel, VoiceChannel]]:
payload = {
"type": channel_type,
@@ -311,7 +304,7 @@ class HttpClient:
return self.request("POST", f"/servers/{server_id}/channels", json=payload)
def fetch_server_invites(self, server_id: str) -> Request[list[PartialInvite]]:
def fetch_server_invites(self, server_id: str) -> Request[list[ServerInvite]]:
return self.request("GET", f"/servers/{server_id}/invites")
def fetch_member(self, server_id: str, member_id: str) -> Request[Member]:
@@ -360,20 +353,5 @@ class HttpClient:
def delete_role(self, server_id: str, role_id: str) -> Request[None]:
return self.request("DELETE", f"/servers/{server_id}/roles/{role_id}")
def fetch_invite(self, code: str) -> Request[Invite]:
return self.request("GET", f"/invites/{code}")
def delete_invite(self, code: str) -> Request[None]:
return self.request("DELETE", f"/invites/{code}")
def edit_channel(self, channel_id: str, remove: list[str], values: dict[str, Any]):
if remove:
values["remove"] = remove
return self.request("PATCH", f"/channels/{channel_id}", json=values)
def edit_role(self, server_id: str, role_id: str, remove: list[str], values: dict[str, Any]):
if remove:
values["remove"] = remove
return self.request("PATCH", f"/servers/{server_id}/roles/{role_id}", json=values)
def connect_to_voice(self, channel_id) -> Request[JoinCallResponse]:
return self.request("POST", f"/channels/{channel_id}/join_call")
-67
View File
@@ -1,67 +0,0 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from .asset import Asset
if TYPE_CHECKING:
from .state import State
from .types import Invite as InvitePayload
__all__ = ("Invite",)
class Invite:
"""Represents a server invite.
Attributes
-----------
code: :class:`str`
The code for the invite
server: :class:`Server`
The server this invite is for
channel: :class:`Channel`
The channel this invite is for
user_name: :class:`str`
The name of the user who made the invite
user: Optional[:class:`User`]
The user who made the invite, this is only set if this was fetched via :meth:`Server.fetch_invites`
user_avatar: Optional[:class:`Asset`]
The invite creator's avatar, if any
member_count: :class:`int`
The member count of the server this invite is for
"""
def __init__(self, data: InvitePayload, code: str, state: State):
self.state = state
self.code = code
self.server = state.get_server(data["server_id"])
self.channel = self.server.get_channel(data["channel_id"])
self.user_name = data["user_name"]
self.user = None
if avatar := data.get("user_avatar"):
self.user_avatar = Asset(avatar, state)
else:
self.user_avatar = None
self.member_count = data["member_count"]
@staticmethod
def _from_partial(code: str, server: str, creator: str, channel: str, state: State) -> Invite:
invite = Invite.__new__(Invite)
invite.state = state
invite.code = code
invite.server = state.get_server(server)
invite.channel = state.get_channel(channel)
invite.user = state.get_user(creator)
invite.user_name = invite.user.name
invite.user_avatar = invite.user.avatar
invite.member_count = len(invite.server.members)
return invite
async def delete(self):
"""Deletes the invite"""
await self.state.http.delete_invite(self.code)
-18
View File
@@ -65,21 +65,3 @@ class Member(User):
if roles:
member_roles = [self.server.get_role(role_id) for role_id in roles]
self.roles = sorted(member_roles, key=lambda role: role.rank, reverse=True)
async def kick(self):
"""Kicks the member from the server"""
await self.state.http.kick_member(self.server.id, self.id)
async def ban(self, *, reason: Optional[str] = None):
"""Bans the member from the server
Parameters
-----------
reason: Optional[:class:`str`]
The reason for the ban
"""
await self.state.http.ban_member(self.server.id, self.id, reason)
async def unban(self):
"""Unbans the member from the server"""
await self.state.http.unban_member(self.server.id, self.id)
+3 -3
View File
@@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, NamedTuple, Optional
from .asset import Asset, PartialAsset
from .channel import Messageable
from .embed import to_embed
from .embed import Embed
if TYPE_CHECKING:
from .state import State
@@ -31,7 +31,7 @@ class Message:
The content of the message, this will not include system message's content
attachments: list[:class:`Asset`]
The attachments of the message
embeds: list[Union[:class:`WebsiteEmbed`, :class:`ImageEmbed`, :class:`TextEmbed`, :class:`NoneEmbed`]]
embeds: list[:class:`Embed`]
The embeds of the message
channel: :class:`Messageable`
The channel the message was sent in
@@ -56,7 +56,7 @@ class Message:
self.id = data["_id"]
self.content = data["content"]
self.attachments = [Asset(attachment, state) for attachment in data.get("attachments", [])]
self.embeds = [to_embed(embed, state) for embed in data.get("embeds", [])]
self.embeds = [Embed.from_dict(embed) for embed in data.get("embeds", [])]
channel = state.get_channel(data["channel"])
assert isinstance(channel, Messageable)
+6 -75
View File
@@ -2,10 +2,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from .enums import SortType
if TYPE_CHECKING:
from .embed import Embed, SendableEmbed
from .embed import Embed
from .file import File
from .message import Masquerade, Message, MessageReply
from .state import State
@@ -25,10 +23,10 @@ class Messageable:
__slots__ = ()
async def _get_channel_id(self) -> str:
def _get_channel_id(self) -> str:
raise NotImplementedError
async def send(self, content: Optional[str] = None, *, embeds: Optional[list[SendableEmbed]] = None, embed: Optional[SendableEmbed] = None, attachments: Optional[list[File]] = None, replies: Optional[list[MessageReply]] = None, reply: Optional[MessageReply] = None, masquerade: Optional[Masquerade] = None) -> Message:
async def send(self, content: Optional[str] = None, *, embeds: Optional[list[Embed]] = None, embed: Optional[Embed] = None, attachments: Optional[list[File]] = None, replies: Optional[list[MessageReply]] = None, reply: Optional[MessageReply] = None, masquerade: Optional[Masquerade] = None) -> Message:
"""Sends a message in a channel, you must send at least one of either `content`, `embeds` or `attachments`
Parameters
@@ -37,10 +35,8 @@ class Messageable:
The content of the message, this will not include system message's content
attachments: Optional[list[:class:`File`]]
The attachments of the message
embed: Optional[:class:`SendableEmbed`]
The embed to send with the message
embeds: Optional[list[:class:`SendableEmbed`]]
The embeds to send with the message
embeds: Optional[list[:class:`Embed`]]
The embeds of the message
replies: Optional[list[:class:`MessageReply`]]
The list of messages to reply to.
@@ -59,70 +55,5 @@ class Messageable:
reply_payload = [reply.to_dict() for reply in replies] if replies else None
masquerade_payload = masquerade.to_dict() if masquerade else None
message = await self.state.http.send_message(await self._get_channel_id(), content, embed_payload, attachments, reply_payload, masquerade_payload)
message = await self.state.http.send_message(self._get_channel_id(), content, embed_payload, attachments, reply_payload, masquerade_payload)
return self.state.add_message(message)
async def fetch_message(self, message_id: str) -> Message:
"""Fetches a message from the channel
Parameters
-----------
message_id: :class:`str`
The id of the message you want to fetch
Returns
--------
:class:`Message`
The message with the matching id
"""
payload = await self.state.http.fetch_message(await self._get_channel_id(), message_id)
return Message(payload, self.state)
async def history(self, *, sort: SortType = SortType.latest, limit: int = 100, before: Optional[str] = None, after: Optional[str] = None, nearby: Optional[str] = None) -> list[Message]:
"""Fetches multiple messages from the channel's history
Parameters
-----------
sort: :class:`SortType`
The order to sort the messages in
limit: :class:`int`
How many messages to fetch
before: Optional[:class:`str`]
The id of the message which should come *before* all the messages to be fetched
after: Optional[:class:`str`]
The id of the message which should come *after* all the messages to be fetched
nearby: Optional[:class:`str`]
The id of the message which should be nearby all the messages to be fetched
Returns
--------
list[:class:`Message`]
The messages found in order of the sort parameter
"""
payloads = await self.state.http.fetch_messages(await self._get_channel_id(), sort=sort, limit=limit, before=before, after=after, nearby=nearby)
return [Message(payload, self.state) for payload in payloads]
async def search(self, query: str, *, sort: SortType = SortType.latest, limit: int = 100, before: Optional[str] = None, after: Optional[str] = None) -> list[Message]:
"""searches the channel for a query
Parameters
-----------
query: :class:`str`
The query to search for in the channel
sort: :class:`SortType`
The order to sort the messages in
limit: :class:`int`
How many messages to fetch
before: Optional[:class:`str`]
The id of the message which should come *before* all the messages to be fetched
after: Optional[:class:`str`]
The id of the message which should come *after* all the messages to be fetched
Returns
--------
list[:class:`Message`]
The messages found in order of the sort parameter
"""
payloads = await self.state.http.search_messages(await self._get_channel_id(), query, sort=sort, limit=limit, before=before, after=after)
return [Message(payload, self.state) for payload in payloads]
+2 -1
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
from .flags import Flags, flag_value
__all__ = (
"ChannelPermissions",
"ServerPermissions"
@@ -42,7 +43,7 @@ class ChannelPermissions(Flags):
@classmethod
def all(cls) -> ChannelPermissions:
return cls._from_value(0b11111111)
return cls._from_value(0b11111011)
@classmethod
def view(cls) -> ChannelPermissions:
+7 -24
View File
@@ -2,7 +2,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from .utils import Missing
from revolt.types import server
from .permissions import ChannelPermissions, ServerPermissions
if TYPE_CHECKING:
@@ -22,7 +23,7 @@ class Role:
The id of the role
name: :class:`str`
The name of the role
colour: Optional[:class:`str`]
colour: :class:`str`
The colour of the role
hoist: :class:`bool`
Whether members with the role will display seperate from everyone else
@@ -37,13 +38,13 @@ class Role:
"""
__slots__ = ("id", "name", "colour", "hoist", "rank", "state", "server", "server_permissions", "channel_permissions")
def __init__(self, data: RolePayload, role_id: str, server: Server, state: State):
def __init__(self, data: RolePayload, role_id: str, state: State, server: Server):
self.state = state
self.id = role_id
self.name = data["name"]
self.colour = None
self.hoist = False
self.rank = 0
self.colour = data.get("colour")
self.hoist = data.get("hoist", False)
self.rank = data.get("rank", 0)
self.server = server
self.server_permissions = ServerPermissions._from_value(data["permissions"][0])
self.channel_permissions = ChannelPermissions._from_value(data["permissions"][1])
@@ -82,21 +83,3 @@ class Role:
if rank:
self.rank = rank
async def delete(self):
"""Deletes the role"""
await self.state.http.delete_role(self.server.id, self.id)
async def edit(self, **kwargs):
"""Edits the role
Parameters
-----------
"""
remove = []
if kwargs.get("colour", Missing) is None:
kwargs.pop("colour")
remove.append("Colour")
await self.state.http.edit_role(self.server.id, self.id, remove, kwargs)
+13 -151
View File
@@ -1,11 +1,10 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Optional, cast
from typing import TYPE_CHECKING, Optional
from .asset import Asset
from .category import Category
from .channel import Channel, VoiceChannel
from .invite import Invite
from .channel import Channel
from .permissions import ChannelPermissions, ServerPermissions
from .role import Role
@@ -17,7 +16,7 @@ if TYPE_CHECKING:
from .types import File as FilePayload
from .types import Permission as PermissionPayload
from .types import Server as ServerPayload
from .types import SystemMessagesConfig, Ban
from .types import SystemMessagesConfig
__all__ = ("Server", "SystemMessages")
@@ -83,12 +82,14 @@ class Server:
Whether the server is nsfw or not
system_messages: :class:`SystemMessages`
The system message config for the server
categories: list[:class:`Category`]
The categories in the server
icon: Optional[:class:`Asset`]
The servers icon
banner: Optional[:class:`Asset`]
The servers banner
"""
__slots__ = ("state", "id", "name", "owner_id", "default_server_permissions", "default_channel_permissions", "_members", "_roles", "_channels", "description", "icon", "banner", "nsfw", "system_messages", "_categories")
__slots__ = ("state", "id", "name", "owner_id", "default_server_permissions", "default_channel_permissions", "_members", "_roles", "_channels", "description", "icon", "banner", "nsfw", "system_messages", "categories")
def __init__(self, data: ServerPayload, state: State):
self.state = state
@@ -99,8 +100,8 @@ class Server:
self.default_channel_permissions = ChannelPermissions._from_value(data["default_permissions"][1])
self.description = data.get("description") or None
self.nsfw = data.get("nsfw", False)
self.system_messages = SystemMessages(data.get("system_messages", cast("SystemMessagesConfig", {})), state)
self._categories = {data["id"]: Category(data, state) for data in data.get("categories", [])}
self.system_messages = SystemMessages(data.get("system_messages", {}), state)
self.categories = [Category(data, state) for data in data.get("categories", [])]
if icon := data.get("icon"):
self.icon = Asset(icon, state)
@@ -113,9 +114,10 @@ class Server:
self.banner = None
self._members: dict[str, Member] = {}
self._roles: dict[str, Role] = {role_id: Role(role, role_id, self, state) for role_id, role in data.get("roles", {}).items()}
self._roles: dict[str, Role] = {role_id: Role(role, role_id, state, self) for role_id, role in data.get("roles", {}).items()}
self._channels: dict[str, Channel] = {channel_id: state.get_channel(channel_id) for channel_id in data.get("channels", [])}
channels = [state.get_channel(channel_id) for channel_id in data["channels"]]
self._channels: dict[str, Channel] = {channel.id: channel for channel in channels}
def _update(self, *, owner: Optional[str] = None, name: Optional[str] = None, description: Optional[str] = None, icon: Optional[FilePayload] = None, banner: Optional[FilePayload] = None, default_permissions: Optional[PermissionPayload] = None, nsfw: Optional[bool] = None, system_messages: Optional[SystemMessagesConfig] = None, categories: Optional[list[CategoryPayload]] = None):
if owner:
@@ -136,13 +138,13 @@ class Server:
if system_messages is not None:
self.system_messages = SystemMessages(system_messages, self.state)
if categories is not None:
self._categories = {data["id"]: Category(data, self.state) for data in categories}
self.categories = [Category(data, self.state) for data in categories]
@property
def roles(self) -> list[Role]:
"""list[:class:`Role`] Gets all roles in the server in decending order"""
return list(self._roles.values())
@property
def members(self) -> list[Member]:
"""list[:class:`Member`] Gets all members in the server"""
@@ -153,11 +155,6 @@ class Server:
"""list[:class:`Member`] Gets all channels in the server"""
return list(self._channels.values())
@property
def categories(self) -> list[Category]:
"""list[:class:`Category`] Gets all categories in the server"""
return list(self._categories.values())
def get_role(self, role_id: str) -> Role:
"""Gets a role from the cache
@@ -203,21 +200,6 @@ class Server:
"""
return self._channels[channel_id]
def get_category(self, category_id: str) -> Category:
"""Gets a category from the cache
Parameters
-----------
id: :class:`str`
The id of the category
Returns
--------
:class:`Category`
The category
"""
return self._categories[category_id]
@property
def owner(self) -> Member:
""":class:`Member` The owner of the server"""
@@ -236,123 +218,3 @@ class Server:
channel_value = (channel_permissions or self.default_channel_permissions).value
await self.state.http.set_default_permissions(self.id, server_value, channel_value)
async def leave_server(self):
"""Leaves or deletes the server"""
await self.state.http.delete_leave_server(self.id)
async def delete_server(self):
"""Leaves or deletes a server, alias to :meth`Server.leave_server`"""
await self.leave_server()
async def create_text_channel(self, *, name: str, description: Optional[str] = None) -> TextChannel:
"""Creates a text channel in the server
Parameters
-----------
name: :class:`str`
The name of the channel
description: Optional[:class:`str`]
The channel's description
Returns
--------
:class:`TextChannel`
The text channel that was just created
"""
payload = await self.state.http.create_channel(self.id, "Text", name, description)
channel = TextChannel(payload, self.state)
self._channels[channel.id] = channel
return channel
async def create_voice_channel(self, *, name: str, description: Optional[str] = None) -> VoiceChannel:
"""Creates a voice channel in the server
Parameters
-----------
name: :class:`str`
The name of the channel
description: Optional[:class:`str`]
The channel's description
Returns
--------
:class:`VoiceChannel`
The voice channel that was just created
"""
payload = await self.state.http.create_channel(self.id, "Voice", name, description)
channel = VoiceChannel(payload, self.state)
self._channels[channel.id] = channel
return channel
async def fetch_invites(self) -> list[Invite]:
"""Fetches all invites in the server
Returns
--------
list[:class:`Invite`]
"""
invite_payloads = await self.state.http.fetch_server_invites(self.id)
return [Invite._from_partial(payload["_id"], payload["server"], payload["creator"], payload["channel"], self.state) for payload in invite_payloads]
async def fetch_member(self, member_id: str) -> Member:
"""Fetches a member from this server
Parameters
-----------
member_id: :class:`str`
The id of the member you are fetching
Returns
--------
:class:`Member`
The member with the matching id
"""
payload = await self.state.http.fetch_member(self.id, member_id)
return Member(payload, self, self.state)
async def fetch_bans(self) -> list[ServerBan]:
"""Fetches all invites in the server
Returns
--------
list[:class:`Invite`]
"""
payload = await self.state.http.fetch_bans(self.id)
return [ServerBan(ban, self.state) for ban in payload["bans"]]
async def create_role(self, name: str) -> Role:
"""Creates a role in the server
Parameters
-----------
name: :class:`str`
The name of the role
Returns
--------
:class:`Role`
The role that was just created
"""
payload = await self.state.http.create_role(self.id, name)
return Role(payload, name, self, self.state)
class ServerBan:
def __init__(self, ban: Ban, state: State):
self.reason = ban.get("reason")
self.server = state.get_server(ban["_id"]["server"])
self.user = state.get_user(ban["_id"]["user"])
self.state = state
async def unban(self):
"""Unbans the user"""
await self.state.http.unban_member(self.server.id, self.user.id)
Executable
+36
View File
@@ -0,0 +1,36 @@
import time
import random
import bitarray
class SecureRTP:
def __init__(self, salt: str, hash: str):
self.salt = salt
self.hash = hash
self.roc = 0
self.seq = 0
self.packet_counter = 0
self.ssrc = random.randbytes(4) # 32 bytes
self.version = [1, 0] # binary for 2
# documentation of header can be found at https://datatracker.ietf.org/doc/html/rfc3550#section-5.1
def create_packet(self, data: bytes) -> bytes:
arr = bitarray.bitarray()
arr.extend(self.version)
arr.extend([0, 0])
arr.extend([0, 0, 0, 0])
arr.append(0)
arr.extend([0, 0, 0, 0, 0, 0, 0])
seq = list(map(int, bin(self.seq)[2:]))
arr.extend(([0] * (16 - len(seq))) + seq)
timestamp = list(map(int, bin(int(time.monotonic()))[2:]))
arr.extend(([0] * (32 - len(timestamp))) + timestamp)
arr.extend(self.ssrc)
arr.extend(data)
return arr.tobytes()
+4 -3
View File
@@ -1,7 +1,7 @@
from __future__ import annotations
from collections import deque
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Callable, Optional
from .channel import Channel, channel_factory
from .member import Member
@@ -22,12 +22,13 @@ if TYPE_CHECKING:
__all__ = ("State",)
class State:
__slots__ = ("http", "api_info", "max_messages", "users", "channels", "servers", "messages")
__slots__ = ("http", "api_info", "max_messages", "users", "channels", "servers", "messages", "dispatch")
def __init__(self, http: HttpClient, api_info: ApiInfo, max_messages: int):
def __init__(self, http: HttpClient, api_info: ApiInfo, max_messages: int, dispatch: Callable[..., None]):
self.http = http
self.api_info = api_info
self.max_messages = max_messages
self.dispatch = dispatch
self.users: dict[str, User] = {}
self.channels: dict[str, Channel] = {}
+1
View File
@@ -10,3 +10,4 @@ from .message import *
from .role import *
from .server import *
from .user import *
from .voice import *
+4 -4
View File
@@ -10,7 +10,7 @@ if TYPE_CHECKING:
__all__ = (
"SavedMessages",
"DMChannel",
"GroupDMChannel",
"Group",
"TextChannel",
"VoiceChannel",
"Channel",
@@ -37,7 +37,7 @@ class _GroupOptional(TypedDict):
permissions: int
description: str
class GroupDMChannel(_NonceChannel, _GroupOptional, BaseChannel):
class Group(_NonceChannel, _GroupOptional, BaseChannel):
recipients: list[str]
name: str
owner: str
@@ -47,12 +47,12 @@ class _TextChannelOptional(TypedDict, total=False):
icon: File
default_permissions: int
role_permissions: dict[str, int]
last_message: str
class TextChannel(_NonceChannel, _TextChannelOptional, BaseChannel):
server: str
name: str
description: str
last_message: str
channel_type: Literal["TextChannel"]
class _VoiceChannelOptional(TypedDict, total=False):
@@ -66,4 +66,4 @@ class VoiceChannel(_NonceChannel, _TextChannelOptional, BaseChannel):
description: str
channel_type: Literal["VoiceChannel"]
Channel = Union[SavedMessages, DMChannel, GroupDMChannel, TextChannel, VoiceChannel]
Channel = Union[SavedMessages, DMChannel, Group, TextChannel, VoiceChannel]
+4 -80
View File
@@ -1,84 +1,8 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Literal, TypedDict, Union
from typing_extensions import NotRequired
from typing import TYPE_CHECKING, TypedDict
if TYPE_CHECKING:
from .file import File
__all__ = ("Embed", "SendableEmbed", "WebsiteEmbed", "ImageEmbed", "TextEmbed", "NoneEmbed", "YoutubeSpecial", "TwitchSpecial", "SpotifySpecial", "SoundcloudSpecial", "BandcampSpecial", "WebsiteSpecial", "JanuaryImage", "JanuaryVideo")
class YoutubeSpecial(TypedDict):
type: Literal["Youtube"]
id: str
timestamp: NotRequired[str]
class TwitchSpecial(TypedDict):
type: Literal["Twitch"]
content_type: Literal["Channel", "Video", "Clip"]
id: str
class SpotifySpecial(TypedDict):
type: Literal["Spotify"]
content_type: str
id: str
class SoundcloudSpecial(TypedDict):
type: Literal["Soundcloud"]
class BandcampSpecial(TypedDict):
type: Literal["Bandcamp"]
content_type: Literal["Album", "Track"]
id: str
WebsiteSpecial = Union[YoutubeSpecial, TwitchSpecial, SpotifySpecial, SoundcloudSpecial, BandcampSpecial]
class JanuaryImage(TypedDict):
url: str
width: int
height: int
size: Literal["Large", "Preview"]
class JanuaryVideo(TypedDict):
url: str
width: int
height: int
class WebsiteEmbed(TypedDict):
type: Literal["Website"]
url: NotRequired[str]
special: NotRequired[WebsiteSpecial]
title: NotRequired[str]
description: NotRequired[str]
image: NotRequired[JanuaryImage]
video: NotRequired[JanuaryVideo]
site_name: NotRequired[str]
icon_url: NotRequired[str]
colour: NotRequired[str]
class ImageEmbed(JanuaryImage):
type: Literal["Image"]
class TextEmbed(TypedDict):
type: Literal["Text"]
icon_url: NotRequired[str]
url: NotRequired[str]
title: NotRequired[str]
description: NotRequired[str]
media: NotRequired[File]
colour: NotRequired[str]
class NoneEmbed(TypedDict):
type: Literal["None"]
Embed = Union[WebsiteEmbed, ImageEmbed, TextEmbed, NoneEmbed]
class SendableEmbed(TypedDict):
type: Literal["Text"]
icon_url: NotRequired[str]
url: NotRequired[str]
title: NotRequired[str]
description: NotRequired[str]
media: NotRequired[str]
colour: NotRequired[str]
__all__ = ("Embed",)
class Embed(TypedDict):
pass # TODO
+3 -3
View File
@@ -2,8 +2,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Literal, TypedDict, Union
from .channel import (Channel, DMChannel, GroupDMChannel, SavedMessages,
TextChannel, VoiceChannel)
from .channel import (Channel, DMChannel, Group, SavedMessages, TextChannel,
VoiceChannel)
from .message import Message
if TYPE_CHECKING:
@@ -69,7 +69,7 @@ class MessageDeleteEventPayload(BasePayload):
class ChannelCreateEventPayload_SavedMessages(BasePayload, SavedMessages):
pass
class ChannelCreateEventPayload_Group(BasePayload, GroupDMChannel):
class ChannelCreateEventPayload_Group(BasePayload, Group):
pass
class ChannelCreateEventPayload_TextChannel(BasePayload, TextChannel):
+5 -1
View File
@@ -7,13 +7,14 @@ if TYPE_CHECKING:
from .message import Message
from .user import User
__all__ = (
"VosoFeature",
"ApiInfo",
"Autumn",
"GetServerMembers",
"MessageWithUserData",
"JoinCallResponse"
)
@@ -50,3 +51,6 @@ class MessageWithUserData(TypedDict):
messages: list[Message]
members: list[Member]
users: list[User]
class JoinCallResponse(TypedDict):
token: str
+15 -23
View File
@@ -1,30 +1,22 @@
from __future__ import annotations
from typing import Literal, TypedDict, Union
from typing import TYPE_CHECKING, Literal, TypedDict
__all__ = (
"GroupInvite",
"ServerInvite",
"Invite",
)
from typing_extensions import NotRequired
class GroupInvite(TypedDict):
type: Literal["Group"]
_id: str
creator: str
channel: str
if TYPE_CHECKING:
from .file import File
__all__ = ("Invite", "PartialInvite")
class Invite(TypedDict):
class ServerInvite(TypedDict):
type: Literal["Server"]
server_id: str
server_name: str
server_icon: NotRequired[str]
server_banner: NotRequired[str]
channel_id: str
channel_name: str
channel_description: NotRequired[str]
user_name: str
user_avatar: NotRequired[File]
member_count: int
class PartialInvite(TypedDict):
_id: str
server: str
channel: str
creator: str
channel: str
Invite = Union[ServerInvite, GroupInvite]
+6 -1
View File
@@ -9,6 +9,11 @@ __all__ = (
Permission = tuple[int, int]
class Role(TypedDict):
class _RoleOptional(TypedDict, total=False):
colour: str
hoist: bool
rank: int
class Role(_RoleOptional):
name: str
permissions: Permission
+2 -6
View File
@@ -27,7 +27,7 @@ class _ServerOptional(TypedDict, total=False):
description: str
categories: list[Category]
system_messages: SystemMessagesConfig
roles: dict[str, Role]
roles: list[Role]
icon: File
banner: File
nsfw: bool
@@ -46,15 +46,11 @@ class BannedUser(_OptionalBannedUser):
_id: str
username: str
class BanId(TypedDict):
server: str
user: str
class _OptionalBan(TypedDict, total=False):
reason: str
class Ban(_OptionalBan):
_id: BanId
_id: str
class ServerBans(TypedDict):
users: list[BannedUser]
+192
View File
@@ -0,0 +1,192 @@
from typing import Optional, TypedDict, Union, Literal
from typing_extensions import NotRequired
TransportProtocol = Literal["TCP", "UDP"]
class BaseWS(TypedDict):
id: NotRequired[int]
class AuthenticationCommandData(TypedDict):
token: str
roomId: str
class AuthenticateCommand(BaseWS):
type: Literal["Authenticate"]
data: AuthenticationCommandData
class RTPCodecFeedback(TypedDict):
parameter: str
type: str
class RTPCodec(TypedDict):
channels: int
clockRate: int
kind: str
mimeType: str
parameters: dict
preferredPayloadType: int
rtcpFeedback: list[RTPCodecFeedback]
class RTPHeaderExtension(TypedDict):
direction: str
kind: str
preferredEncrypt: bool
preferredId: int
uri: str
class RTPCapabilities(TypedDict):
codecs: list[RTPCodec]
headerExtensions: list[RTPHeaderExtension]
class AuthenticationResponseData(TypedDict):
roomId: str
userId: str
version: str
rtpCapabilities: RTPCapabilities
class AuthenticateResponse(BaseWS):
type: Literal["Authenticate"]
data: AuthenticationResponseData
class RoomInfoCommand(BaseWS):
type: Literal["RoomInfo"]
class VoiceUser(TypedDict):
audio: bool
class RoomInfoResponseData(TypedDict):
id: str
users: dict[str, VoiceUser]
videoAllowed: bool
class RoomInfoResponse(BaseWS):
type: Literal["RoomInfo"]
data: RoomInfoResponseData
class InitializeTransportCommandData(TypedDict):
mode: Literal["SplitWebRTC", "CombinedWebRTC", "CombinedRTP"]
rtpCapabilities: RTPCapabilities
class InitializeTransportCommand(BaseWS):
type: Literal["InitializeTransports"]
data: InitializeTransportCommandData
class IceParameters(TypedDict):
usernameFragment: str
password: str
iceLite: NotRequired[bool]
class IceCandidate(TypedDict):
foundation: str
priority: str
ip: str
protocol: TransportProtocol
tcp_type: NotRequired[Literal["passive"]]
class DTLSFingerprints(TypedDict):
algorithm: Literal["Sha1", "Sha224", "Sha256", "Sha384", "Sha512"]
value: str
class DtlsParameters(TypedDict):
role: Literal["auto", "client", "server"]
fingerprints: list[DTLSFingerprints]
class SctpParameters(TypedDict):
port: int
OS: int
MIS: int
max_message_size: int
class TransportInfo(TypedDict):
id: str
iceParameters: IceParameters
iceCandidates: list[IceCandidate]
dtlsParameters: DtlsParameters
sctpParameters: Optional[SctpParameters]
class InitializeTransportResponseDataSplitWebRTC(TypedDict):
recvTransport: TransportInfo
sendTransport: TransportInfo
class InitializeTransportResponseDataCombinedWebRTC(TypedDict):
transport: TransportInfo
class InitializeTransportResponseDataCombinedRtp(TypedDict):
ip: bytes
port: int
protocol: TransportProtocol
id: str
srtpCryptoSuite: Literal["AES_CM_128_HMAC_SHA1_80", "AES_CM_128_HMAC_SHA1_32"]
InitializeTransportResponseData = Union[InitializeTransportResponseDataSplitWebRTC, InitializeTransportResponseDataCombinedWebRTC, InitializeTransportResponseDataCombinedRtp]
class InitializeTransportResponse(BaseWS):
type: Literal["InitializeTransports"]
data: InitializeTransportResponseData
class SrtpParameters(TypedDict):
cryptoSuite: Literal["AES_CM_128_HMAC_SHA1_80", "AES_CM_128_HMAC_SHA1_32"]
keyBase64: str
class ConnectTransportData(TypedDict):
id: str
dtlsParameters: NotRequired[DtlsParameters]
srtpParameters: NotRequired[SrtpParameters]
class ConnectTransportCommand(BaseWS):
type: Literal["ConnectTransport"]
data: ConnectTransportData
class ConnectTransportResponse(BaseWS):
type: Literal["ConnectTransport"]
class RTPEncodingRtx(TypedDict):
ssrc: int
class RTPEncoding(TypedDict, total=False):
ssrc: int
rid: str
codec_payload_type: int
rtx: RTPEncodingRtx
dtx: bool
scalability_mode: str
scale_resolution_down_by: float
max_bitrate: int
class RTPHeaderExtensionParameters(TypedDict):
uri: str
id: int
encrypt: bool
class RtcpParameters(TypedDict):
cname: NotRequired[str]
reduceSize: bool
mux: NotRequired[bool]
class RTPParameters(TypedDict):
mid: NotRequired[str]
codecs: list[RTPCodec]
headerExtensions: list[RTPHeaderExtensionParameters]
encodings: list[RTPEncoding]
rtcp: RtcpParameters
class StartProduceCommandData(TypedDict):
type: Literal["audio", "video", "saudio", "svideo"]
rtpParameters: RTPParameters
class StartProduceCommand(BaseWS):
type: Literal["StartProduce"]
data: StartProduceCommandData
class StartProduceResponseData(TypedDict):
producerId: str
class StartProduceResponse(BaseWS):
type: Literal["StartProduce"]
data: StartProduceResponseData
WSCommand = Union[AuthenticateCommand, RoomInfoCommand, InitializeTransportCommand, ConnectTransportCommand, StartProduceCommand]
WSResponse = Union[AuthenticateResponse, RoomInfoResponse, InitializeTransportResponse, ConnectTransportResponse, StartProduceResponse]
__all__ = ("BaseWS", "WSCommand", "WSResponse", "AuthenticateCommand", "RoomInfoCommand", "InitializeTransportCommand", "ConnectTransportCommand", "StartProduceCommand", "AuthenticateResponse", "RoomInfoResponse", "InitializeTransportResponse", "ConnectTransportResponse", "StartProduceResponse")
+2 -45
View File
@@ -3,10 +3,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING, NamedTuple, Optional, Union
from .asset import Asset, PartialAsset
from .channel import DMChannel
from .enums import PresenceType, RelationshipType
from .flags import UserBadges
from .messageable import Messageable
if TYPE_CHECKING:
from .state import State
@@ -32,7 +30,7 @@ class UserProfile(NamedTuple):
content: Optional[str]
background: Optional[Asset]
class User(Messageable):
class User:
"""Represents a user
Attributes
@@ -55,17 +53,14 @@ class User(Messageable):
The relationship between the user and the bot
status: Optional[:class:`Status`]
The users status
dm_channel: Optional[:class:`DMChannel`]
The dm channel between the client and the user, this will only be set if the client has dm'ed the user or :meth:`User.open_dm` was run
"""
__flattern_attributes__ = ("id", "bot", "owner_id", "badges", "online", "flags", "relations", "relationship", "status", "masquerade_avatar", "masquerade_name", "original_name", "original_avatar", "profile", "dm_channel")
__flattern_attributes__ = ("id", "bot", "owner_id", "badges", "online", "flags", "relations", "relationship", "status", "masquerade_avatar", "masquerade_name", "original_name", "original_avatar", "profile")
__slots__ = (*__flattern_attributes__, "state")
def __init__(self, data: UserPayload, state: State):
self.state = state
self.id = data["_id"]
self.original_name = data["username"]
self.dm_channel = None
bot = data.get("bot")
if bot:
@@ -105,13 +100,6 @@ class User(Messageable):
self.masquerade_avatar: Optional[PartialAsset] = None
self.masquerade_name: Optional[str] = None
async def _get_channel_id(self):
if not self.dm_channel:
payload = await self.state.http.open_dm(self.id)
self.dm_channel = DMChannel(payload, self.state)
return self.id
@property
def owner(self) -> Optional[User]:
owner_id = self.owner_id
@@ -147,34 +135,3 @@ class User(Messageable):
if online:
self.online = online
async def default_avatar(self) -> bytes:
"""Returns the default avatar for this user
Returns
--------
:class:`bytes`
The bytes of the image
"""
return await self.state.http.fetch_default_avatar(self.id)
async def fetch_profile(self) -> UserProfile:
"""Fetches the user's profile
Returns
--------
:class:`UserProfile`
The user's profile
"""
if profile := self.profile:
return profile
payload = await self.state.http.fetch_profile(self.id)
if file := payload.get("background"):
background = Asset(file, self.state)
else:
background = None
self.profile = UserProfile(payload.get("content"), background)
return self.profile
+3 -45
View File
@@ -1,10 +1,9 @@
import inspect
from operator import attrgetter
from typing import Any, Callable, Coroutine, Iterable, TypeVar, Union
from typing import Any, Callable, Coroutine, TypeVar, Union
from typing_extensions import ParamSpec
__all__ = ("Missing", "copy_doc", "maybe_coroutine", "get")
__all__ = ("Missing", "copy_doc", "maybe_coroutine")
class _Missing:
def __repr__(self):
@@ -18,7 +17,7 @@ def copy_doc(from_t: T) -> Callable[[T], T]:
def inner(to_t: T) -> T:
to_t.__doc__ = from_t.__doc__
return to_t
return inner
R_T = TypeVar("R_T")
@@ -35,44 +34,3 @@ async def maybe_coroutine(func: Callable[P, Union[R_T, Coroutine[Any, Any, R_T]]
value = await value
return value # type: ignore
def get(iterable: Iterable[T], **attrs: Any) -> T:
"""A convenience function to help get a value from an iterable with a specific attribute
Examples
---------
.. code-block:: python
:emphasize-lines: 3
from revolt import utils
channel = utils.get(server.channels, name="General")
await channel.send("Hello general chat.")
Parameters
-----------
iterable: Iterable
The values to search though
**attrs: Any
The attributes to check
Returns
--------
Any
The value from the iterable with the met attributes
Raises
-------
LookupError
Raises when none of the values in the iterable matches the attributes
"""
converted = [(attrgetter(attr.replace('__', '.')), value) for attr, value in attrs.items()]
for elem in iterable:
if all(pred(elem) == value for pred, value in converted):
return elem
raise LookupError
+204
View File
@@ -0,0 +1,204 @@
from __future__ import annotations
import json
import logging
import asyncio
import aiohttp
import random
import string
import nacl
import nacl.public
import nacl.encoding
import nacl.utils
import base64
from typing import TYPE_CHECKING, Literal, BinaryIO
from revolt.types.voice import AuthenticationResponseData, InitializeTransportResponseDataCombinedRtp, RTPCapabilities, RTPHeaderExtensionParameters, StartProduceResponseData
if TYPE_CHECKING:
from .state import State
from .types.voice import *
__all__ = ("VoiceClient",)
logger = logging.getLogger("revolt.voice_client")
class VoiceClient:
__slots__ = ("session", "token", "websocket", "dispatch", "state", "ws_url", "channel_id", "loop", "id", "futures", "capabilities", "rtp_info", "ssrc", "cname", "producer_id", "key", "salt")
def __init__(self, session: aiohttp.ClientSession, ws_url: str, channel_id: str, token: str, state: State):
self.id = 0
self.session = session
self.ws_url = ws_url
self.channel_id = channel_id
self.websocket: aiohttp.ClientWebSocketResponse
self.token = token
self.dispatch = state.dispatch
self.state = state
self.loop = asyncio.get_running_loop()
self.futures = dict[int, asyncio.Future]()
self.ssrc = random.randint(0, 1 << 31)
self.cname = "".join(random.choices(string.ascii_letters, k=15))
self.capabilities: RTPCapabilities
self.rtp_info: InitializeTransportResponseDataCombinedRtp
self.producer_id: str
self.key = nacl.utils.random(16)
self.salt = nacl.utils.random(14)
async def send_payload(self, payload) -> int:
self.id += 1
payload["id"] = self.id
logging.info(payload)
await self.websocket.send_str(json.dumps(payload))
return self.id
async def heartbeat(self):
while not self.websocket.closed:
# logger.info("Sending voice hearbeat")
await self.websocket.ping()
await asyncio.sleep(15)
async def send_authenticate(self):
payload: AuthenticateCommand = {
"type": "Authenticate",
"data": {
"token": self.token,
"roomId": self.channel_id
}
}
id = await self.send_payload(payload)
response: AuthenticationResponseData = await self.set_future(id)
self.capabilities = response["rtpCapabilities"]
async def send_initialize_transport(self):
payload: InitializeTransportCommand = {
"type": "InitializeTransports",
"data": {
"mode": "CombinedRTP",
"rtpCapabilities": self.capabilities
}
}
id = await self.send_payload(payload)
response: InitializeTransportResponseDataCombinedRtp = await self.set_future(id)
self.rtp_info = response
async def send_connect_transport(self):
payload: ConnectTransportCommand = {
"type": "ConnectTransport",
"data": {
"srtpParameters": {
"cryptoSuite": self.rtp_info["srtpCryptoSuite"],
"keyBase64": base64.b64encode(self.salt + self.key).decode(),
},
"id": self.rtp_info["id"],
}
}
await self.send_payload(payload)
async def send_start_produce(self, type: Literal["audio", "video", "saudio", "svideo"]):
header_extensions: list[RTPHeaderExtensionParameters] = [{"uri": d["uri"], "id": d["preferredId"], "encrypt": d["preferredEncrypt"]} for d in self.capabilities["headerExtensions"]]
payload: StartProduceCommand = {
"type": "StartProduce",
"data": {
"type": type,
"rtpParameters": {
"codecs": self.capabilities["codecs"],
"headerExtensions": header_extensions,
"encodings": [{
"ssrc": self.ssrc,
}],
"mid": "0",
"rtcp": {
"cname": self.cname,
"reduceSize": True
}
}
}
}
id = await self.send_payload(payload)
response: StartProduceResponseData = await self.set_future(id)
producer_id = response["producerId"]
self.producer_id = producer_id
async def handle_event(self, payload: WSResponse):
event_type = payload["type"].lower()
logger.debug("Recieved event %s %s", event_type, payload)
try:
func = getattr(self, f"handle_{event_type}")
except:
logger.debug("Unknown event '%s'", event_type)
return
await func(payload)
async def handle_authenticate(self, _):
logger.info("Successfully authenticated")
async def set_future(self, id: int):
future = asyncio.Future()
self.futures[id] = future
return await future
async def websocket_loop(self):
async for msg in self.websocket:
payload: WSResponse = json.loads(msg.data)
print(payload)
if future := self.futures.get(payload.get("id", "")):
future.set_result(payload.get("data"))
self.loop.create_task(self.handle_event(payload))
async def start(self):
self.websocket = await self.session.ws_connect(self.ws_url)
asyncio.create_task(self.heartbeat())
asyncio.create_task(self.websocket_loop())
async def send_audio(self, audio: BinaryIO):
await self.send_start_produce("audio")
"""
{
'type': 'StartProduce',
'data': {
'type': 'audio',
'rtpParameters': {
'codecs': [
{'kind': 'audio', 'mimeType': 'audio/opus', 'preferredPayloadType': 100, 'clockRate': 48000, 'channels': 2, 'parameters': {}, 'rtcpFeedback': [{'type': 'transport-cc', 'parameter': ''}]}
],
'headerExtensions': [{'uri': 'urn:ietf:params:rtp-hdrext:sdes:mid', 'id': 1, 'encrypt': False}, {'uri': 'urn:ietf:params:rtp-hdrext:sdes:mid', 'id': 1, 'encrypt': False}, {'uri': 'urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id', 'id': 2, 'encrypt': False}, {'uri': 'urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id', 'id': 3, 'encrypt': False}, {'uri': 'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time', 'id': 4, 'encrypt': False}, {'uri': 'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time', 'id': 4, 'encrypt': False}, {'uri': 'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01', 'id': 5, 'encrypt': False}, {'uri': 'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01', 'id': 5, 'encrypt': False}, {'uri': 'http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07', 'id': 6, 'encrypt': False}, {'uri': 'urn:ietf:params:rtp-hdrext:framemarking', 'id': 7, 'encrypt': False}, {'uri': 'urn:ietf:params:rtp-hdrext:ssrc-audio-level', 'id': 10, 'encrypt': False}, {'uri': 'urn:3gpp:video-orientation', 'id': 11, 'encrypt': False}, {'uri': 'urn:ietf:params:rtp-hdrext:toffset', 'id': 12, 'encrypt': False}],
'encodings': [{'ssrc': 896646738}],
'mid': '0',
'rtcp': {'cname': 'iUaoUqyQjShruzl', 'reduceSize': True}}
},
'id': 4}
"""
"""
{
"type": "StartProduce",
"data": {
"type":"audio",
"rtpParameters": {
"codecs": [
{"mimeType":"audio/opus","payloadType":111,"clockRate":48000,"channels":2,"parameters":{"minptime":10,"useinbandfec":1},"rtcpFeedback":[{"type":"transport-cc","parameter":""}]}
],
"headerExtensions":[{"uri":"urn:ietf:params:rtp-hdrext:sdes:mid","id":4,"encrypt":false,"parameters":{}},{"uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","id":2,"encrypt":false,"parameters":{}},{"uri":"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01","id":3,"encrypt":false,"parameters":{}},{"uri":"urn:ietf:params:rtp-hdrext:ssrc-audio-level","id":1,"encrypt":false,"parameters":{}}],
"encodings":[{"ssrc":2276570677,"dtx":false}],
"mid":"0"}
"rtcp":{"cname":"KAhFB58ELM5Tf+9a","reducedSize":true},
}
}
"id":7,
"""
+11 -17
View File
@@ -6,6 +6,9 @@ from copy import copy
from typing import TYPE_CHECKING, Callable, cast
from .enums import RelationshipType
from .types import (ChannelCreateEventPayload, ChannelDeleteEventPayload,
ChannelDeleteTypingEventPayload,
ChannelStartTypingEventPayload, ChannelUpdateEventPayload)
from .types import Message as MessagePayload
from .types import (MessageDeleteEventPayload, MessageUpdateEventPayload,
ServerDeleteEventPayload, ServerMemberJoinEventPayload,
@@ -13,11 +16,8 @@ from .types import (MessageDeleteEventPayload, MessageUpdateEventPayload,
ServerMemberUpdateEventPayload,
ServerRoleDeleteEventPayload, ServerRoleUpdateEventPayload,
ServerUpdateEventPayload, UserRelationshipEventPayload,
UserUpdateEventPayload, ChannelCreateEventPayload, ChannelDeleteEventPayload,
ChannelDeleteTypingEventPayload,
ChannelStartTypingEventPayload, ChannelUpdateEventPayload)
from .user import Status, UserProfile
from .channel import TextChannel, GroupDMChannel, VoiceChannel
UserUpdateEventPayload)
from .user import Status
try:
import ujson as json
@@ -163,12 +163,10 @@ class WebsocketHandler:
if clear := payload.get("clear"):
if clear == "Icon":
if isinstance(channel, (TextChannel, VoiceChannel, GroupDMChannel)):
channel.icon = None
pass # TODO
elif clear == "Description":
if isinstance(channel, (TextChannel, VoiceChannel, GroupDMChannel)):
channel.description = None
channel.description = None # type: ignore
self.dispatch("channel_update", old_channel, channel)
@@ -265,13 +263,9 @@ class WebsocketHandler:
if clear := payload.get("clear"):
if clear == "ProfileContent":
if profile := user.profile:
user.profile = UserProfile(None, profile.background)
...
elif clear == "ProfileBackground":
if profile := user.profile:
user.profile = UserProfile(profile.content, None)
...
elif clear == "StatusText":
# user.status will never be none because they are trying to remove the text
if user.status.presence is None: # type: ignore
@@ -285,8 +279,8 @@ class WebsocketHandler:
# the keys have . in them so i need to replace with _
data = payload["data"]
data["profile_content"] = data.pop("profile.content", None)
data["profile_background"] = data.pop("profile.background", None)
data["profile_content"] = data.get("profile.content", None)
data["profile_background"] = data.get("profile.background", None)
user._update(**data)
+11 -12
View File
@@ -5,16 +5,20 @@ from setuptools import find_packages, setup
here = pathlib.Path(__file__).parent.resolve()
long_description = (here / "README.md").read_text(encoding="utf-8")
requirements = (here / "requirements.txt").read_text(encoding="utf-8").splitlines()
requirements = []
with open('requirements.txt') as f:
requirements = f.read().splitlines()
setup(
name="revolt.py",
version="0.1.6",
description="Python wrapper for the revolt.chat API",
version="0.1.1",
description="Python wrapper around revolt.chat",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/revoltchat/revolt.py",
url="https://github.com/Zomatree/revolt.py",
author="Zomatree",
classifiers=[
"Development Status :: 4 - Beta",
@@ -25,7 +29,6 @@ setup(
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3 :: Only",
],
@@ -33,15 +36,11 @@ setup(
packages=find_packages(),
python_requires=">=3.9",
extras_require={
"speedups": [
"ujson",
"aiohttp[speedups]==3.7.4.post0",
"msgpack==1.0.2"
],
"speedups": ["ujson", "aiohttp[speedups]==3.7.4.post0", "msgpack==1.0.2"],
},
install_requires=requirements,
project_urls={
"Bug Reports": "https://github.com/revoltchat/revolt.py/issues",
"Source": "https://github.com/revoltchat/revolt.py/",
"Bug Reports": "https://github.com/Zomatree/revolt.py/issues",
"Source": "https://github.com/Zomatree/revolt.py/",
},
)