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

Commit

Permalink
New message example and fix ready example
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeldeee committed Aug 31, 2023
1 parent 2f0a3db commit 2dce7b9
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 32 deletions.
9 changes: 9 additions & 0 deletions examples/events/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pytecord import Client, Message

client = Client('YOUR_TOKEN')

@client.listen()
async def message_create(message: Message): # also you can use async def message():
await message.reply(f'Hello, {message.author}!') # str(message.author) == message.author.mention

client.run()
8 changes: 4 additions & 4 deletions examples/events/ready.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytecord
from pytecord import Client

client = pytecord.Client('YOUR_TOKEN')
client = Client('YOUR_TOKEN')

@client.listen('ready')
@client.listen()
async def ready():
print("Hello, i'm bot!")

client.run()
client.run()
6 changes: 5 additions & 1 deletion pytecord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def presence(self, new: Presence) -> Presence:
def listen(self):
def decorator(func_to_decorate: Callable[..., Coroutine[Any, Any, Any]]):
event_name = func_to_decorate.__name__

if event_name == 'message':
event_name = 'message_create'

match event_name:
case 'ready':
async def func(data: 'GatewayOutput'):
Expand All @@ -68,7 +72,7 @@ async def func(data: 'GatewayOutput'):
await func_to_decorate(event)
case _:
raise ValueError('Invalid event name: %s' % event_name)

self.webhook.add_event(event_name.upper(), func)

return decorator
Expand Down
36 changes: 18 additions & 18 deletions pytecord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class WelcomeScreenChannel:
def __init__(self, data: dict[str, Any], token: str) -> None:
self.description: str = data.get('description')
self.emoji_id: int | None = get_snowflake('emoji_id')
self.emoji_id: int | None = get_snowflake(data.get('emoji_id'))
self.emoji_name: str | None = data.get('emoji_name')

self.__channel_id = int(data.get('channel_id'))
Expand Down Expand Up @@ -171,10 +171,10 @@ def __init__(self, data: dict[str, Any], token: str):
self.explicit_content_filter: ExplicitContentFilterLevel = ExplicitContentFilterLevel(data.get('explicit_content_filter'))
self.features: list[str] = data.get('features')
self.mfa_enabled: bool = data.get('mfa_level') == 1
self.application_id: int | None = get_snowflake('application_id')
self.system_channel_id: int | None = get_snowflake('system_channel_id')
self.application_id: int | None = get_snowflake(data.get('application_id'))
self.system_channel_id: int | None = get_snowflake(data.get('system_channel_id'))
self.system_channel_flags: int = data.get('system_channel_flags')
self.rules_channel_id: int | None = get_snowflake('rules_channel_id')
self.rules_channel_id: int | None = get_snowflake(data.get('rules_channel_id'))
self.max_presences: int | None = data.get('max_presences')
self.max_members: int | None = data.get('max_members')
self.vanity_url_code: str | None = data.get('vanity_url_code')
Expand All @@ -190,7 +190,7 @@ def __init__(self, data: dict[str, Any], token: str):
self.approximate_presence_count: int | None = data.get('approximate_presence_count')
self.nsfw_level: NSFWLevel = NSFWLevel(data.get('nsfw_level'))
self.premium_progress_bar_enabled: bool = data.get('premium_progress_bar_enabled')
self.safety_alerts_channel_id: int | None = get_snowflake('safety_alerts_channel_id')
self.safety_alerts_channel_id: int | None = get_snowflake(data.get('safety_alerts_channel_id'))

self.__owner_id = data.get('owner_id')
self.__token = token
Expand Down Expand Up @@ -320,7 +320,7 @@ def search(self, query: str, limit: int = 1) -> list[GuildMember] | GuildMember

class Overwrite:
def __init__(self, data: dict[str, Any]) -> None:
self.id = get_snowflake('id')
self.id = get_snowflake(data.get('id'))
self.type: Literal[0, 1] = data.get('type')
self.str_type: Literal['role', 'member'] = 'role' if self.type == 0 else 'member'
self.allow: permissions_set = data.get('allow')
Expand All @@ -332,23 +332,23 @@ def __int__(self) -> int:

class GuildChannel(Object):
def __init__(self, data: dict[str, Any], token: str):
self.id: int = get_snowflake('id')
self.id: int = get_snowflake(data.get('id'))
self.type: int = data.get('type')
self.position: int | None = data.get('position')
self.permission_overwrites: list[Overwrite] = get_list_of_types(Overwrite, data.get('permission_overwrites', []))
self.name = data.get('name')
self.topic = data.get('topic')
self.nsfw = data.get('nsfw')
self.last_message_id = get_snowflake('last_message_id')
self.last_message_id = get_snowflake(data.get('last_message_id'))
self.bitrate = data.get('bitrate')
self.user_limit = data.get('user_limit')
self.rate_limit_per_user = data.get('rate_limit_per_user')
self.recipients = get_list_of_types(User, data.get('recipients'), token)
self.icon = data.get('icon')
self.owner_id = get_snowflake('owner_id')
self.application_id = get_snowflake('application_id')
self.owner_id = get_snowflake(data.get('owner_id'))
self.application_id = get_snowflake(data.get('application_id'))
self.managed = data.get('managed')
self.parent_id = get_snowflake('parent_id')
self.parent_id = get_snowflake(data.get('parent_id'))
self.last_pin_timestamp = data.get('last_pin_timestamp')
self.rtc_region = data.get('rtc_region')
self.video_quality_mode = data.get('video_quality_mode')
Expand All @@ -367,7 +367,7 @@ def __init__(self, data: dict[str, Any], token: str):
self.default_sort_order = data.get('default_sort_order')
self.default_forum_layout = data.get('default_forum_layout')

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

