Skip to content

Commit 5f98d04

Browse files
authored
Merge branch 'discord-py-slash-commands:master' into master
2 parents 12a3a33 + d68ea8e commit 5f98d04

File tree

8 files changed

+134
-57
lines changed

8 files changed

+134
-57
lines changed

.github/banner_transparent.png

14 KB
Loading

README.md

Lines changed: 115 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,89 @@
1+
<div align="center">
2+
<a href="https://pypi.org/project/discord-py-slash-command/">
3+
<img src="https://raw.githubusercontent.com/discord-py-slash-commands/discord-py-interactions/goverfl0w-new-readme/.github/banner_transparent.png" alt="discord-py-interactions" height="128">
4+
</a>
5+
<h2>Your ultimate discord interactions library for <a href="https://github.com/Rapptz/discord.py">discord.py</a>.</h2>
6+
</div>
7+
8+
<div align="center">
9+
<a href="https://app.codacy.com/gh/eunwoo1104/discord-py-slash-command?utm_source=github.com&utm_medium=referral&utm_content=eunwoo1104/discord-py-slash-command&utm_campaign=Badge_Grade_Settings">
10+
<img src="https://api.codacy.com/project/badge/Grade/224bdbe58f8f43f28a093a33a7546456" alt="Codacy Badge">
11+
</a>
12+
<a href="https://discord.gg/KkgMBVuEkx">
13+
<img alt="Discord" src="https://img.shields.io/discord/789032594456576001">
14+
</a>
15+
</div>
16+
117
<p align="center">
2-
<br>
3-
<a href="https://pypi.org/project/discord-py-slash-command/"><img src="https://raw.githubusercontent.com/eunwoo1104/discord-py-slash-command/master/.github/discordpyslashlogo.png" alt="discord-py-slash-command" height="128"></a>
4-
<h2 align = "center">A simple discord slash command handler for <a href="https://github.com/Rapptz/discord.py">discord.py</a></h2>
5-
</p>
6-
<p align="center">
7-
<a href="https://app.codacy.com/gh/eunwoo1104/discord-py-slash-command?utm_source=github.com&utm_medium=referral&utm_content=eunwoo1104/discord-py-slash-command&utm_campaign=Badge_Grade_Settings"><img src="https://api.codacy.com/project/badge/Grade/224bdbe58f8f43f28a093a33a7546456" alt="Codacy Badge"></a>
8-
<a href="https://discord.gg/KkgMBVuEkx"> <img alt="Discord" src="https://img.shields.io/discord/789032594456576001"></a>
9-
</p>
10-
<p align="center">
11-
<a href="#about">About</a> ⦿
12-
<a href="#installation">Installation</a> ⦿
13-
<a href="#examples">Examples</a> ⦿
14-
<a href="https://discord-py-slash-command.readthedocs.io/en/latest/">Documentation</a> ⦿
15-
<a href="https://discord.gg/KkgMBVuEkx">Discord Server</a>
18+
<a href="#about">About</a> |
19+
<a href="#installation">Installation</a> |
20+
<a href="#examples">Examples</a> |
21+
<a href="https://discord.gg/KkgMBVuEkx">Discord</a> |
22+
<a href="https://pypi.org/project/discord-py-slash-command/">PyPI</a>
1623
</p>
1724

18-
## About
19-
Discord Slash Commands are a new implementation for the Bot API that utilize the forward-slash "/" symbol.
20-
Released on 15 December 2020, many bot developers are still learning to learn how to implement this into
21-
their very own bots. This command handler aims to help serve as a guidance for those looking into wanting to add
22-
these new slash commands into their bots for those that use discord.py, building off of the current library
23-
code and substituting its own for where it's needed. *discord-py-slash-command* stands as the first public
24-
slash command handler library to be made for Discord Bot API libraries.
25+
# About
26+
## What is discord-interactions?
27+
discord-interactions is, in the simplest terms, a library extension that builds off of the currently existing
28+
discord.py API wrapper. While we do use our own basic class code for our own library, a large majority of
29+
this library uses discord.py base events in order to make contextualization of interactions relatively easy
30+
for us.
31+
32+
### When did this begin?
33+
In mid-December of 2020, Discord released the very first type of components, **slash commands.** These were
34+
relatively primitive at the time of their debut, however, over time they slowly came to grew more complex
35+
and mutable. This library was created 2 days after the release of slash commands to Discord, and ever since
36+
has been actively growing.
37+
38+
## What do we currently support?
39+
At this time, we are able to provide you an non-exhaustive list, however, incomplete (because Discord are actively
40+
creating more interactions at this time) of all components integrated as interactions:
41+
42+
* Slash Commands
43+
* Buttons
44+
* Selects (also known as *dropdowns* or *menus*)
2545

26-
## Installation
27-
You are able to easily install the *discord-py-slash-command* library by using the given PIP line below:
46+
# Installation
47+
## (Recommended)
48+
We recommend using pip in order to install our library. You are able to do this by typing the following line below:
2849

