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

Commit 0e250c6

Browse files
committed
v1.0.4
1 parent 41e1eee commit 0e250c6

File tree

3 files changed

+74
-38
lines changed

3 files changed

+74
-38
lines changed

README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Example:
110110
```py
111111
@client.listen('on_button_press')
112112
async def on_button(btn: PressedButton, msg: ResponseMsg):
113-
btn.respond("You pressed " + btn.content)
113+
await btn.respond("You pressed " + btn.content)
114114
```
115115

116116
- - - -
@@ -133,7 +133,7 @@ async def on_message(message: discord.Message):
133133

134134
@client.listen('on_button_press')
135135
async def on_button(btn: PressedButton, msg: ResponseMessage):
136-
msg.respond("You pressed on " + btn.content + " with the customID " + btn.custom_id)
136+
await msg.respond("You pressed on " + btn.content + " with the customID " + btn.custom_id)
137137

138138
client.run("Your secret token")
139139
```
@@ -158,9 +158,9 @@ async def on_message(message: discord.Message):
158158
btn, msg = await client.wait_for("button_press", check=lambda btn, msg: btn.member.id == message.author.id and msg.id == question.id)
159159

160160
if btn.custom_id == "cats":
161-
msg.respond("yay, cats!")
161+
await msg.respond("yay, cats!")
162162
elif btn.custom_id == "dogs":
163-
msg.respond("yay, dogs")
163+
await msg.respond("yay, dogs")
164164
```
165165

166166
- - - -
@@ -498,6 +498,10 @@ Extends the `Message` object
498498

499499
- pressedButton: `Button`
500500
> The button which was pressed
501+
502+
- acknowledged: `bool`
503+
> Whether the message was acknowledged
504+
501505
</details>
502506

503507
<details>
@@ -518,15 +522,17 @@ Extends the `Message` object
518522

519523
- <details>
520524
<summary>respond: <code>function</code></summary>
521-
Responds to the interaction
525+
Responds to the interaction and sends a message
522526

523527
```py
524-
def respond(self, content=None, *, tts=False,
528+
async def respond(self, content=None, *, tts=False,
525529
embed = None, embeds=None, file=None, files=None, nonce=None,
526-
allowed_mentions=None, reference=None, mention_author=None, buttons=None,
527-
ninjaMode = False):
530+
allowed_mentions=None, mention_author=None, buttons=None,
531+
ninjaMode = False) -> Message or None:
528532
```
529533

534+
_| coroutine |_
535+
530536
#### **Parameters**
531537

532538
- content: `str`
@@ -553,9 +559,6 @@ Extends the `Message` object
553559
- allowed_mentions: `discord.Allowed_mentions`
554560
> Mentions allowed in this message
555561

556-
- reference: `discord.MessageReference or discord.Message`
557-
> The message to which the message replies
558-
559562
- mention_author: `bool`
560563
> Whether the author should be mentioned
561564

@@ -567,6 +570,10 @@ Extends the `Message` object
567570
>
568571
> (User will see nothing)
569572

573+
#### **Returns**
574+
- `Message or None`
575+
> The sent message if ninjaMode is false
576+
570577
</details>
571578
</details>
572579

@@ -579,6 +586,7 @@ Extends the `Message` object
579586

580587
- - - -
581588

589+
582590
## Events
583591

584592
Added events for `client.wait_for` and `client.listen`
@@ -609,8 +617,27 @@ Added events for `client.wait_for` and `client.listen`
609617
# Changelog
610618

611619
- <details>
612-
<summary>1.0.3</summary>
620+
<summary>1.0.4</summary>
613621

614-
- Added `hash` attribute to buttons
622+
### **Added**
623+
- `ResponseMessage.acknowledged`
624+
> If the message was acknowledged with the `ResponseMessage.acknowledge()` function, the variable will be set to true
625+
626+
### **Changed**
627+
628+
- `ResponseMessage.respond()` => `await ResponseMessage.respond()`
629+
> respond() function is now async and needs to be awaited
630+
631+
- `ResponseMessage.respond() -> None` => `ResponseMessage.respond() -> Message or None`
632+
> respond() now returns the sent message or None if ninjaMode is true
633+
634+
</details>
615635

636+
- <details>
637+
<summary>1.0.3</summary>
638+
639+
### **Added**
640+
- `Button.hash`
641+
> Buttons have now a custom hash property, generated by the discord api
642+
616643
</details>

