Logging In

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

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!

Last updated