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

Commit

Permalink
Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed Mar 13, 2023
1 parent c2dba26 commit 3189101
Show file tree
Hide file tree
Showing 22 changed files with 2,252 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/app_commands/basic_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"""

# Import client
from disspy import Client
from pytecord import Client

# Create a client
# note: You should replace 'token' to your token
client = Client(token='token')

# Import application commands module
from disspy import app
from pytecord import app

# Create a command!
# Docstring is description of command (visible in discord)
Expand Down
8 changes: 4 additions & 4 deletions examples/app_commands/context_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"""

# For start - create a client
from disspy import Client
from pytecord import Client
client = Client(token='token')

# import commands module:
from disspy import app
from pytecord import app

# Import messages:
from disspy import Message
from pytecord import Message

# note: context menus is divided for 2 types: User and Message

Expand All @@ -27,7 +27,7 @@ async def message_context_menu(ctx: app.Context, message: Message):
await ctx.send_message('Content:', message.content, '\n', 'Id:', message.id, ephemeral=True)

# Import users:
from disspy import User
from pytecord import User

# This is example user context menu
# 'user: User' is showing that command is user context menu
Expand Down
6 changes: 3 additions & 3 deletions examples/app_commands/modals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"""

# Create a client
from disspy import Client
from pytecord import Client
client = Client(token='token')

# Create a command
from disspy import app
from pytecord import app

# note: also you must import ui module
from disspy import ui
from pytecord import ui

@client.command()
async def modals(ctx: app.Context):
Expand Down
2 changes: 1 addition & 1 deletion examples/events/ready.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

# Create a client
from disspy import Client
from pytecord import Client
client = Client(token='token') # Replace with your token

# For using events you can use `event` decorator
Expand Down
4 changes: 2 additions & 2 deletions examples/get_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"""

# For get started import library
import disspy
import pytecord

# Also you can import any things using `from`
from disspy import Client
from pytecord import Client