2950
`pip install -U discord-py-slash-command`
3051

31-
## Examples
32-
### Quick Startup
33-
This is a quick startup method towards using slash commands.
52+
# Examples
53+
## Slash Commands
54+
This example shows a very quick and simplistic solution to implementing a slash command.
55+
3456
```py
35-
import discord
36-
from discord.ext import commands
57+
from discord import Client, Intents, Embed
3758
from discord_slash import SlashCommand, SlashContext
3859

39-
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
60+
bot = Client(intents=Intents.default())
4061
slash = SlashCommand(bot)
4162

4263
@slash.slash(name="test")
43-
async def _test(ctx: SlashContext):
44-
embed = discord.Embed(title="embed test")
45-
await ctx.send(content="test", embeds=[embed])
64+
async def test(ctx: SlashContext):
65+
embed = Embed(title="Embed Test")
66+
await ctx.send(embed=embed)
4667

4768
bot.run("discord_token")
4869
```
4970

50-
### Advanced
51-
This offers implementation of the slash command library in the usage of a cog.
71+
### Cogs
72+
This example serves as an alternative method for using slash commands in a cog instead.
73+
5274
```py
5375
# bot.py
54-
from discord.ext import commands
55-
from discord_slash import SlashCommand
76+
from discord import Client, Intents, Embed
77+
from discord_slash import SlashCommand, SlashContext
5678

57-
bot = commands.Bot(command_prefix="prefix")
58-
slash = SlashCommand(bot, sync_commands=True, sync_on_cog_reload=True)
79+
bot = Client(intents=Intents.default())
80+
slash = SlashCommand(bot)
5981

6082
bot.load_extension("cog")
61-
bot.run("TOKEN")
83+
bot.run("discord_token")
6284

6385
# cog.py
64-
import discord
65-
from discord.ext import commands
86+
from discord import Embed
6687
from discord_slash import cog_ext, SlashContext
6788

6889
class Slash(commands.Cog):
@@ -71,17 +92,65 @@ class Slash(commands.Cog):
7192

7293
@cog_ext.cog_slash(name="test")
7394
async def _test(self, ctx: SlashContext):
74-
embed = discord.Embed(title="embed test")
75-
await ctx.send(content="test", embeds=[embed])
76-
95+
embed = Embed(title="Embed Test")
96+
await ctx.send(embed=embed)
97+
7798
def setup(bot):
7899
bot.add_cog(Slash(bot))
79100
```
80101

