DiscordKit

1.0.0

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

What's New

Finally, bot support!

2023-05-10T16:57:10Z

DiscordKit for Bots: The missing Discord framework for bots you've been waiting for!

Now, you can build Discord bots in Swift with elegance, with beautiful and clean slash command builders and a well thought out API! Built on a solid battle-tested core also used in Swiftcord, DiscordKit's bot support has robust rock-solid performance even in the face of network instabilities and other unexpected conditions.

Although I've tried to consider as many common use cases, DiscordKit for Bots is still in early phases of support and might not have good support for more advanced uses. Contributions are welcome! <3

This is DiscordKit's first stable release, and as such there isn't a previous release to compare against. Here are some features of note:

  • Beautiful SwiftUI-like slash command builders, including modifiers and execution callbacks:

    NewAppCommand("hello", description: "Get a nice hello message") { interaction in
        print("Received hello interaction!")
        try? await interaction.reply("Hello there!")
    }

    produces the following (operational) command:

    image

    Register and respond to commands in one place!

  • Battle-tested WebSocket gateway reconnection logic, with zlib-stream compression support

  • Well-documented and well-though-out (bot) API

Get up to speed quick with the guide

Coming Soon:

  • Button component support
  • Image/attachment up/downloading

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.

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.

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

Last updated: Tue Dec 17 2024 07:28:45 GMT-1000 (Hawaii-Aleutian Standard Time)