# Let's create client!
# 1 step: Copy token from Discord Developer Portal
Expand Down
2 changes: 1 addition & 1 deletion import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Test import
"""

import disspy
import pytecord

print("Success: %s" % "import disspy")
46 changes: 46 additions & 0 deletions pytecord/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''
Pytecord is a libary for simple creating bot clients in discord API written in Python
Example client:
```
from pytecord import Client
client = Client(token='your_token')
@client.event
async def ready():
print("Hello! I'm ready!")
client()
```
This bot will print "Hello! I'm ready!" string in console when it become ready
More examples you can find in `examples` directory on this link:
### Links
GitHub repo: https://github.com/pixeldeee/pytecord
More examples: https://github.com/pixeldeee/pytecord/tree/master/examples
PyPi: https://pypi.org/project/pytecord
Docs: https://disspy.readthedocs.io/en/latest
'''

from pytecord.app import *
from pytecord.channel import *
from pytecord.client import *
from pytecord.profiles import *
from pytecord.role import *
from pytecord.ui import *
from pytecord.files import *

# Info
__version__: str = '1.0-alpha-1'
__lib_name__: str = 'pytecord'
__lib_description__: str = (
'Pytecord is a library for simple creating bot clients in discord API written in Python'
)
43 changes: 43 additions & 0 deletions pytecord/annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Protocol, Generic, TypeVar, TypeAlias

__all__ = (
'Strable',
)

T = TypeVar('T')
ST = TypeVar('ST', bound=str)

class Strable(Protocol):
'''
(protocol) Strable
'''
def __str__(self) -> str:
...

class Subclass(Generic[T]):
'''
Only subclass of given class
Example using:
```
Subclass[Class]
```
'''
... # pylint: disable=unnecessary-ellipsis

class Startswith(str, Generic[ST]):
'''
Indicates that this string startswith other string
Using:
```
Startswith['I like disspy'] # F. e., 'I like disspy very much!'
```
'''
... # pylint: disable=unnecessary-ellipsis

# Type aliases
Snowflake: TypeAlias = int # f. e., 1234567890987654321
Filename: TypeAlias = str
Url: TypeAlias = Startswith['http']
ProxyUrl: TypeAlias = Url
203 changes: 203 additions & 0 deletions pytecord/channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
from asyncio import get_event_loop

from pytecord import utils
from pytecord.payloads import MessagePayload
from pytecord.route import Route

from typing import TYPE_CHECKING


if TYPE_CHECKING:
from pytecord.annotations import Strable

__all__ = (
'Message',
'RawMessage',
'Channel',
)

class Message:
'''
Channel message object
### Magic operations
---
`str()` -> Message content
`int()` -> Message id
`in` -> Check what message contains in channel
`==` -> This message is equal with other message
`!=` -> This message is not equals with other message
`<` -> Message life time (how long the message has been sent) less that other message life time
(ID1 > ID2)
`>` -> Message life time more that other message life time (ID1 < ID2)
`<=` -> Message life time less or equals that other message life time (ID1 >= ID2)
`>=` -> Message life time more or equals that other message life time (ID1 <= ID2)
```
# message1.id = 1; message2.id = 2; message = message1
str(message) # message.content
int(message) # message.id
if message in channel: # if message.channel.id == channel.id
print('This message in this channel!')
print('Equals!' if message1 == message2 else 'Not equals!')
print('Not equals!' if message1 != message2 else 'Equals!')
print(message1 < message2) # False
print(message1 > message2) # True
print(message1 <= message2) # False
print(message1 >= message2) # True
```
'''
def __init__(self, session, **data: MessagePayload) -> None:
self._session = session

_ = data.get
self.id: int = int(_('id'))
self.channel_id: int = _('channel_id')
self.author = _('author', None) # todo: Add support for users
self.content = _('content', None)
self.timestamp: str = _('timestamp')
self.edited_timestamp: str | None = _('edited_timestamp', None)
self.tts: bool = _('tts')
self.mention_everyone: bool = _('mention_everyone')
self.mentions: list[dict] = _('mentions', []) # todo: Add support for users
self.mention_roles: list[dict] = _('mention_roles', []) # todo: Add support for roles
self.mention_channels: list[dict] = _('mention_channels', []) # todo: Add assign mention channels
self.attachments: list[dict] = _('attachments', []) # todo: Add support for attachments
self.embeds: list[dict] = _('embeds', []) # todo: Add support for embeds
self.reactions: dict | None = _('reactions', None) # todo: Add support for reactions
self.pinned: bool = _('pinned')
self.webhook_id: int | None = _('webhook_id', None)
self.type: int = _('type')
self.application_id: int | None = _('application_id', None)
self.message_reference: data | None = _('message_reference', None) # todo: Add support for message reference
self.flags: int | None = _('flags', None)
self.referenced_message: dict | None = _('referenced_message', None) # todo: Add assign referenced message
self.interaction: dict | None = _('interaction', None) # todo: Add support for interactions
self.thread: dict | None = _('thread', None) # todo: Add assign thread
self.components: list[dict] = _('components', [])
self.stickers: list[dict] = _('stickers', []) # todo: Add support for stickers
self.position: int | None = _('position', None)

@property
def channel(self) -> 'Channel':
channel_json, _ = Route(
'/channels/%s', self.channel_id,
method='GET',
token=utils.get_token_from_auth(self._session.headers)
).request()
return Channel(self._session, **channel_json)

def __str__(self) -> str:
return self.content
def __int__(self) -> int:
return self.id
def __eq__(self, __o: 'Message') -> bool: # ==
return self.id == __o.id
def __ne__(self, __o: 'Message') -> bool: # !=
return self.id != __o.id
def __lt__(self, other: 'Message') -> bool: # <
return self.id > other.id
def __gt__(self, other: 'Message') -> bool: # >
return self.id < other.id
def __le__(self, other: 'Message'): # <=
return self.id >= other.id
def __ge__(self, other: 'Message'): # >=
return self.id <= other.id

async def reply(self, *strings: 'list[Strable]', sep: str = ' '):
'''
Reply to a message
'''
payload = utils.message_payload(*strings, sep=sep)
payload['message_reference'] = {
'message_id': self.id
}

route = Route(
'/channels/%s/messages', self.channel_id,
method='POST',
payload=payload
)
j, _ = await route.async_request(self._session, get_event_loop())
return Message(self._session, **j)


class RawMessage:
def __init__(self, session, **data) -> None:
self._session = session

self.id: int = data.get('id', None)
self.channel_id: int = data.get('channel_id', None)
self.guild_id: int | None = data.get('guild_id', None)


class Channel:
'''
Channel object.
### Magic operations
---
`str()` -> Name of channel
`int()` -> Channel id
`in` -> Check what message contains in channel
`[key]` -> Fetch the message
```
str(channel)
int(channel)
if message in channel:
print('This message in this channel!')
fetched_message = channel[1076055795042615298]
```
'''
def __init__(self, session, **data) -> None:
self._session = session

_ = data.get
self.id: int = int(_('id'))
self.name: str = _('name')

def fetch(self, message_to_fetch_id: int) -> 'Message':
data, _ = Route(
'/channels/%s/messages/%s', self.id, str(message_to_fetch_id),
method='GET',
token=utils.get_token_from_auth(self._session.headers)
).request()
return Message(self._session, **data)

def __str__(self) -> str:
return self.name

def __int__(self) -> int:
return self.id

def __contains__(self, value: 'Message') -> bool:
return self.id == value.channel_id

def __getitem__(self, key: int) -> 'Message':
return self.fetch(key)

async def send(self, *strings: 'list[Strable]', sep: str = ' ', tts: bool = False) -> 'Message | None':
route = Route(
'/channels/%s/messages', self.id,
method='POST',
payload=utils.message_payload(*strings, sep=sep, tts=tts)
)
j, _ = await route.async_request(self._session, get_event_loop())
return Message(self._session, **j)
Loading

0 comments on commit 3189101

Please sign in to comment.