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

Commit

Permalink
many updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeldeee committed Aug 30, 2023
1 parent c3974a8 commit 4962a75
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
48 changes: 46 additions & 2 deletions pytecord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,53 +198,93 @@ def __init__(self, data: dict[str, Any], token: str):

@property
def owner(self) -> User:
"""
This property returns the user object of the guild owner.
"""
data = rget(f'/users/{self.__owner_id}', self.__token).json()
return User(data, self.__token)

@property
def channels(self) -> 'list[GuildChannel]':
"""
This property returns a list of all guild channels.
"""
data = rget(f'/guilds/{self.id}/channels', self.__token).json()
return get_list_of_types(GuildChannel, data, self.__token)

@property
def emojis(self) -> list[Emoji]:
"""
This property returns a list of all emojis in the guild.
"""
data = rget(f'/guilds/{self.id}/emojis', self.__token).json()
return get_list_of_types(Emoji, data, self.__token)

@property
def roles(self) -> list[Role]:
"""
This property returns a list of all roles in the guild.
"""
data = rget(f'/guilds/{self.id}/roles', self.__token).json()
return get_list_of_types(Role, data)

@property
def stickers(self) -> list[Sticker]:
"""
This property returns a list of all stickers in the guild.
"""
data = rget(f'/guilds/{self.id}/stickers', self.__token).json()
return get_list_of_types(Sticker, data, self.__token)

@property
def welcome_screen(self) -> WelcomeScreen | None:
"""
This property returns the welcome screen configuration for the guild, if available.
"""
try:
data = rget(f'/guilds/{self.id}/welcome-screen', self.__token)
except:
return
return WelcomeScreen(data, self.__token)

@property
def preview(self) -> GuildPreview:
"""
This property returns a preview object for the guild, providing some basic information.
"""
data = rget(f'/guilds/{self.id}/preview', self.__token).json()
return GuildPreview(data, self.__token)

@property
def onboarding(self) -> GuildOnboarding:
"""
This property returns the onboarding configuration for the guild, if available.
"""
data = rget(f'/guilds/{self.id}/onboarding', self.__token).json()
return GuildOnboarding(data, self.__token)

@property
def widget(self) -> Widget | None:
"""
This property returns the widget configuration for the guild, if available.
"""
try:
data = rget(f'/guilds/{self.id}/widget.json', self.__token).json()
except:
return
return Widget(data, self.__token)

@property
def member(self) -> GuildMember | None:
"""
This property returns the guild member object for bot, if bot is in guild.
"""
try:
data = rget(f'/users/@me/guilds/{self.id}/member', self.__token)
except:
return
return GuildMember(data, self.__token)


def __int__(self) -> int:
return self.id
Expand All @@ -257,9 +297,13 @@ def eval(self) -> dict[str, Any]:

def search(self, query: str, limit: int = 1) -> list[GuildMember] | GuildMember | None:
"""
Returns a list of guild member objects whose username or nickname starts with a provided string
The search function is a method that allows you to search for guild members on a Discord server. It takes a query string as input, which is used to search for members whose username or nickname starts with the provided string. The limit parameter limits the maximum number of search results (by default, it's set to 1).
The function sends a request to the Discord API, using the provided authentication token, to perform the search. The search results are then processed and returned as a list of GuildMember objects. If the limit is set to 1 and only one result is found, the function returns that result as a single GuildMember object. If no results are found, it returns None.
You can find more information about the function in the Discord API documentation.
https://discord.com/developers/docs/resources/guild#search-guild-members
Link: https://discord.com/developers/docs/resources/guild#search-guild-members
"""
payload = {
'query': query,
Expand Down
33 changes: 19 additions & 14 deletions pytecord/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async def run(self, intents: int = None):

async with ClientSession(headers=self.headers) as session:
async with session.ws_connect(
f"wss://gateway.discord.gg/?v={self.gateway_version}&encoding=json"
f'wss://gateway.discord.gg/?v={self.gateway_version}&encoding=json'
) as ws:
self._ws = ws

Expand Down Expand Up @@ -220,36 +220,41 @@ async def register_app_commands(self, data: GatewayOutput):
for i in range(1, 3):
for server_command in commands_by_types[i]:
if server_command['name'] not in names[i]:
id_to_delete = server_command['id']
if self.debug:
print(f'DEBUG DELETE /applications/.../commands/{server_command["id"]}')
await adelete(f'/applications/{app_id}/commands/{server_command["id"]}', self.token)

print(f'DEBUG DELETE /applications/.../commands/{id_to_delete}')
await adelete(f'/applications/{app_id}/commands/{id_to_delete}', self.token)

async def interaction_create(data: GatewayOutput):
name = data.d['data']['name']
type = data.d['data']['type']
command_data = data.d['data']

command_type = command_data['type']
command_name = command_data['name']
command_options = command_data.get('options')

command = None
for i in self.commands[type]:
if i.name == name:
command = i
selected_command = None
for i in self.commands[command_type]:
if i.name == command_name:
selected_command = i

interaction = Interaction(data.d, self.token)

if data.d['data'].get('options'):
if command_options:
options = {}
for i in data.d['data']['options']:
for i in command_options:
options[i['name']] = i['value']

await self.commands[type][command](interaction, **options)
await self.commands[command_type][selected_command](interaction, **options)
else:
await self.commands[type][command](interaction)
await self.commands[command_type][selected_command](interaction)

self.add_event('INTERACTION_CREATE', interaction_create)

async def run(self, intents: int = 0):
await self.stream.run(intents)

# API methods

def get_guild(self, id: int) -> 'Guild':
from .guild import Guild

Expand Down

0 comments on commit 4962a75

Please sign in to comment.