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!
DiscordKit is a Swift package for creating Discord bots in Swift.
If DiscordKit has helped you, please give it a ⭐ star, or consider sponsoring! It keeps me motivated to continue developing this and other projects.
Package.swift
Add the following to your Package.swift
:
.package(url: "https://github.com/SwiftcordApp/DiscordKit", branch: "bot-support")
Xcode Project
Add a package dependancy in your Xcode project with the following parameters:
Package URL:
https://github.com/SwiftcordApp/DiscordKit
Branch:
bot-support
Product:
- DiscordKitBot
For more detailed instructions, refer to this page in the DiscordKit guide.
Create a simple bot with a /ping command:
import DiscordKitBot
let bot = Client(intents: .unprivileged)
bot.ready.listen {
print("Logged in as \(bot.user!.username)#\(bot.user!.discriminator)!")
print("Started refreshing application (/) commands.")
try? await bot.registerApplicationCommands(guild: ProcessInfo.processInfo.environment["COMMAND_GUILD_ID"]) {
NewAppCommand("ping", description: "Ping me!") { interaction in
try? await interaction.reply("Pong!")
}
}
print("Successfully reloaded application (/) commands.")
}
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!
Here are some (WIP) resources that might be useful while developing with DiscordKit.
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.
Linux and Windows is not supported at the moment, primarily due to our reliance on Apple's Combine
framework. We have not blocked building DiscordKit on other platforms in the event that support for those frameworks is added to Swift's corelibs in the future.
Linux support is planned, and will arrive sometime in the future. Unfortunately, we do not have a timeline for that at the moment.