Bot config and channel init

This commit is contained in:
BreadTube 2025-09-23 04:50:23 +09:00 committed by Corentin
commit 72edbe6599
7 changed files with 499 additions and 109 deletions

View file

@ -1,8 +1,27 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
from enum import Enum, IntFlag
class FileMime(Enum):
AUDIO_OGG = 'audio/ogg'
IMAGE_JPEG = 'image/jpeg'
IMAGE_PNG = 'image/png'
IMAGE_SVG = 'image/svg'
JSON = 'application/json'
PDF = 'application/pdf'
TEXT_CSV = 'text/csv'
TEXT_HTML = 'text/html'
TEXT_MARKDOWN = 'text/markdown'
TEXT_PLAIN = 'text/plain'
VIDEO_MP4 = 'video/mp4'
VIDEO_MPEG = 'video/mpeg'
VIDEO_WEBM = 'video/webm'
ZIP = 'application/zip'
class ChannelType(Enum):
GUILD_TEXT = 0
DM = 1
@ -35,8 +54,9 @@ class OverwriteType(Enum):
class Permissions(IntFlag):
NONE = 0
# Allows creation of instant invites
CREATE_INSTANT_INVITE = 1 << 0
CREATE_INSTANT_INVITE = 1
# Allows kicking members
KICK_MEMBERS = 1 << 1
# Allows banning members
@ -153,6 +173,14 @@ class Overwrite:
allow: Permissions
deny: Permissions
@staticmethod
def from_dict(info: dict) -> Overwrite:
return Overwrite(
id=int(info['id']),
type=OverwriteType(info['type']),
allow=Permissions(int(info['allow'])),
deny=Permissions(int(info['deny'])))
@dataclass
class ChannelCategory:
@ -164,6 +192,18 @@ class ChannelCategory:
parent_id: int | None
flags: ChannelFlags
@staticmethod
def from_dict(info: dict) -> ChannelCategory:
parent_id: str | None = info.get('parent_id')
return ChannelCategory(
id=int(info['id']),
guild_id=int(info['guild_id']),
position=int(info['position']),
permission_overwrites=[Overwrite.from_dict(o) for o in info['permission_overwrites']],
name=info.get('name'),
parent_id=int(parent_id) if parent_id is not None else None,
flags=ChannelFlags(info['flags']))
@dataclass
class TextChannel:
@ -180,6 +220,25 @@ class TextChannel:
last_pin_timestamp: datetime | None
flags: ChannelFlags
@staticmethod
def from_dict(info: dict) -> TextChannel:
parent_id: str | None = info.get('parent_id')
last_message_id: str | None = info.get('last_message_id')
last_pin_timestamp: str | None = info.get('last_pin_timestamp')
return TextChannel(
id=int(info['id']),
guild_id=int(info['guild_id']),
position=int(info['position']),
permission_overwrites=[Overwrite.from_dict(o) for o in info['permission_overwrites']],
name=info.get('name'),
topic=info.get('topic'),
nsfw=info['nsfw'],
last_message_id=int(last_message_id) if last_message_id is not None else None,
rate_limit_per_user=int(info['rate_limit_per_user']),
parent_id=int(parent_id) if parent_id is not None else None,
last_pin_timestamp=(datetime.fromisoformat(last_pin_timestamp) if last_pin_timestamp is not None else None),
flags=ChannelFlags(info['flags']))
@dataclass
class User: # TODO : complete attributes
@ -188,6 +247,14 @@ class User: # TODO : complete attributes
discriminator: str
global_name: str | None
@staticmethod
def from_dict(info: dict) -> User:
return User(
id=int(info['id']),
username=info['username'],
discriminator=info['discriminator'],
global_name=info.get('global_name'))
@dataclass
class Message: # TODO : complete attributes
@ -197,3 +264,96 @@ class Message: # TODO : complete attributes
content: str
timestamp: datetime
edited_timestamp: datetime | None
@staticmethod
def from_dict(info: dict) -> Message:
edited_timestamp: str | None = info.get('edited_timestamp')
return Message(
id=int(info['id']),
channel_id=int(info['channel_id']),
author=(User.from_dict(info['author']) if info.get('webhook_id') is None else User(
id=info['webhook_id'], username='webhook', discriminator='webhook', global_name=None)),
content=info['content'],
timestamp=datetime.fromisoformat(info['timestamp']),
edited_timestamp=datetime.fromisoformat(edited_timestamp) if edited_timestamp is not None else None)
@dataclass
class RoleColors:
primary_color: int
seconday_color: int | None
tertiary_color: int | None
@staticmethod
def from_dict(info: dict) -> RoleColors:
seconday_color = info.get('secondary_color')
tertiary_color = info.get('tertiary_color')
return RoleColors(
primary_color=int(info['primary_color']),
seconday_color=int(seconday_color) if seconday_color is not None else None,
tertiary_color=int(tertiary_color) if tertiary_color is not None else None)
class RoleFlags(IntFlag):
NONE = 0
# role can be selected by members in an onboarding prompt
IN_PROMPT = 1
@dataclass
class RoleTags:
bot_id: int | None
intergration_id: int | None
premium_subscriber: bool
subcription_listing_id: int | None
available_for_purchase: bool
guild_connections: bool
@staticmethod
def from_dict(info: dict) -> RoleTags:
bot_id = info.get('bot_id')
intergration_id = info.get('intergration_id')
subcription_listing_id = info.get('subcription_listing_id')
return RoleTags(
bot_id=int(bot_id) if bot_id is not None else None,
intergration_id=int(intergration_id) if intergration_id is not None else None,
premium_subscriber='premium_subscriber' in info,
subcription_listing_id=int(subcription_listing_id) if subcription_listing_id is not None else None,
available_for_purchase='available_for_purchase' in info,
guild_connections='guild_connections' in info)
@dataclass
class Role:
id: int # role id
name: str # role name
color: int # Deprecated integer representation of hexadecimal color code
colors: RoleColors # the role's colors
hoist: bool # if this role is pinned in the user listing
icon: str | None # role icon hash
unicode_emoji: str | None # role unicode emoji
position: int # position of this role (roles with the same position are sorted by id)
permissions: Permissions # permission bit set
managed: bool # whether this role is managed by an integration
mentionable: bool # whether this role is mentionable
tags: RoleTags | None # the tags this role has
flags: int # role flags combined as a bitfield
@staticmethod
def from_dict(info: dict) -> Role:
tags = info.get('tags')
return Role(
id=int(info['id']),
name=info['name'],
color=int(info['color']),
colors=RoleColors.from_dict(info['colors']),
hoist=info['hoist'],
icon=info.get('icon'),
unicode_emoji=info.get('unicode_emoji'),
position=int(info['position']),
permissions=Permissions(int(info['permissions'])),
managed=info['managed'],
mentionable=info['mentionable'],
tags=RoleTags.from_dict(tags) if tags is not None else None,
flags=RoleFlags(int(info['flags']))
)