Expand Down Expand Up @@ -409,7 +409,7 @@ async def send(self, content: str) -> 'Message':

class Message:
def __init__(self, data: dict[str, Any], token: str) -> None:
self.id = get_snowflake('id')
self.id = get_snowflake(data.get('id'))
self.author = User(data.get('author'), token)
self.content = data.get('content')
self.timestamp = data.get('timestamp')
Expand All @@ -424,11 +424,11 @@ def __init__(self, data: dict[str, Any], token: str) -> None:
self.reactions = data.get('reactions')
self.nonce = data.get('nonce')
self.pinned = data.get('pinned')
self.webhook_id = get_snowflake('webhook_id')
self.webhook_id = get_snowflake(data.get('webhook_id'))
self.type = data.get('type')
self.activity = data.get('activity')
self.application = data.get('application')
self.application_id = get_snowflake('application_id')
self.application_id = get_snowflake(data.get('application_id'))
self.message_reference = data.get('message_reference')
self.flags = data.get('flags')
self.referenced_message = data.get('referenced_message')
Expand All @@ -441,7 +441,7 @@ def __init__(self, data: dict[str, Any], token: str) -> None:
self.role_subscription_data = data.get('role_subscription_data')

# Extra fields for message create
self.guild_id = get_snowflake('guild_id')
self.guild_id = get_snowflake(data.get('guild_id'))
self.member = data.get('member')

self.__channel_id = data.get('channel_id')
Expand Down Expand Up @@ -470,10 +470,10 @@ async def reply(self, content: str) -> 'Message':

class MessageDeleteEvent:
def __init__(self, data: dict[str, Any], token: str) -> None:
self.id = get_snowflake('id')
self.id = get_snowflake(data.get('id'))

self.__channel_id = int(data.get('channel_id'))
self.__guild_id = get_snowflake('guild_id')
self.__guild_id = get_snowflake(data.get('guild_id'))
self.__token = token

@property
Expand Down
8 changes: 4 additions & 4 deletions pytecord/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Emoji:
def __init__(self, data: dict[str, Any], token: str) -> None:
self.id: int = get_snowflake('id')
self.id: int = get_snowflake(data.get('id'))
self.name: str | None = data.get('name')
self.roles: list[Role] | None = get_list_of_types(Role, data.get('roles'))
self.user: User = User(data.get('user'), token)
Expand All @@ -17,15 +17,15 @@ def __init__(self, data: dict[str, Any], token: str) -> None:

class Sticker:
def __init__(self, data: dict[str, Any], token: str) -> None:
self.id: int = get_snowflake('id')
self.pack_id = get_snowflake('pack_id')
self.id: int = get_snowflake(data.get('id'))
self.pack_id = get_snowflake(data.get('pack_id'))
self.name: str = data.get('name')
self.description: str | None = data.get('description')
self.tags: str = data.get('tags')
self.asset: str = data.get('asset') # deprecated
self.type: int = data.get('type')
self.format_type: int = data.get('format_type')
self.available: bool | None = data.get('available')
self.guild_id: int | None = get_snowflake('guild_id')
self.guild_id: int | None = get_snowflake(data.get('guild_id'))
self.user: User | None = User(x, token) if (x := data.get('user')) else None
self.sort_value: int | None = data.get('sort_value')
6 changes: 3 additions & 3 deletions pytecord/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

class RoleTags:
def __init__(self, data: dict[str, Any]) -> None:
self.bot_id: int | None = get_snowflake('bot_id')
self.integration_id: int | None = get_snowflake('integration_id')
self.bot_id: int | None = get_snowflake(data.get('bot_id'))
self.integration_id: int | None = get_snowflake(data.get('integration_id'))
self.premium_subscriber: bool = 'premium_subscriber' in list(data.keys())
self.subscription_listing_id: int | None = get_snowflake('subscription_listing_id')
self.subscription_listing_id: int | None = get_snowflake(data.get('subscription_listing_id'))
self.available_for_purchase: bool = 'available_for_purchase' in list(data.keys())
self.guild_connections: bool = 'guild_connections' in list(data.keys())

Expand Down
12 changes: 11 additions & 1 deletion pytecord/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class User(Object):
def __init__(self, data: dict[str, Any], token: str) -> None:
self.id: int = get_snowflake('id')
self.id: int = get_snowflake(data.get('id'))
self.username: str = data.get('username')
self.discriminator: str | None = data.get('discriminator')
self.global_name: str | None = data.get('global_name')
Expand All @@ -30,9 +30,19 @@ def __init__(self, data: dict[str, Any], token: str) -> None:
def eval(self) -> dict[str, Any]:
return self.__data

@property
def mention(self) -> str:
return f'<@{self.id}>'

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

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

def __repr__(self) -> str:
return self.mention


class GuildMember(Object):
def __init__(self, data: dict[str, Any], token: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pytecord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, *args: object) -> None:
class MessagePayload:
def __init__(self, content: str) -> None:
self.json = {
'content': content
'content': str(content)
}

def make_reply(self, message_id: int) -> None:
Expand Down

0 comments on commit 2dce7b9

Please sign in to comment.