102+
## Buttons
103+
This basic example shows how to easily integrate buttons into your commands. Buttons are not limited to
104+
slash commands and may be used in regular discord.py commands as well.
105+
(This example refers off of [slash commands.](#slash-commands))
106+
107+
```py
108+
from discord_slash.utils.manage_components import create_button, create_actionrow
109+
from discord_slash.model import ButtonStyle
110+
111+
buttons = [
112+
create_button(style=ButtonStyle.green, label="A green button"),
113+
create_button(style=ButtonStyle.blue, label="A blue button")
114+
]
115+
action_row = create_actionrow(*buttons)
116+
117+
await ctx.send(components=[action_row])
118+
```
119+
120+
### Advanced
121+
For more advanced use, please refer to our official documentation on [buttons here.](https://discord-py-slash-command.readthedocs.io/en/latest/components.html#responding-to-interactions)
122+
123+
## Selects
124+
This basic example shows how to add selects into our bot. Selects offer the same accessibility as buttons do
125+
in premise of limitations.
126+
(This exmaple refers off of [slash commands.](#slash-commands))
127+
128+
```py
129+
from discord_slash.utils.manage_components import create_select, create_select_option, create_actionrow
130+
131+
select = create_select(
132+
options=[
133+
create_select_option("Lab Coat", value="coat", emoji="🥼"),
134+
create_select_option("Test Tube", value="tube", emoji="🧪"),
135+
create_select_option("Petri Dish", value="dish", emoji="🧫")
136+
],
137+
placeholder="Choose your option",
138+
min_values=1, # the minimum number of options a user must select
139+
max_values=2 # the maximum number of options a user can select
140+
)
141+
action_row = create_actionrow(select)
142+
143+
await ctx.send(components=[action_row])
144+
```
145+
146+
### Advanced
147+
For more advanced use, please refer to our official documentation on [selects here.](https://discord-py-slash-command.readthedocs.io/en/latest/components.html#what-about-selects-dropdowns)
148+
81149
--------
82-
- This library is based on gateway event. If you are looking for webserver based, have a look at this:
150+
151+
- The discord-interactions library is based off of API gateway events. If you are looking for a library webserver-based, please consider:
83152
- [dispike](https://github.com/ms7m/dispike)
84153
- [discord-interactions-python](https://github.com/discord/discord-interactions-python)
85-
- Or for other languages:
154+
- If you are looking for a similar library for other languages, please refer to here:
86155
- [discord-api-docs Community Resources: Interactions](https://discord.com/developers/docs/topics/community-resources#interactions)
87156

discord_slash/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Discord Slash Constants"""
22

3-
__version__ = "2.2.0"
3+
__version__ = "2.3.2"
44

55
BASE_API = "https://discord.com/api/v8"

discord_slash/context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ def __init__(
321321
)
322322
self.component = self.origin_message.get_component(self.custom_id)
323323

324-
self.selected_options = _json["data"]["values"] if self.component_type == 3 else None
324+
self.selected_options = None
325+
326+
if self.component_type == 3:
327+
self.selected_options = _json["data"].get("values", [])
325328

326329
async def defer(self, hidden: bool = False, edit_origin: bool = False):
327330
"""

discord_slash/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,15 @@ async def _slash_edit(self, **fields):
549549

550550
allowed_mentions = fields.get("allowed_mentions")
551551
if allowed_mentions is not None:
552-
if self.bot.allowed_mentions is not None:
553-
_resp["allowed_mentions"] = self.bot.allowed_mentions.merge(
552+
if self._state.allowed_mentions is not None:
553+
_resp["allowed_mentions"] = self._state.allowed_mentions.merge(
554554
allowed_mentions
555555
).to_dict()
556556
else:
557557
_resp["allowed_mentions"] = allowed_mentions.to_dict()
558558
else:
559-
if self.bot.allowed_mentions is not None:
560-
_resp["allowed_mentions"] = self.bot.allowed_mentions.to_dict()
559+
if self._state.allowed_mentions is not None:
560+
_resp["allowed_mentions"] = self._state.allowed_mentions.to_dict()
561561
else:
562562
_resp["allowed_mentions"] = {}
563563

discord_slash/utils/manage_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ async def update_single_command_permissions(bot_id, bot_token, guild_id, command
184184
url = f"https://discord.com/api/v8/applications/{bot_id}/guilds/{guild_id}/commands/{command_id}/permissions"
185185
async with aiohttp.ClientSession() as session:
186186
async with session.put(
187-
url, headers={"Authorization": f"Bot {bot_token}"}, json=permissions
187+
url, headers={"Authorization": f"Bot {bot_token}"}, json={"permissions": permissions}
188188
) as resp:
189189
if resp.status == 429:
190190
_json = await resp.json()

discord_slash/utils/manage_components.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def create_select(
202202
placeholder: typing.Optional[str] = None,
203203
min_values: typing.Optional[int] = None,
204204
max_values: typing.Optional[int] = None,
205+
disabled: bool = False,
205206
):
206207
"""
207208
Creates a select (dropdown) component for use with the ``components`` field. Must be inside an ActionRow to be used (see :meth:`create_actionrow`).
@@ -211,6 +212,7 @@ def create_select(
211212
:param placeholder: Custom placeholder text if nothing is selected
212213
:param min_values: The minimum number of items that **must** be chosen
213214
:param max_values: The maximum number of items that **can** be chosen
215+
:param disabled: Disables this component. Defaults to ``False``.
214216
"""
215217
if not len(options) or len(options) > 25:
216218
raise IncorrectFormat("Options length should be between 1 and 25.")
@@ -222,6 +224,7 @@ def create_select(
222224
"placeholder": placeholder or "",
223225
"min_values": min_values,
224226
"max_values": max_values,
227+
"disabled": disabled,
225228
}
226229

227230

@@ -302,7 +305,7 @@ async def wait_for_component(
302305
custom_ids = list(get_components_ids(components)) if components else None
303306

304307
# automatically convert improper custom_ids
305-
if not all(isinstance(x, str) for x in custom_ids):
308+
if custom_ids and not all(isinstance(x, str) for x in custom_ids):
306309
custom_ids = [str(i) for i in custom_ids]
307310
logger.warning(
308311
"Custom_ids have been automatically converted to a list of strings. Please use lists of strings in future.\n"

docs/components.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ Each button gets a ``custom_id`` (which is always a string), this is a unique id
117117
What about selects / Dropdowns?
118118
_______________________________
119119

120-
Yep we support those too. You use them much the same as buttons:
120+
Yep we support those too. You use them much the same as buttons. You can only have 1 select per action row, but each select can have up to 25 options in it!
121121

122122
.. code-block:: python
123123
124-
from discord_slash.utils.manage_components import create_select, create_select_option
124+
from discord_slash.utils.manage_components import create_select, create_select_option, create_actionrow
125125
126126
select = create_select(
127127
options=[# the options in your dropdown
@@ -133,8 +133,10 @@ Yep we support those too. You use them much the same as buttons:
133133
min_values=1, # the minimum number of options a user must select
134134
max_values=2, # the maximum number of options a user can select
135135
)
136+
137+
await ctx.send("test", components=[create_actionrow(select)]) # like action row with buttons but without * in front of the variable
136138
137139
@bot.event
138140
async def on_component(ctx: ComponentContext):
139141
# ctx.selected_options is a list of all the values the user selected
140-
await ctx.send(content=f"You selected {ctx.selected_options}")
142+
await ctx.send(content=f"You selected {ctx.selected_options}")

0 commit comments

Comments
 (0)