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

Commit 05f1780

Browse files
author
kuso-senpai
committed
added hidden responses, issues fixed
1 parent 5eb49b9 commit 05f1780

File tree

5 files changed

+93
-83
lines changed

5 files changed

+93
-83
lines changed

README.md

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ Buttons(client: discord.ext.commands.client)
196196
<summary><b>send</b></summary>
197197

198198
```py
199-
async def send(self, channel, content = None, *, tts = False, embed = None,
200-
embeds = None, file = None, files = None, delete_after = None, nonce = None,
201-
allowed_mentions = None, reference = None, mention_author = None, buttons = None
199+
async def send(self, channel, content=None, *, tts=False, embed=None,
200+
embeds=None, file=None, files = None, delete_after = None, nonce = None,
201+
allowed_mentions=None, reference=None, mention_author=None, buttons=None
202202
) -> Message:
203203
```
204204

@@ -208,8 +208,6 @@ Buttons(client: discord.ext.commands.client)
208208

209209
- channel: `discord.TextChannel`
210210
> The textchannel where the message should be sent
211-
>
212-
> __Required__
213211

214212
- content: `str`
215213
> The text content of the message
@@ -499,23 +497,30 @@ Extends the `Message` object
499497
- pressedButton: `Button`
500498
> The button which was pressed
501499

502-
- acknowledged: `bool`
503-
> Whether the message was acknowledged
500+
- deferred: `bool`
501+
> Whether the message was deferred
504502

505503
</details>
506504

507505
<details>
508506
<summary><b>Methods</b></summary>
509507

510508
- <details>
511-
<summary>acknowledge: <code>function</code></summary>
509+
<summary>defer: <code>function</code></summary>
512510

513-
Acknowledges that the interaction was received
511+
defers that the interaction was received
514512

515513
```py
516-
def acknowledge():
514+
async def defer(hidden = False):
517515
```
518516

517+
_| coroutine |_
518+
519+
#### **Parameters**
520+
521+
- hidden
522+
> Whether the loading thing will be shown only to the user
523+
519524
> This function should be used if your client needs more than 15 seconds to responod
520525

521526
</details>
@@ -527,7 +532,7 @@ Extends the `Message` object
527532
```py
528533
async def respond(self, content=None, *, tts=False,
529534
embed = None, embeds=None, file=None, files=None, nonce=None,
530-
allowed_mentions=None, mention_author=None, buttons=None,
535+
allowed_mentions=None, mention_author=None, buttons=None, hidden=False,
531536
ninjaMode = False) -> Message or None:
532537
```
533538

@@ -564,6 +569,9 @@ Extends the `Message` object
564569

565570
- buttons: `List[Button]`
566571
> A list of buttons in this message
572+
573+
- hidden: `bool`
574+
> Whether the message should be only visible to the user
567575

568576
- ninjaMode: `bool`
569577
> Whether the client should respond silent like a ninja to the interaction
@@ -572,7 +580,7 @@ Extends the `Message` object
572580

573581
#### **Returns**
574582
- `Message or None`
575-
> The sent message if ninjaMode is false
583+
> The sent message if ninjaMode and hidden are both set to false
576584

577585
</details>
578586
</details>
@@ -617,19 +625,41 @@ Added events for `client.wait_for` and `client.listen`
617625
# Changelog
618626

619627

628+
- <details>
629+
<summary>1.1.0</summary>
630+
631+
### **Changed**
632+
- Major changes to request code, now using the client's request
633+
- `ResponseMessage.acknowledge()` -> `ResponseMessage.defer()`
634+
> Changed the name of the function + changed `ResponseMessage.acknowledged` -> `ResponseMessage.deferred`
635+
- `ResponseMessage.defer()` => `await ResponseMessage.defer()`
636+
> `defer` (`acknowledge`) is now async and needs to be awaited
637+
638+
### **Added**
639+
- hidden responses
640+
> You can now send responses only visible to the user
641+
642+
643+
### **Fixed**
644+
- `ResponseMessage.respond()`
645+
> Now doesn't show a failed interaction
646+
647+
648+
</details>
649+
620650
- <details>
621651
<summary>1.0.5</summary>
622652

623653
### **Fixed**
624654
- `ResponseMessage.respond()`
625-
> responding now doesn't fail after sending the message, it will now acknowledge the interaction by it self if not already acknowledged and then send the message
655+
> responding now doesn't fail after sending the message, it will now defer the interaction by it self if not already deferred and then send the message
626656

627657
- <details>
628658
<summary>1.0.4</summary>
629659

