Skip to content
quefep edited this page Nov 15, 2025 · 2 revisions

SwiftDisc Wiki

A modern, Swift-native Discord API library for building powerful bots with async/await, type-safe models, and production-ready features.


Quick Links


Installation (Swift Package Manager)

Add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/M1tsumi/SwiftDisc.git", from: "0.10.0")
]

targets: [
    .target(name: "YourBot", dependencies: ["SwiftDisc"]) 
]

Minimums: iOS 14, macOS 11, tvOS 14, watchOS 7, Windows (Swift 5.9+).


Quick Start

import SwiftDisc

@main
struct MyFirstBot {
    static func main() async {
        let token = ProcessInfo.processInfo.environment["DISCORD_BOT_TOKEN"] ?? ""
        let client = DiscordClient(token: token)
        do {
            try await client.loginAndConnect(intents: [.guilds, .guildMessages, .messageContent])
            for await event in client.events {
                switch event {
                case .ready(let info):
                    print("✅ Bot is online as \(info.user.username)!")
                case .messageCreate(let msg) where msg.content == "!hello":
                    try await client.sendMessage(channelId: msg.channel_id, content: "Hello!")
                default:
                    break
                }
            }
        } catch {
            print("SwiftDisc error: \(error)")
        }
    }
}

Tips:

  • Enable necessary gateway intents in the Discord Developer Portal (e.g. MESSAGE_CONTENT).
  • Never hardcode tokens; prefer environment variables.

Key Features

  • Gateway: Async event stream, heartbeats, resume, sharding.
  • REST: Channels, messages, guilds, roles, webhooks, slash commands.
  • DX: Type-safe APIs, clear errors, helpful utilities (embeds, components).
  • Extras: Autocomplete, file uploads, caching, permissions utilities.

Examples

  • Ping Bot: Basic message handling
  • Commands Bot: Prefix routing and help
  • Slash Bot: Modern interactions
  • File Upload Bot: Multipart uploads with embeds

Browse: https://github.com/M1tsumi/SwiftDisc/tree/main/Examples


Help & Community


Contributing

  • Open an issue for major changes before a PR.
  • Follow Swift API Design Guidelines.
  • Include tests when possible.

License: MIT

Cross-Platform

  • Custom WebSocket adapter path for Windows compatibility (URLSession-based adapter used across platforms)
  • Continuous integration for macOS and Windows
  • Platform-specific optimizations

Features

  • Embeds support in message sending and interaction responses
  • Minimal slash commands: create global/guild commands and reply to interactions

Deployment

Build & Run

  • Server/CLI: Build with SwiftPM; run with environment variable DISCORD_TOKEN
  • Use a process supervisor (systemd, launchd, PM2, or Docker) to keep the bot running

Configuration

  • Set required intents in the Discord Developer Portal
  • Configure shard count if you operate at scale; use ShardManager

Environment

  • Enable logging (stdout/stderr) and retain logs
  • Ensure outbound network access to Discord domains; time sync is recommended

Scaling

  • Horizontal: Multiple processes with shard ranges
  • Respect REST and gateway limits; avoid per-second spikes

Secrets

  • Never commit tokens. Use environment variables or secret stores (Keychain/KeyVault/Parameter Store)

CI/CD

  • Use GitHub Actions CI provided (build/test/coverage)
  • Add a deploy job to your infrastructure

Security Best Practices

  • Use environment variables for bot tokens
  • Leverage secure storage on device platforms (Keychain, credential managers)
  • Follow Discord's developer policies
  • Be mindful when requesting privileged intents

Community & Support

  • Discord Server: Join the community for support, announcements, and discussions
  • GitHub Issues: Report bugs and request features
  • GitHub Discussions: Ask questions and share your projects

Contributing

Contributions are welcome! Please read the contributing guidelines before submitting pull requests.

Acknowledgments

SwiftDisc adapts proven patterns from discord.py (BSD-licensed), implementing them idiomatically for Swift. Key adaptations include intents, event dispatch, and rate limiting strategies.

Versioning

This project follows Semantic Versioning. See CHANGELOG.md for detailed release notes.

License

SwiftDisc is released under the MIT License. See the LICENSE file for details.


Built with ❤️ for the Swift and Discord communities

Repository: https://github.com/M1tsumi/SwiftDisc