discord_py_buttons/apiRequests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def jsonifyMessage(content = None, *, tts=False,
5858
payload["embeds"] = [em.to_dict() for em in embeds]
5959

6060
if reference is not None:
61-
if type(reference) is not discord.MessageReference and type(reference) is not discord.Message:
61+
if type(reference) not in [discord.MessageReference, discord.Message] and not issubclass(type(reference), discord.Message):
6262
raise TypeError("Reference must be of type 'discord.MessageReference' or 'discord.Message', not " + str(type(reference)))
6363
if type(reference) is discord.MessageReference:
6464
payload["message_reference"] = reference.to_dict()

discord_py_buttons/receive.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ class ResponseMessage(Message):
220220
pressedButton: `PressedButton`
221221
The button which was presed
222222
acknowledge: `function`
223-
Function to acknowledge the buttonPress interaction
223+
Function to acknowledge the button-press interaction
224224
respond: `function`
225225
Function to respond to the buttonPress interaction
226+
acknowledged: `bool`
227+
Whether the button was acknowledged with the acknowledge functionn
226228
227229
Attributes
228230
----------------
@@ -329,6 +331,7 @@ def __init__(self, *, state, channel, data, user, client):
329331
super().__init__(state=state, channel=channel, data=data["message"])
330332

331333
self._discord = client
334+
self.acknowledged = False
332335
for x in self.buttons:
333336
if hasattr(x, 'custom_id') and x.custom_id == data["data"]["custom_id"]:
334337
self.pressedButton = PressedButton(data, user, x)
@@ -346,10 +349,14 @@ def acknowledge(self):
346349
if r.status_code == 403:
347350
raise discord.ClientException(r.json(), "forbidden")
348351

349-
def respond(self, content=None, *, tts=False, embed = None, embeds=None, file=None, files=None, nonce=None,
350-
allowed_mentions=None, reference=None, mention_author=None, buttons=None,
351-
ninjaMode = False):
352+
self.acknowledged = True
353+
354+
async def respond(self, content=None, *, tts=False, embed = None, embeds=None, file=None, files=None, nonce=None,
355+
allowed_mentions=None, mention_author=None, buttons=None,
356+
ninjaMode = False) -> Message or None:
352357
"""
358+
| coro |
359+
353360
Function to respond to the interaction
354361
355362
@@ -388,10 +395,6 @@ def respond(self, content=None, *, tts=False, embed = None, embeds=None, file=No
388395
```
389396
Controls the mentions being processed in this message
390397
```py
391-
(discord.MessageReference or discord.Message) reference
392-
```
393-
The message to refer
394-
```py
395398
(bool) mention_author
396399
```
397400
Whether the author should be mentioned
@@ -403,23 +406,29 @@ def respond(self, content=None, *, tts=False, embed = None, embeds=None, file=No
403406
```py
404407
(bool) ninjaMode
405408
```
406-
If true, the client will respond to the button interaction with almost nothing
409+
If true, the client will respond to the button interaction with almost nothing and returns nothing
410+
411+
Returrns
412+
-----------------------
413+
```py
414+
(Message or None)
415+
```
416+
The sent message if ninjaMode is false, otherwise `None`
417+
407418
"""
408-
if ninjaMode:
409-
r = POST(self._discord.http.token, f'https://discord.com/api/v8/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback', {
410-
"type": 6
411-
})
412-
else:
413-
json = jsonifyMessage(content=content, tts=tts, embed=embed, embeds=embeds, file=file, files=files, nonce=nonce, allowed_mentions=allowed_mentions, reference=reference, mention_author=mention_author, buttons=buttons)
414-
if "embed" in json:
415-
json["embeds"] = [json["embed"]]
416-
del json["embed"]
417-
418-
r = POST(self._discord.http.token, f'https://discord.com/api/v8/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback', {
419-
"type": 4,
420-
"data": json
421-
})
419+
msg = None
420+
if not ninjaMode:
421+
json = jsonifyMessage(content=content, tts=tts, embed=embed, embeds=embeds, file=file, files=files, nonce=nonce, allowed_mentions=allowed_mentions, reference=discord.MessageReference(message_id=self.id, channel_id=self.channel.id), mention_author=mention_author, buttons=buttons)
422+
r = POST(token=self._discord.http.token, URL=(url + f"/channels/{self.channel.id}/messages"), data=json)
423+
msg = await getResponseMessage(self._discord, r.json(), response=False)
424+
425+
r = POST(self._discord.http.token, f'https://discord.com/api/v8/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback', {
426+
"type": 6
427+
})
428+
422429
if r.status_code == 403:
423430
raise discord.ClientException(r.json(), "Forbidden")
424431
if r.status_code == 400:
425-
raise discord.ClientException(r.json(), "Error while sending message")
432+
raise discord.ClientException(r.json(), "Error while sending message")
433+
434+
return msg

0 commit comments

Comments
 (0)