Skip to content

Commit

Permalink
Refactor code and improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vadim-su committed Jun 20, 2024
1 parent 2b26ff1 commit ed5c7c0
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 173 deletions.
6 changes: 3 additions & 3 deletions asyncord/client/bans/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async def ban(
"""
url = self.bans_url / str(user_id)

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -117,7 +117,7 @@ async def unban(self, user_id: SnowflakeInputType, reason: str | None = None) ->
user_id: ID of the user to unban.
reason: Reason for unbanning the user. Defaults to None.
"""
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -143,7 +143,7 @@ async def bulk_ban(
"""
url = self.guilds_url / str(self.guild_id) / 'bulk-ban'

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down
12 changes: 6 additions & 6 deletions asyncord/client/channels/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async def create_channel(
"""
url = REST_API_URL / 'guilds' / str(guild_id) / 'channels'

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -142,7 +142,7 @@ async def update(
"""
url = self.channels_url / str(channel_id)

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -189,7 +189,7 @@ async def delete(self, channel_id: SnowflakeInputType, reason: str | None = None
"""
url = self.channels_url / str(channel_id)

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -216,7 +216,7 @@ async def update_permissions(
permission_data: The data to update the permissions with.
reason: Reason for updating the permissions.
"""
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -244,7 +244,7 @@ async def delete_permission(
overwrite_id: Role or user id.
reason: Reason for deleting the permission.
"""
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -290,7 +290,7 @@ async def create_channel_invite(
"""
url = self.channels_url / str(channel_id) / 'invites'

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down
6 changes: 3 additions & 3 deletions asyncord/client/emojis/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def create_guild_emoji(
Reference:
https://discord.com/developers/docs/resources/emoji#create-guild-emoji
"""
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -101,7 +101,7 @@ async def update_guild_emoji(
Reference:
https://discord.com/developers/docs/resources/emoji#modify-guild-emoji
"""
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -125,7 +125,7 @@ async def delete_guild_emoji(
Reference:
https://discord.com/developers/docs/resources/emoji#delete-guild-emoji
"""
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down
16 changes: 8 additions & 8 deletions asyncord/client/guilds/models/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from collections.abc import Sequence
from typing import Annotated, Any, Self

from fbenum.adapter import FallbackAdapter
from pydantic import BaseModel, Field, field_serializer, field_validator, model_validator

from asyncord.base64_image import Base64ImageInputType
Expand Down Expand Up @@ -207,36 +206,37 @@ class UpdateAutoModerationRuleRequest(BaseModel):
https://discord.com/developers/docs/resources/auto-moderation#modify-auto-moderation-rule-json-params
"""

name: str
name: str | None = None
"""Name of the rule."""

event_type: FallbackAdapter[AutoModerationRuleEventType]
event_type: AutoModerationRuleEventType | None = None
"""Rule event type."""

trigger_metadata: TriggerMetadata | None = None
"""Rule trigger metadata.
Required, but can be omited based on trigger type.
Reference:
https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-trigger-metadata
"""

actions: list[RuleAction]
actions: list[RuleAction] | None = None
"""Actions which will execute when the rule is triggered."""

enabled: bool
enabled: bool | None = None
"""Whether the rule is enabled.
False by default.
"""

exempt_roles: list[SnowflakeInputType] = Field(max_length=20)
exempt_roles: Annotated[list[SnowflakeInputType], Field(max_length=20)] | None = None
"""Role ids that should not be affected by the rule.
Maximum of 20.
"""

exempt_channels: list[SnowflakeInputType] = Field(max_length=50)
exempt_channels: Annotated[list[SnowflakeInputType], Field(max_length=50)] | None = None
"""Channel ids that should not be affected by the rule.
Maximum of 50.
Expand Down Expand Up @@ -394,7 +394,7 @@ class PruneRequest(BaseModel):
"""

days: int
"""Number of days to prune members for ."""
"""Number of days to prune members for."""

compute_prune_count: bool | None = None
"""Whether to compute the number of pruned members."""
Expand Down
2 changes: 1 addition & 1 deletion asyncord/client/guilds/models/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class PruneResponse(BaseModel):
https://discord.com/developers/docs/resources/guild#get-guild-prune-count
"""

pruned: int
pruned: int | None
"""Number of members pruned."""


Expand Down
10 changes: 5 additions & 5 deletions asyncord/client/guilds/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async def get_prune_count(
if include_roles is not None:
url_params['include_roles'] = ','.join(map(str, include_roles))

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -272,7 +272,7 @@ async def begin_prune(
"""
payload = prune_data.model_dump(mode='json', exclude_unset=True)

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -413,7 +413,7 @@ async def update_widget(
"""
url = self.guilds_url / str(guild_id) / 'widget'

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -477,7 +477,7 @@ async def update_onboarding(
"""
url = self.guilds_url / str(guild_id) / 'onboarding'

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -523,7 +523,7 @@ async def update_welcome_screen(
"""
url = self.guilds_url / str(guild_id) / 'welcome-screen'

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down
2 changes: 1 addition & 1 deletion asyncord/client/invites/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def delete_invite(
Reference:
https://discord.com/developers/docs/resources/invite#delete-invite
"""
if reason is not None:
if reason:
headers = {'X-Audit-Log-Reason': reason}
else:
headers = {}
Expand Down
10 changes: 5 additions & 5 deletions asyncord/client/members/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def update(
The updated member.
"""
url = self.members_url / str(user_id)
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -144,7 +144,7 @@ async def update_current_member(
The updated member.
"""
url = self.members_url / '@me'
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -167,7 +167,7 @@ async def add_role(
reason: Reason for adding the role to the member. Defaults to None.
"""
url = self.members_url / str(user_id) / 'roles' / str(role_id)
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -188,7 +188,7 @@ async def remove_role(
reason: Reason for removing the role from the member. Defaults to None.
"""
url = self.members_url / str(user_id) / 'roles' / str(role_id)
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -202,7 +202,7 @@ async def kick(self, user_id: SnowflakeInputType, reason: str | None = None) ->
reason: Reason for kicking the member. Defaults to None.
"""
url = self.members_url / str(user_id)
if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down
4 changes: 2 additions & 2 deletions asyncord/client/messages/models/requests/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from asyncord.client.messages.models.requests.components import ActionRow, Component
from asyncord.client.messages.models.requests.embeds import Embed
from asyncord.client.models.attachments import Attachment, AttachmentContentType
from asyncord.client.polls.models.requests import PollRequest
from asyncord.client.polls.models.requests import Poll
from asyncord.snowflake import SnowflakeInputType

__ALL__ = (
Expand Down Expand Up @@ -381,7 +381,7 @@ class CreateMessageRequest(BaseMessage):
I set it to True because it will be default behavior in the near future.
"""

poll: PollRequest | None = None
poll: Poll | None = None
"""A poll."""


Expand Down
8 changes: 4 additions & 4 deletions asyncord/client/messages/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async def delete(self, message_id: SnowflakeInputType, reason: str | None = None
"""
url = self.messages_url / str(message_id)

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -146,7 +146,7 @@ async def bulk_delete(
url = self.messages_url / 'bulk-delete'
payload = {'messages': [str(message_id) for message_id in message_ids]}

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down Expand Up @@ -196,7 +196,7 @@ async def pin_message(
"""
url = self.channels_url / str(channel_id) / 'pins' / str(message_id)

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand All @@ -217,7 +217,7 @@ async def unpin_message(
"""
url = self.channels_url / str(channel_id) / 'pins' / str(message_id)

if reason is not None:
if reason:
headers = {AUDIT_LOG_REASON: reason}
else:
headers = {}
Expand Down
Loading

0 comments on commit ed5c7c0

Please sign in to comment.