|
| 1 | +Quickstart |
| 2 | +========== |
| 3 | + |
| 4 | +Before doing anything, I highly recommend to read discord.py's quickstart. |
| 5 | +You can find it by clicking :ref:`this <discord:quickstart>`. |
| 6 | + |
| 7 | +Let's use this extension real quick. |
| 8 | + |
| 9 | +First of all, let's install this extension. |
| 10 | + |
| 11 | +.. code-block:: |
| 12 | +
|
| 13 | + pip install -U discord-py-slash-command |
| 14 | +
|
| 15 | +Now, let's create a simple bot. Create one Python code file. |
| 16 | +I'll use ``main.py``. |
| 17 | + |
| 18 | +Then, write like this: |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | +
|
| 22 | + import discord |
| 23 | + from discord_slash import SlashCommand |
| 24 | +
|
| 25 | + client = discord.Client(intents=discord.Intents.all()) |
| 26 | + slash = SlashCommand(client, auto_register=True) |
| 27 | +
|
| 28 | + @client.event |
| 29 | + async def on_ready(): |
| 30 | + print("Ready!") |
| 31 | +
|
| 32 | + client.run("your_bot_token_here") |
| 33 | +
|
| 34 | +1. 1st and 2nd line is importing library and extension. |
| 35 | +2. Then, creates client instance and extension instance. |
| 36 | +3. Finally, runs our bot. |
| 37 | + |
| 38 | +Well, now this will run but do literally nothing except printing ``Ready!``. |
| 39 | + |
| 40 | +Let's add one simple command. |
| 41 | + |
| 42 | +Fix your source code like this: |
| 43 | + |
| 44 | +.. code-block:: python |
| 45 | +
|
| 46 | + import discord |
| 47 | + from discord_slash import SlashCommand |
| 48 | +
|
| 49 | + client = discord.Client(intents=discord.Intents.all()) |
| 50 | + slash = SlashCommand(client, auto_register=True) |
| 51 | +
|
| 52 | + guild_ids = [789032594456576001] # Put your testing server ID. |
| 53 | +
|
| 54 | + @client.event |
| 55 | + async def on_ready(): |
| 56 | + print("Ready!") |
| 57 | +
|
| 58 | + @slash.slash(name="ping", guild_ids=guild_ids) |
| 59 | + async def _ping(ctx): |
| 60 | + await ctx.send(content=f"Pong! ({client.latency*1000}ms)") |
| 61 | +
|
| 62 | + client.run("your_bot_token_here") |
| 63 | +
|
| 64 | +I'll explain what is changed: |
| 65 | + |
| 66 | +- ``guild_ids = [789032594456576001]``: This is for adding your command as a guild command. Otherwise, you need to wait for an hour to wait until your command is added. |
| 67 | +- ``@slash.slash(name="ping"...)`` ~ ``await ctx.send(...)``: This adds new slash command. This command basically sends an API latency. |
| 68 | + |
| 69 | +Let's do some more complicated things: |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + import discord |
| 74 | + from discord_slash import SlashCommand |
| 75 | + from discord_slash.utils import manage_commands |
| 76 | +
|
| 77 | + client = discord.Client(intents=discord.Intents.all()) |
| 78 | + slash = SlashCommand(client, auto_register=True) |
| 79 | +
|
| 80 | + guild_ids = [789032594456576001] # Put your testing server ID. |
| 81 | +
|
| 82 | + @client.event |
| 83 | + async def on_ready(): |
| 84 | + print("Ready!") |
| 85 | +
|
| 86 | + @slash.slash(name="ping", guild_ids=guild_ids) |
| 87 | + async def _ping(ctx): |
| 88 | + await ctx.send(content=f"Pong! ({client.latency*1000}ms)") |
| 89 | +
|
| 90 | + @slash.slash(name="echo", guild_ids=guild_ids, options=[manage_commands.create_option("string", "A random string.", 3, True)]) |
| 91 | + async def _echo(ctx, string): |
| 92 | + await ctx.send(content=string) |
| 93 | +
|
| 94 | + client.run("your_bot_token_here") |
| 95 | +
|
| 96 | +Again, I'll explain what is changed: |
| 97 | + |
| 98 | +- ``from discord_slash.utils import manage_commands``: This imports utility module of this extension. |
| 99 | +- ``@slash.slash(name="echo"...)`` ~ ``await ctx.send(...)``: This adds another slash command. This command echos what you typed at ``string``. |
| 100 | + |
| 101 | +What you need to see is ``options``. Unlike discord.py's ext.commands, slash command requires extra effort to add args. |
| 102 | +Hopefully, this extension comes with utility function that helps this, which is ``manage_commands.create_option``. |
| 103 | +Any other steps are automatically processed by the extension, since we set ``auto_register=True``. |
0 commit comments