DisCatSharp

repository·main·Indexed 40 days ago

https://github.com/aiko-it-systems/discatsharp

A C# library for .NET designed for building Discord applications, providing high-level abstractions to interact with the Discord API. It features a modular ecosystem including packages for application commands, voice and audio (Lavalink), .NET Generic Host integration, and ASP.NET Core ingress.

Tokens
123.3K
Snippets
291
Records
488
Agent score
49%

What's inside DisCatSharp

  1. Explore the DisCatSharp API packages

    main

    DisCatSharp is modularized into several specialized packages. Depending on your requirements (e.g., hosting, voice, or command handling), you should include the relevant package in your project.

    Core Packages

    • DisCatSharp: The primary library for Discord interaction.
    • DisCatSharp.ApplicationCommands: Support for Discord's modern Application Commands (Slash Commands).
    • DisCatSharp.CommandsNext: A command handling framework for DisCatSharp.
    • DisCatSharp.Interactivity: Tools for handling user interactions (e.g., buttons, select menus, modals).
  2. Planned roadmap for DisCatSharp

    main

    The following major updates are planned for future versions:

    • DisCatSharp.Voice: A replacement for DisCatSharp.VoiceNext designed for better performance, stability, and the ability to receive incoming audio.
    • Caching System Overhaul: A complete rewrite to increase reliability, performance, and storage flexibility.
    • DisCatSharp.CommandNext Revamp: A proposed complete rewrite for improved maintainability and usability.
    • Audit Log Enhancement: A revamp of the Discord audit log API implementation to improve ease-of-use.
    • Signed Attachments: Re-introduction of signed attachment entities.
  3. Locate DisCatSharp library components

    main

    When navigating the DisCatSharp repository to find specific functionality, use the following mapping to identify the correct project or directory:

    AreaTypical location
    Core clients, entities, REST, GatewayDisCatSharp/
    Slash/context commandsDisCatSharp.ApplicationCommands/
    Prefix commandsDisCatSharp.CommandsNext/
    Shared primitivesDisCatSharp.Common/
    Configuration packageDisCatSharp.Configuration/
    Experimental APIsDisCatSharp.Experimental/
    Hosting and DIDisCatSharp.Hosting*/
    Interaction helpersDisCatSharp.Interactivity/
    LavalinkDisCatSharp.Lavalink/
    Voice and native packagingDisCatSharp.Voice*/
    Shared targets and versionsDisCatSharp.Targets/
    Analyzers and repository toolsDisCatSharp.Tools/
    TestsDisCatSharp.Tests/ and tool-specific test projects
    DocFX source and templateDisCatSharp.Docs/
    Workflows and repository automation.github/

    Note: Always use repository search to verify if a path is current before assuming its location.

  4. Overview of DisCatSharp Templates

    main

    DisCatSharp provides three main types of templates to scaffold your application:

    • Bot Template: A class library designed for bot-related code. It includes a JSON configuration file for bot settings. It uses an extension class to simplify adding bot services and configuration to the Dependency Injection (DI) pipeline.
    • Web Template: A minimal web project containing a default "Hello World" endpoint.
    • Solution Template: A combined template that includes both the Bot and Web templates. It sets up the necessary references and calls to get the bot running with minimal initial effort.
  5. What is Lavalink Voice Bridge Mode?

    main

    In standard mode, Lavalink handles both audio decoding and Discord voice transport (via the Koe library). This means the bot cannot receive voice while playing, and the DisCatSharp.Voice stack is bypassed.

    Bridge Mode splits these responsibilities:

    • Lavalink: Decodes audio and exports Opus frames.
    • DisCatSharp.Voice: Handles the Discord voice protocol (RTP, DAVE E2EE, AEAD encryption, and UDP).
    • DisCatSharp.Lavalink: Coordinates both components.

    Use Bridge Mode if your bot needs to perform voice tasks (like receiving audio or mixing) while playing music, or if you want to leverage the full DisCatSharp.Voice transport stack.

  6. What is sharding and when should I use it?

    main

    Sharding is the process of splitting the event workload of a DiscordClient into multiple connections, called shards. Each shard handles a specific set of Discord servers (guilds) and only receives events from those servers.

    Key behaviors:

    • Each shard is responsible for a subset of guilds.
    • All direct messages (DMs) are handled by your first shard.

    When to shard:

    • Recommended: When your bot reaches 1,000 servers.
    • Required: When your bot reaches 2,500 servers.
  7. How DisCatSharp handles OAuth2

    main

    DisCatSharp's OAuth2 implementation is split into two distinct layers to separate the Discord protocol logic from HTTP hosting concerns:

    1. DiscordOAuth2Client (Protocol Client): Handles the core OAuth2 protocol logic. Use this for generating authorization URLs, validating state, exchanging codes, refreshing/revoking tokens, and calling Discord's OAuth-protected endpoints.
    2. DisCatSharp.Hosting.AspNetCore (HTTP Ingress & Hosting): Handles the web-facing infrastructure. Use this to receive OAuth callbacks, Discord interactions, or signed Discord webhook events within an ASP.NET Core environment.

    Note: DiscordOAuth2Client is not deprecated. The deprecated component is DisCatSharp.Extensions.OAuth2Web.

  8. Understand the DisCatSharp 10.7.0 Configuration Layout

    main

    Starting with DisCatSharp 10.7.0, the DiscordConfiguration object uses a nested hierarchical layout. Instead of a flat list of properties, settings are grouped into specialized sub-objects (e.g., Api, Gateway, Rest, Cache, Logging). This structure helps organize the 40+ available settings into logical domains.

    Configuration Hierarchy

    DiscordConfiguration
    ├── Token / TokenType / Intents / ServiceProvider / Proxy
    ├── Api            → API protocol settings
    ├── Gateway        → Connection, sharding, compression
    │   └── Advanced   → Dispatch mode, reconnect tuning
    ├── Rest           → HTTP timeout, rate-limit strategy
    │   └── Advanced   → Queue limits, retries, circuit breaker
    ├── Cache          → Message/presence cache, auto-fetch
    ├── Logging        → Log level, timestamp format, logger factory
    ├── Diagnostics    → Payload events, update checks
    │   └── UpdateChecks → Version check mode and options
    └── Telemetry      → Sentry error reporting
  9. Create translation JSON for Slash Command Groups

    main

    When your command structure uses nested SlashCommandGroup attributes, your translation JSON must be an array of [Command Group Objects]. Each object maps a name to its translations and contains a commands array for the subcommands within that group.

    Key fields:

    • name: The internal name of the group.
    • type: Must be 1 for slash command groups.
    • name_translations: An object mapping locale codes (e.g., en-US, de) to the translated name.
    • description_translations: An object mapping locale codes to the translated description.
    • commands: An array of command objects belonging to this group.
    [
    	{
    		"name": "my_command",
    		"description": "This is description of the command group.",
    		"type": 1,
    		"name_translations": {
    			"en-US": "my_command",
    			"de": "mein_befehl"
    		},
    		"description_translations": {
    			"en-US": "This is description of the command group.",
    			"de": "Das ist die description der Befehl Gruppe."
    		},
    		"groups": [],
    		"commands": [
    			{
    				"name": "first",
    				"description": "First",
    				"name_translations": {
    					"en-US": "first",
    					"de": "erste"
    				},
    				"description_translations": {
    					"en-US": "This is description of the command.",
    					"de": "Das ist die Beschreibung des Befehls."
    				}
    			}
    		]
    	}
    ]