DiscordKit Guide
GitHub Repository
  • 👋Welcome
  • 🚀Getting Started
    • Creating a Project
      • Creating a bot using a SPM project (Recommended)
      • Creating a bot using an Xcode Project
    • Installation
    • Example Project
  • ðŸĪ–Creating a Bot
    • Getting off the Ground
    • Logging In
    • Registering Commands
    • Responding to Commands
    • Adding Command Options
    • Deploying on Linux
  • ðŸŠĶLegacy
    • Message Commands
  • âœĻEnhancements
    • Logging
  • ðŸ“ĶNitty-gritty
    • What's in the box?
    • Gateway
    • Event Dispatching
Powered by GitBook
On this page
  1. Creating a Bot

Logging In

The most crucial step - logging in to Discord's bot API

PreviousGetting off the GroundNextRegistering Commands

Last updated 2 years ago

The first step to talk to Discord is always logging in.

In your Bot.swift (or equivalent) file, you'll need to add the following line to your main() function:

// Login to Discord's API
bot.login()

// Run the main RunLoop to prevent the program from exiting
// If this line throws a compile error for you, make sure you imported Foundation!

As simple as that: if you run your app now (assuming you've added your token correctly), it'll attempt to connect to the Discord Gateway and set the REST API handler, ready for API requests. You should see something like the following at the bottom of your console:

However, most of the time you'd like to listen to some events, which can be done by adding the code below to your main() function, before the call to bot.login():

bot.ready.listen {
    print("Successfully logged in as \(bot.user!.username)#\(bot.user!.discriminator)!")
}

Rerun your app, and you should see that message printed out in the console shortly after!

ðŸĪ–
GitHub - SwiftcordApp/DiscordKitGettingStarted at logging-inGitHub
Use this branch as a reference for how your project should look!
The sweet taste of success
Logo