630660
### **Added**
631661
- `ResponseMessage.acknowledged`
632-
> If the message was acknowledged with the `ResponseMessage.acknowledge()` function, the variable will be set to true
662+
> Whether the message was acknowledged with the `ResponseMessage.acknowledged()` function
633663

634664
### **Changed**
635665

discord_py_buttons/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from .buttons import Button, LinkButton, Colors
33
from .receive import ResponseMessage, Message, PressedButton
44

5-
__version__ = "1.0.5"
5+
__version__ = "1.1.0"

discord_py_buttons/client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from . import apiRequests
1+
from typing import List
2+
from .tools import jsonifyMessage, V8Route
23
from .receive import Message, getResponseMessage
34

45
import discord
@@ -61,7 +62,7 @@ async def on_socket_response(self, msg):
6162

6263

6364
async def send(self, channel: discord.TextChannel, content=None, *, tts=False,
64-
embed=None, embeds=None, file=None, files=None, delete_after=None, nonce=None,
65+
embed=None, embeds=None, file: discord.File=None, files: List[discord.File]=None, delete_after=None, nonce=None,
6566
allowed_mentions=None, reference=None, mention_author=None, buttons=None
6667
) -> Message:
6768
"""
@@ -133,14 +134,14 @@ async def send(self, channel: discord.TextChannel, content=None, *, tts=False,
133134
if type(channel) != discord.TextChannel:
134135
raise discord.InvalidArgument("Channel must be of type discord.TextChannel")
135136

136-
r = apiRequests.POST(self._discord.http.token, f"{apiRequests.url}/channels/{channel.id}/messages", data=apiRequests.jsonifyMessage(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))
137-
if r.status_code == 403:
138-
raise discord.ClientException(r.json(), "Got forbidden response")
139-
if r.status_code != 200:
140-
raise Exception(r.json())
137+
payload = jsonifyMessage(content, tts=tts, embed=embed, embeds=embeds, nonce=nonce, allowed_mentions=allowed_mentions, reference=reference, mention_author=mention_author, buttons=buttons)
138+
141139

142-
msg = await getResponseMessage(self._discord, r.json(), response=False)
140+
route = V8Route("POST", f"/channels/{channel.id}/messages")
143141

142+
r = await self._discord.http.request(route, json=payload)
143+
msg = await getResponseMessage(self._discord, r, response=False)
144+
144145
if delete_after is not None:
145146
await msg.delete(delay=delete_after)
146147

discord_py_buttons/receive.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .buttons import Button, LinkButton
2-
from .apiRequests import POST, url, jsonifyMessage
2+
from .tools import V8Route, jsonifyMessage
33

44
import discord
55
from discord.ext import commands
@@ -219,12 +219,12 @@ class ResponseMessage(Message):
219219
----------------
220220
pressedButton: `PressedButton`
221221
The button which was presed
222-
acknowledge: `function`
223-
Function to acknowledge the button-press interaction
222+
defer: `function`
223+
Function to defer 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
226+
deferred: `bool`
227+
Whether the button was deferred with the defer functionn
228228
229229
Attributes
230230
----------------
@@ -336,23 +336,22 @@ def __init__(self, *, state, channel, data, user, client):
336336
if hasattr(x, 'custom_id') and x.custom_id == data["data"]["custom_id"]:
337337
self.pressedButton = PressedButton(data, user, x)
338338

339-
def acknowledge(self):
339+
async def defer(self, hidden=False):
340340
"""
341341
This will acknowledge the interaction. This will show the (*Bot* is thinking...) Dialog
342342
343343
This function should be used if the Bot needs more than 15 seconds to respond
344344
"""
345345

346-
r = POST(self._discord.http.token, f'{url}/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback', {
347-
"type": 5
348-
})
349-
if r.status_code == 403:
350-
raise discord.ClientException(r.json(), "forbidden")
351-
352-
self.acknowledged = True
346+
body = {"type": 5}
347+
if hidden == True:
348+
body |= { "flags": 64 }
349+
350+
await self._discord.http.request(V8Route("POST", f'/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback'), json=body)
351+
self.deferred = True
353352

354353
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,
354+
allowed_mentions=None, mention_author=None, buttons=None, hidden=False,
356355
ninjaMode = False) -> Message or None:
357356
"""
358357
| coro |
@@ -402,7 +401,10 @@ async def respond(self, content=None, *, tts=False, embed = None, embeds=None, f
402401
List[Button] buttons
403402
```
404403
A list of Buttons for the message to be included
405-
404+
```py
405+
(bool) hidden
406+
```
407+
Whether the response should be visible only to the user
406408
```py
407409
(bool) ninjaMode
408410
```
@@ -416,23 +418,22 @@ async def respond(self, content=None, *, tts=False, embed = None, embeds=None, f
416418
The sent message if ninjaMode is false, otherwise `None`
417419
418420
"""
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-
424-
if not self.acknowledged:
425-
# Preventing the interaction to fail while fetching the channel
426-
self.acknowledge()
427-
msg = await getResponseMessage(self._discord, r.json(), response=False)
421+
if ninjaMode:
422+
await self._discord.http.request(V8Route("POST", f'/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback'), json={
423+
"type": 6
424+
})
425+
return
426+
427+
body = jsonifyMessage(content=content, tts=tts, embed=embed, embeds=embeds, nonce=nonce, allowed_mentions=allowed_mentions, reference=discord.MessageReference(message_id=self.id, channel_id=self.channel.id), mention_author=mention_author, buttons=buttons)
428428

429-
r = POST(self._discord.http.token, f'https://discord.com/api/v8/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback', {
430-
"type": 6
431-
})
429+
if hidden:
430+
body |= {"flags": 64}
432431

433-
if r.status_code == 403:
434-
raise discord.ClientException(r.json(), "Forbidden")
435-
if r.status_code == 400:
436-
raise discord.ClientException(r.json(), "Error while sending message")
437-
438-
return msg
432+
await self._discord.http.request(V8Route("POST", f'/interactions/{self.pressedButton.interaction["id"]}/{self.pressedButton.interaction["token"]}/callback'), json={
433+
"type": 4,
434+
"data": body
435+
})
436+
if not hidden:
437+
responseMSG = await self._discord.http.request(V8Route("GET", f"/webhooks/{self._discord.user.id}/{self.pressedButton.interaction['token']}/messages/@original"))
438+
439+
return await getResponseMessage(self._discord, responseMSG, response=False)

discord_py_buttons/apiRequests.py renamed to discord_py_buttons/tools.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
from .buttons import Button
2+
from discord.http import Route
23

34
import discord
45

5-
import requests
66
from typing import List
77

88
url = "https://discord.com/api/v8"
99

1010

11-
def POST(token, URL, data):
12-
"""POST request with the Bot token in the Authorization Header"""
13-
return requests.post(URL,
14-
json=data, headers={"Authorization": f"Bot {token}"})
15-
def GET(token, URL):
16-
"""GET request with the Bot token in the Authorization Header"""
17-
return requests.get(URL,
18-
headers={"Authorization": f"Bot {token}"})
19-
def DELETE(token, URL):
20-
"""DELETE request with the Bot token in the Authorization Header"""
21-
return requests.delete(URL,
22-
headers={"Authorization": f"Bot {token}"})
11+
class V8Route(Route):
12+
BASE = "https://discord.com/api/v8"
2313

2414
def jsonifyMessage(content = None, *, tts=False,
25-
embed: discord.Embed = None, embeds: List[discord.Embed], file: discord.File = None, files: List[discord.File] = None, nonce: int = None,
26-
allowed_mentions: discord.AllowedMentions = None, reference: discord.MessageReference = None, mention_author: bool = None, buttons: List[Button] = None):
15+
embed: discord.Embed = None, embeds: List[discord.Embed], nonce: int = None,
16+
allowed_mentions: discord.AllowedMentions = None, reference: discord.MessageReference = None, mention_author: bool = None, buttons: List[Button] = None):
2717
"""Turns parameters from the `discord.TextChannel.send` function into json for requests"""
2818
payload = { "tts": tts }
2919

@@ -33,18 +23,6 @@ def jsonifyMessage(content = None, *, tts=False,
3323
if nonce is not None:
3424
payload["nonce"] = nonce
3525

36-
if file is not None and files is not None:
37-
raise discord.InvalidArgument("cannot pass both 'files' and 'file' parameter")
38-
39-
if file is not None:
40-
if type(file) is not discord.File:
41-
raise TypeError("file must be of type discord.File")
42-
raise Exception("sending file is not supportet in this version, instead try using the normal discord.TextChannel.send(file=yourFile) function until this is completed")
43-
if files is not None:
44-
if type(file) is not discord.File:
45-
raise TypeError("file must be of type discord.File")
46-
raise Exception("sending files is not supportet in this version, instead try using the normal discord.TextChannel.send(files=[yourFiles...]) function until this is completed")
47-
4826
if embed is not None and embeds is not None:
4927
raise discord.InvalidArgument("cannot pass both 'embed' and 'embeds' parameter")
5028

0 commit comments

Comments
 (0)