A client library for the Discord API written in Swift.
This project is a fork of nuclearace's SwiftDiscord
, which is no longer actively maintained as of 2023. Among other changes, the codebase has been refactored to employ modern Swift patterns, such as value types and Codable
, along with support for the v9 API.
A simple Discord bot that responds to every "ping" message with "pong" could be implemented as follows:
import Discord
import Dispatch
class PingPongBot: DiscordClientDelegate {
private var client: DiscordClient!
init() {
client = DiscordClient(token: "Bot <your token>", delegate: self)
client.connect()
}
func client(_ client: DiscordClient, didCreateMessage message: DiscordMessage) {
if message.content == "ping" {
client.sendMessage("pong", to: message.channelId)
}
}
}
let bot = PingPongBot()
dispatchMain()
You can run this example (which is provided as a snippet) with
swift run PingPongBot <your token>
Check out the docs for more detailed information about the API.
- macOS and Linux support
- v9 API (including threads, interactions, slash commands and message components)
- Configurable sharding
- Swift 5.7+
swift build