Example Project

If creating your own bot from scratch seems a little scary, an example project is available to help you out!

Prerequisites

  • Latest version of Xcode (14.3 as of writing)

  • A Discord bot and its token - follow Creating a Discord Bot for detailed instructions on creating a bot in Discord's developer portal

Creating a Discord Bot

By following these instructions, you'll register your very own Discord bot. Obviously, these steps assume you have a Discord account.

  1. Go to Discord's Developer Portal

  2. Press the New Application button to start creating an application

  3. Enter a totally not overused name for your application (you can set a different name for your bot later), then hit the inviting Create button

  4. Go to the Bot section of your new application, and click Add Bot

  5. After your bot's created, you could change its Discord username (if required). We'll be keeping all other options at their defaults for now.

  6. Press the Reset Token button, enter your OTP if/when requested, and copy the token (as shown below). You might want to save it somewhere, or just keep it in your clipboard.

  7. In the sidebar, go to OAuth2 > URL Generator to get a URL to add your bot to a server. Select the bot scope and ensure (at least) the Send Messages permission is enabled. Refer to the screenshot below for the checkboxes that you'll need to check (many options have been removed to fit everything in one page).

    Visit the Generated URL and follow the authorisation flow to add the bot to a server.

  8. With the bot token and ID of the server you added the bot to, continue with the steps below

Cloning the Example Project

If you'd like an example to get you started, fret not! Simply fork the DiscordKitBotDemo repository by hitting the inviting Fork button on the top right. (The exampe project only works with Xcode right now, sorry!)

Then, in the Welcome to Xcode window, use the Clone an existing project action and provide the URL to your repository.

Configuration

Resist the urge to hit â–ļī¸ just yet, for you'll need to configure the project for your bot first!

  1. Create a new Configuration Settings File in the Xcode project and add the following line (as shown in the image below):

    TOKEN=<your-discord-token>

    Note: The name of the .xcconfig file doesn't matter, it'll work as long as it's added to your app's target.

  2. Ensure your bot is added to a server and minimally has the Send Messages permission

  3. Copy the ID of that server and set it as the value of the COMMAND_GUILD_ID environment variable in the DiscordKitDemo target

If you're using a version control system such as Git, ensure the .xcconfig file is never added to version control by adding an entry in .gitignore or similar. This ensures you never accidentally expose your token on GitHub or such. If you're using Git, you can do so by adding the following line to your .gitignore file (create one if it doesn't already exist):

*.xcconfig

Now that's all done, go ahead to build and run the project! If everything goes well, you'll see several slash commands registered in the server that you set in step 3. Go ahead and try them out, and you've just ran your first Swift Discord bot powered by DiscordKit!

Last updated