<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cedric</title>
    <description>The latest articles on DEV Community by Cedric (@cedricmkl).</description>
    <link>https://dev.to/cedricmkl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F805941%2F59725c3e-1e22-423f-9b7b-40f2386e76d3.jpg</url>
      <title>DEV Community: Cedric</title>
      <link>https://dev.to/cedricmkl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cedricmkl"/>
    <language>en</language>
    <item>
      <title>Register Discord Slash Commands via the Discord REST API</title>
      <dc:creator>Cedric</dc:creator>
      <pubDate>Thu, 28 Jul 2022 20:39:00 +0000</pubDate>
      <link>https://dev.to/cedricmkl/register-discord-slash-commands-via-the-discord-rest-api-3l0f</link>
      <guid>https://dev.to/cedricmkl/register-discord-slash-commands-via-the-discord-rest-api-3l0f</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you've built a small Discord bot and don't want to register commands through your bot code, you can easily register them manually through the Rest API.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Firstly, you need an application to make requests from. I personally use &lt;a href="https://hoppscotch.io/" rel="noopener noreferrer"&gt;Hoppscotch&lt;/a&gt;. Because it's a web application you don't have to install anything. You can also save the requests for later use, which is pretty useful in this case. Of course you can also use a CLI tool like curl. I will use &lt;a href="https://hoppscotch.io/" rel="noopener noreferrer"&gt;Hoppscotch&lt;/a&gt;in this demo.&lt;/p&gt;

&lt;p&gt;First, let's start with the Guild Application Commands. According to &lt;a href="https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-guild-application-commands" rel="noopener noreferrer"&gt;Discord's documentation&lt;/a&gt;, to bulk overwrite the commands, we need to send a PUT request to &lt;code&gt;/applications/{application id}/guilds/{guild}/commands&lt;/code&gt; with an array of commands as the body. For test purposes I use this simple payload with just one command. If you want to do something more complicated, take a look &lt;a href="https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-guild-application-commands-bulk-application-command-json-params" rel="noopener noreferrer"&gt;at this table&lt;/a&gt; in Discord's documentation. This will create a slash Command called test. Keep in mind that there are other types of commands, like message or user commands. If you want to create these, you have to set the type property, the default is set to slash commands so we don't have to set this property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"This is a test command!"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the guild and application id in the url with your application data from the dev portal and put &lt;code&gt;https://discord.com/api/v10&lt;/code&gt; in the front (v10 is the api version, as of writing this, the latest is v10). You should get something like this: &lt;code&gt;https://discord.com/api/v10/applications/885151175224201237/guilds/828931295592710195/commands&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, put it all into Hoppscotch or the tool you're using to create web requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp65hh5c0tdlvfpze502f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp65hh5c0tdlvfpze502f.png" width="800" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, now we've only one thing left. Authorization. Grab your Bot's token and go to the "Authorization" in Hoppscotch. Set Authorization Type to API Key and Pass by to Headers to send your token via the request's headers to discord. Set Authorization as the key and Bot YOUR_BOT_TOKEN as the value.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bz8cheho5aqpkxpqagx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bz8cheho5aqpkxpqagx.png" alt="Image description" width="769" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, set the request method in the top left from GET to PUT and press send. If you got a &lt;code&gt;200 OK&lt;/code&gt; and a Json response body with the data of the commands, everything went well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7t7hko3jyt8w0lsurrjm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7t7hko3jyt8w0lsurrjm.png" width="487" height="388"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;If you want to create Global Application Commands, just remove &lt;code&gt;/guilds/{guild id}&lt;/code&gt; from the url and you should be fine.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
