Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 扩充 API, event, model 和 tpyes #41

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nonebot/adapters/discord/api/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ class ApiClient:
files: list[File] | None = ...,
attachments: list[AttachmentSend] | None = ...,
flags: MessageFlag | None = ...,
poll: PollRequest | None = ...,
) -> MessageGet:
"""create message

Expand Down Expand Up @@ -1827,6 +1828,8 @@ class ApiClient:
attachments: list[AttachmentSend] | None = ...,
flags: int | None = ...,
thread_name: str | None = ...,
applied_tags: list[SnowflakeType] | None = ...,
poll: PollRequest | None = ...,
**data,
) -> None: ...
@overload
Expand All @@ -1848,6 +1851,8 @@ class ApiClient:
attachments: list[AttachmentSend] | None = ...,
flags: int | None = ...,
thread_name: str | None = ...,
applied_tags: list[SnowflakeType] | None = ...,
poll: PollRequest | None = ...,
**data,
) -> MessageGet: ...
async def execute_webhook(
Expand All @@ -1868,6 +1873,8 @@ class ApiClient:
attachments: list[AttachmentSend] | None = ...,
flags: int | None = ...,
thread_name: str | None = ...,
applied_tags: list[SnowflakeType] | None = ...,
poll: PollRequest | None = ...,
**data,
) -> MessageGet | None:
"""https://discord.com/developers/docs/resources/webhook#execute-webhook"""
Expand Down Expand Up @@ -1918,6 +1925,7 @@ class ApiClient:
components: list[Component] | None = ...,
files: list[File] | None = ...,
attachments: list[AttachmentSend] | None = ...,
poll: PollRequest | None = ...,
) -> MessageGet:
"""https://discord.com/developers/docs/resources/webhook#edit-webhook-message"""
...
Expand Down
27 changes: 26 additions & 1 deletion nonebot/adapters/discord/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,8 @@ class InteractionCallbackMessage(BaseModel):
attachments: Optional[list["AttachmentSend"]] = None
"""attachment objects with filename and description.
See Uploading Files for details."""
poll: Optional["PollRequest"] = None
"""Details about the poll"""

files: Optional[list["File"]] = None

Expand Down Expand Up @@ -1757,6 +1759,7 @@ class MessageSend(BaseModel):
files: Optional[list[File]] = None
attachments: Optional[list[AttachmentSend]] = None
flags: Optional[MessageFlag] = None
poll: Optional["PollRequest"] = None


class ModifyChannelParams(BaseModel):
Expand Down Expand Up @@ -3677,7 +3680,7 @@ class Poll(BaseModel):
"""The question of the poll. Only `text` is supported."""
answers: list["PollAnswer"]
"""Each of the answers available in the poll."""
expiry: datetime.datetime
expiry: Optional[datetime.datetime] = None
"""The time when the poll ends."""
allow_multiselect: bool
"""Whether a user can select multiple answers"""
Expand All @@ -3687,6 +3690,27 @@ class Poll(BaseModel):
"""The results of the poll"""


class PollRequest(BaseModel):
"""This is the request object used when creating a poll across the
different endpoints. It is similar but not exactly identical to the
main poll object. The main difference is that the request has `duration`
which eventually becomes `expiry`.

see https://discord.com/developers/docs/resources/poll#poll-create-request-object
"""

question: "PollMedia"
"""The question of the poll. Only `text` is supported."""
answers: list["PollAnswer"]
"""Each of the answers available in the poll, up to 10"""
duration: Missing[int] = UNSET
"""Number of hours the poll should be open for, up to 32 days. Defaults to 24"""
allow_multiselect: Missing[bool] = UNSET
"""Whether a user can select multiple answers. Defaults to false"""
layout_type: Missing[int]
"""The layout type of the poll"""


class PollAnswer(BaseModel):
"""answer_id: Only sent as part of responses from Discord's API/Gateway.

Expand Down Expand Up @@ -4226,6 +4250,7 @@ class MessagePollVoteRemove(BaseModel):
"TeamMemberUser",
"AuthorizationResponse",
"Poll",
"PollRequest",
"PollAnswer",
"PollMedia",
"PollResults",
Expand Down
3 changes: 3 additions & 0 deletions nonebot/adapters/discord/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ class EmbedTypes(StrEnum):
"""article embed"""
link = "link"
"""link embed"""
poll_result = "poll_result"
"""poll result embed"""


class EntitlementType(IntEnum):
Expand Down Expand Up @@ -1148,6 +1150,7 @@ class MessageType(IntEnum):
GUILD_INCIDENT_REPORT_RAID = 38
GUILD_INCIDENT_REPORT_FALSE_ALARM = 39
PURCHASE_NOTIFICATION = 44
POLL_RESULT = 46


class MembershipState(IntEnum):
Expand Down
26 changes: 26 additions & 0 deletions nonebot/adapters/discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
File,
MessageGet,
MessageReference,
Poll,
PollRequest,
SelectMenu,
Snowflake,
SnowflakeType,
Expand Down Expand Up @@ -170,6 +172,10 @@ def reference(

return ReferenceSegment("reference", {"reference": _reference})

@staticmethod
def poll(poll: Union[Poll, PollRequest]) -> "PollSegment":
return PollSegment("poll", {"poll": poll})

@override
def is_text(self) -> bool:
return self.type == "text"
Expand Down Expand Up @@ -366,6 +372,21 @@ def __str__(self):
return f"<Reference:{self.data['reference'].message_id}>"


class PollData(TypedDict):
poll: Union[Poll, PollRequest]


@dataclass
class PollSegment(MessageSegment):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MeetWq 抱歉打扰了!

我添加了一个 PollSegment,需要配合 #42 添加 _validate

如何配合?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等你的合并之后我再改吧

if TYPE_CHECKING:
type: Literal["poll"]
data: PollData

@override
def __str__(self) -> str:
return f"<Poll:{self.data["poll"].question.text}>"


class Message(BaseMessage[MessageSegment]):
@classmethod
@override
Expand Down Expand Up @@ -455,6 +476,8 @@ def from_guild_message(cls, message: MessageGet) -> "Message":
msg.extend(
MessageSegment.component(component) for component in message.components
)
if message.poll:
msg.append(MessageSegment.poll(message.poll))
return msg

def extract_content(self) -> str:
Expand Down Expand Up @@ -487,6 +510,8 @@ def parse_message(message: Union[Message, MessageSegment, str]) -> dict[str, Any
components = [component.data["component"] for component in components]
if sticker_ids := (message["sticker"] or None):
sticker_ids = [sticker.data["id"] for sticker in sticker_ids]
if poll := (message["poll"] or None):
poll = poll[-1].data["poll"]

attachments = None
files = None
Expand All @@ -507,6 +532,7 @@ def parse_message(message: Union[Message, MessageSegment, str]) -> dict[str, Any
"message_reference": reference,
"components": components,
"sticker_ids": sticker_ids,
"poll": poll,
"files": files,
"attachments": attachments,
}.items()
Expand Down
Loading