Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit 9a187c8

Browse files
author
kuso-senpai
committed
added EphemeralResponseMessage
1 parent 8f0b6d9 commit 9a187c8

File tree

5 files changed

+80
-28
lines changed

5 files changed

+80
-28
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ You can find more (and better) examples [here](https://github.com/discord-py-ui/
158158

159159
# Changelog
160160

161+
- <details>
162+
<summary>3.3.2</summary>
163+
164+
## **Added**
165+
- EphemeralResponseMessage
166+
> You can now edit a ephemeral message which was created from an interaction (ex. when a button in a hidden message was pressed)
167+
168+
</details>
161169

162170
- <details>
163171
<summary>3.3.1</summary>

discord_ui/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from .slash.types import OptionType, SlashPermission, SlashOption
44
from .slash.tools import ParseMethod
55
from .tools import components_to_dict
6-
from .receive import Interaction, InteractionType, ResponseMessage, Message, WebhookMessage, PressedButton, SelectedMenu, SlashedCommand, SlashedSubCommand, EphemeralMessage
6+
from .receive import Interaction, InteractionType, ResponseMessage, Message, WebhookMessage, PressedButton, SelectedMenu, SlashedCommand, SlashedSubCommand, EphemeralMessage, EphemeralResponseMessage
77

88
from .override import override_dpy
99
override_dpy()
1010

11-
__version__ = "3.3.1"
11+
__version__ = "3.3.2"

discord_ui/cogs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Gotta do something here soon

discord_ui/receive.py

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
from discord.message import PartialMessage
12
from discord_ui.slash.tools import handle_options
2-
from .errors import InvalidEvent, OutOfValidRange
3+
from .errors import InvalidEvent, OutOfValidRange, WrongType
34
from .slash.errors import AlreadyDeferred, EphemeralDeletion
45
from .slash.types import ContextCommand, SlashCommand, SubSlashCommand
56
from .tools import MISSING, setup_logger
67
from .http import BetterRoute, jsonifyMessage, send_files
7-
from .components import ActionRow, Button, LinkButton, SelectMenu, SelectOption
8+
from .components import ActionRow, Button, ComponentType, LinkButton, SelectMenu, SelectOption
89

910
import discord
11+
from discord.ext.commands import Bot
1012
from discord.errors import HTTPException
1113
from discord.state import ConnectionState
1214

@@ -357,12 +359,11 @@ async def getResponseMessage(state: ConnectionState, data, user=None, response =
357359
channel = state.get_channel(int(data["channel_id"]))
358360
if response and user:
359361
if data.get("message") is not None and data["message"]["flags"] == 64:
360-
try:
361-
return ResponseMessage(state=state, channel=channel, data=data, user=user)
362-
except:
363-
return EphemeralMessage(state=state, channel=channel, data=data["message"])
362+
return EphemeralResponseMessage(state=state, channel=channel, data=data, user=user)
364363
return ResponseMessage(state=state, channel=channel, data=data, user=user)
365364

365+
if data.get("message") is not None and data["message"]["flags"] == 64:
366+
return EphemeralMessage(state=state, channel=channel, data=data["message"])
366367
return Message(state=state, channel=channel, data=data)
367368

368369
class Message(discord.Message):
@@ -371,6 +372,7 @@ def __init__(self, *, state, channel, data):
371372
self.__slots__ = discord.Message.__slots__ + ("components", "supressed")
372373
self._payload = data
373374

375+
self._state: ConnectionState = None
374376
super().__init__(state=state, channel=channel, data=data)
375377
self.components: typing.List[typing.Union[Button, LinkButton, SelectMenu]] = []
376378
"""The components in the message
@@ -599,7 +601,8 @@ def check(btn, msg):
599601
if custom_id is not MISSING and btn.custom_id == custom_id:
600602
return True
601603
return True
602-
604+
if not isinstance(client, Bot):
605+
raise WrongType("client", client, "discord.ext.commands.Bot")
603606
return (await client.wait_for('button_press' if event_name.lower() == "button" else "menu_select", check=check, timeout=timeout))[0]
604607

605608
raise InvalidEvent(event_name, ["button", "select"])
@@ -665,22 +668,62 @@ def __init__(self, value) -> None:
665668

666669

667670
class EphemeralMessage(Message):
668-
"""Represents a hidden (ephemeral) message
669-
670-
.. note::
671-
672-
You will get this class for a message only if you sent a hidden response
673-
674-
This class is almost the same as the :class:`~Message` class, but you can't edit, delete or reply to the message
675-
676-
If you want to "reply" to id, use the `interaction.send` method instead
677-
"""
678-
def __init__(self, *, state, channel, data, application_id=MISSING, token=MISSING):
671+
"""Represents a hidden (ephemeral) message"""
672+
def __init__(self, state, channel, data, application_id=MISSING, token=MISSING):
679673
Message.__init__(self, state=state, channel=channel, data=data)
680674
self._application_id = application_id
681675
self._interaction_token = token
682676
async def edit(self, **fields):
683677
r = BetterRoute("PATCH", f"/webhooks/{self._application_id}/{self._interaction_token}/messages/{self.id}")
684678
self._update(await self._state.http.request(r, json=jsonifyMessage(**fields)))
685679
async def delete(self):
686-
raise EphemeralDeletion()
680+
raise EphemeralDeletion()
681+
682+
class EphemeralResponseMessage(ResponseMessage):
683+
"""A ephemeral message wich was created from an interaction
684+
685+
.. important::
686+
687+
Methods like `.edit()`, which change the original message, need a `token` paremeter passed in order to work
688+
"""
689+
def __init__(self, *, state, channel, data, user):
690+
ResponseMessage.__init__(self, state=state, channel=channel, data=data, user=user)
691+
692+
async def edit(self, token, **fields):
693+
"""Edits the message
694+
695+
Parameters
696+
----------
697+
token: :class:`str`
698+
The token of the interaction with wich this ephemeral message was sent
699+
fields: :class:`kwargs`
700+
The fields to edit (ex. `content="...", embed=..., attachments=[...]`)
701+
702+
Example
703+
704+
.. code-block::
705+
706+
async def testing(ctx):
707+
msg = await ctx.send("hello hidden world", components=[Button("test")])
708+
btn = await msg.wait_for(client, "button")
709+
await btn.message.edit(ctx.token, content="edited", components=None)
710+
711+
"""
712+
route = BetterRoute("PATCH", f"/webhooks/{self.interaction.application_id}/{token}/messages/{self.id}")
713+
self._update(await self._state.http.request(route, json=jsonifyMessage(**fields)))
714+
async def delete(self):
715+
raise EphemeralDeletion()
716+
async def disable_components(self, token, disable = True):
717+
"""Disables all component in the message
718+
719+
Parameters
720+
----------
721+
disable: :class:`bool`, optional
722+
Whether to disable (``True``) or enable (``False``) als components; default True
723+
724+
"""
725+
fixed = []
726+
for x in self.components:
727+
x.disabled = disable
728+
fixed.append(x)
729+
await self.edit(token, components=fixed)

discord_ui/slash/tools.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ def resolve(data, _state):
6161
channel = None
6262
if ChannelType(channel_data["type"]) is ChannelType.text:
6363
channel = discord.TextChannel(data=channel_data, guild=guild, state=_state)
64-
elif ChannelType(channel_data["type"]) is ChannelType.voice:
64+
elif ChannelType(channel_data["type"]) is ChannelType.voice:
6565
channel = discord.VoiceChannel(data=channel_data, guild=guild, state=_state)
66-
elif ChannelType(channel_data["type"]) is ChannelType.category:
66+
elif ChannelType(channel_data["type"]) is ChannelType.category:
6767
channel = discord.CategoryChannel(data=channel_data, guild=guild, state=_state)
68-
elif ChannelType(channel_data["type"]) is ChannelType.group:
68+
elif ChannelType(channel_data["type"]) is ChannelType.group:
6969
channel = discord.GroupChannel(data=channel_data, guild=guild, state=_state)
70-
elif ChannelType(channel_data["type"]) is ChannelType.news:
70+
elif ChannelType(channel_data["type"]) is ChannelType.news:
7171
channel = discord.NewsChannel(data=channel_data, guild=guild, state=_state)
72-
elif ChannelType(channel_data["type"]) is ChannelType.private:
72+
elif ChannelType(channel_data["type"]) is ChannelType.private:
7373
channel = discord.DMChannel(data=channel_data, guild=guild, state=_state)
74-
elif ChannelType(channel_data["type"]) is ChannelType.store:
74+
elif ChannelType(channel_data["type"]) is ChannelType.store:
7575
channel = discord.StoreChannel(data=channel_data, guild=guild, state=_state)
76-
elif ChannelType(channel_data["type"]) is ChannelType.stage_voice:
76+
elif ChannelType(channel_data["type"]) is ChannelType.stage_voice:
7777
channel = discord.StageChannel(data=channel_data, guild=guild, state=_state)
7878
resolved["channels"][channel_id] = channel
7979
elif x == "roles":

0 commit comments

Comments
 (0)