Skip to content

The Discord API implementation behind Swiftcord, implemented completely from scratch in Swift

Notifications You must be signed in to change notification settings

SwiftcordApp/DiscordKit

Repository files navigation

DiscordKit

Package for interacting with Discord's API to build Swift bots

DiscordKit for Bots is now released! Use DiscordKit to create that bot you've been looking to make, in the Swift that you know and love!

About

DiscordKit is a Swift package for creating Discord bots in Swift.

If DiscordKit has helped you, please give it a ⭐ star and consider sponsoring! It keeps me motivated to continue developing this and other projects.

Installation

Swift Package Manager (SPM):

Package.swift

Add the following to your Package.swift:

.package(url: "https://github.com/SwiftcordApp/DiscordKit", branch: "main")
Xcode Project

Add a package dependancy in your Xcode project with the following parameters:

Package URL:

https://github.com/SwiftcordApp/DiscordKit

Branch:

main

Product:

  • DiscordKitBot

For more detailed instructions, refer to this page in the DiscordKit guide.

Example Usage

Create a simple bot with a /ping command:

import DiscordKitBot

let bot = Client(intents: .unprivileged)

// Guild to register commands in. If the COMMAND_GUILD_ID environment variable is set, commands are scoped
// to that server and update instantly, useful for debugging. Otherwise, they are registered globally.
let commandGuildID = ProcessInfo.processInfo.environment["COMMAND_GUILD_ID"]

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

    try? await bot.registerApplicationCommands(guild: commandGuildID) {
        NewAppCommand("ping", description: "Ping me!") { interaction in
            try? await interaction.reply("Pong!")
        }
    }
}

bot.login() // Reads the bot token from the DISCORD_TOKEN environment variable and logs in with the token

// Run the main RunLoop to prevent the program from exiting
RunLoop.main.run()

(Yes, that's really the whole code, no messing with registering commands with the REST API or anything!)

Not sure what to do next? Check out the guide below, which walks you through all the steps to create your own Discord bot!

Resources

Here are some (WIP) resources that might be useful while developing with DiscordKit.

Platform Support

Currently, DiscordKit only offically supports macOS versions 11 and up. Theoretically, you should be able to compile and use DiscordKit on any Apple platform with equivalent APIs, however this has not been tested and is considered an unsupported setup.

DiscordKitBot and DiscordKitCore is supported on Linux, but not DiscordKit itself. However, you are able to develop and host bots made with DiscordKit on Linux.

Windows is not supported natively at the moment. The recommended method is to use Windows Subsystem for Linux to do any development/hosting of DiscordKit bots on Windows. Native support may come in the future.