> For the complete documentation index, see [llms.txt](https://docs.ggwp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ggwp.com/ggwp-client-sdk/additional-supported-integrations/chat-discord-social-sdk.md).

# Chat: Discord Social SDK

*This document covers different approaches for integrating GGWP with Discord Social SDK for Chat services.*&#x20;

There are three ways to integrate the GGWP Chat API with a game using the Discord Social SDK.

**Note:** Moderation capabilities in approaches (1) and (2) are limited (details below), while approach (3) provides full moderation support.

## Approach 1: Discord Webhooks - Direct Integration

1. Login to your Discord Developer Portal and go to the [Applications page](https://discord.com/developers/applications)

   <figure><img src="/files/ncn3AEwVwbAbLlTRINYI" alt=""><figcaption></figcaption></figure>
2. Select the application being used in your Social SDK integration

   <figure><img src="/files/lDwW2Wm5lBYEvjl5XI5L" alt=""><figcaption></figcaption></figure>
3. From the `General Information` option under `Settings`, copy the `APPLICATION ID` and `PUBLIC KEY` and share with your GGWP account manager.

   <figure><img src="/files/BY4FaxNSuO7XQNJAi85h" alt=""><figcaption></figcaption></figure>
4. GGWP Team would configure your app in its system and share an `ENDPOINT URL`
5. Select `Webhooks` under `Settings` on the left and put the URL shared by GGWP in `ENDPOINT URL` field

   <figure><img src="/files/ShQ7mGrJhaJ6YFzzsXcX" alt=""><figcaption></figcaption></figure>
6. Toggle `Events` switch to enabled state and check `Lobby Message Create`&#x20;

   <figure><img src="/files/FUeU7156swsdfnFeBnxo" alt=""><figcaption></figcaption></figure>

### **Salient Points**

1. Zero code integration for data-ingestion!
2. Customizing [filter strength](https://docs.ggwp.com/api-docs-chat/standard-package/chat-api#headers) by room, group, or community is not supported. GGWP Chat services would use the global [content filter configuration](https://docs.ggwp.com/dashboard-user-guide/admin-features#content-filter-configuration).
3. Toxic messages cannot be deleted as Discord Social SDK currently supports [moderation on the client only](https://discord.com/developers/docs/discord-social-sdk/how-to/integrate-moderation).
4. Once GGWP Automod is activated, the user sanctions can be enforced via GGWP webhooks, but you would be responsible for handling communication on the game client and applying the appropriate actions.

### **Message Flow**

<figure><img src="/files/zqX8ysJQLUjlglGmpl2b" alt=""><figcaption></figcaption></figure>

## Approach 2: Discord Webhooks - Self Managed

1. Follow the Discord [Webhook Events guide](https://discord.com/developers/docs/events/webhook-events) to implement the webhook in your server.
2. Upon receipt of the message via Discord webhook call [GGWP Chat API](https://docs.ggwp.com/api-docs-chat/standard-package/chat-api) using the GGWP shared API key.

### **Salient Points**

1. Since you’re calling the Chat API directly, you can set the filter strength for each room, group, or community using attributes like lobby ID or channel ID. If you don’t specify one, the global [content filter settings](https://docs.ggwp.com/dashboard-user-guide/admin-features#content-filter-configuration) would be used.
2. Toxic messages cannot be deleted as Discord Social SDK currently supports [moderation on the client only](https://discord.com/developers/docs/discord-social-sdk/how-to/integrate-moderation).
3. Once GGWP Automod is activated, the user sanctions can be enforced via GGWP webhooks, but you would be responsible for handling communication on the game client and applying the appropriate actions.

### Message Flow

<figure><img src="/files/GHmWs14R1H29e5HxI9Ck" alt=""><figcaption></figcaption></figure>

## Approach 3: Using GGWP SDK

1. Requires client side integration. Once SDK initialization is done, it will take care of,
   1. Secure auth using short-lived dynamic tokens
   2. Sending the messages to GGWP for moderation
   3. Preventing the toxic message to appear in Social SDK
2. Requires building an API on your server to handle GGWP SDK authentication

   This API will communicate with the GGWP server using the `x-api-key` for authentication. In response, you’ll receive an auth code, which is valid for 5 minutes and can be exchanged for a temporary access token.

### **Salient Points**

1. Filter strength can be controlled on the client side for any given room, group, or community.
2. Prevents toxic messages to appear
3. Once GGWP Automod is activated, user sanctions can be handled and enforced via the SDK directly

### Message Flow

<figure><img src="/files/euIj9FJbOzADihxPZ3El" alt=""><figcaption></figcaption></figure>

### Integration Guide - Unity

Before integrating the SDK please ensure your services server is setup to support authentication via [Dynamic Tokens](/ggwp-client-sdk/authentications.md#dynamic-tokens).

GGWP's Chat Product is fully supported via the client SDK. Once SDK initialisation is done on the client, the Chat API can be accessed as below:

```csharp
GGWPSDK.Instance?.InitializeSDK("user_id", "username");

if (GGWPSDK.Instance?.ChatAPI != null)
{
    // Chat API is ready to use
}
```

There are two aspects to understand under this topic:

1. Message Replacement and Filter Strength Configuration - Deciding how the message should be evaluate and what should be done with the results.
2. Calling the Chat service - Actually sending the message to GGWP's server

#### Message Replacement and Filter Strength Configuration

**Setting Filter Strength**

Filter strength can be set for the message being sent to GGWP to decide how harshly/softly the evaluation should happen. Configuring filter strength is **fully optional**.  If you don't set it, the defaults will be used automatically. The defaults can be [updated using the dashboard](/dashboard-user-guide/admin-features.md#content-filter-configuration).

[Here ](/api-docs-chat/standard-package/chat-api-v2-legacy.md#headers)is the list of available options offered by GGWP. On the client it can be set by using `SetSanitizationConfig` .&#x20;

**Message Replacement Strategies**

When a message is processed by the GGWP service, it returns multiple versions of the message in the response. The `messageReplacementConfig` setting determines which version is used by the SDK when you call `ReplaceMessage`.&#x20;

Following versions of the message are available along with the identifiers to use while setting up the replacement strategy in the client

* **original\_message**
  * Definition: The raw message sent by the user.
  * Identifier: `UseOriginal`
* **filtered\_message**
  * Definition: Version of the message with flagged terms filtered.
    * For space-delimited languages (e.g., English), entire words are filtered. E.g. `You are a shithead` --> `You are a *****`
    * For languages without explicit word boundaries (e.g., Korean, Japanese, Chinese), only the detected term itself is filtered." E.g. `좆까고 있네`--> `*****고 있네`
  * (Default) Identifier: `UseFiltered`
* **replaced\_message**
  * Definition: Safe or humorous alternative generated to replace the original message.
  * Identifier:  `UseReplaced`
* **recommended\_message**
  * Definition: Suggested version of the message to be passed to the client. For active users, this value defaults to `filtered_message`. For muted users, the `recommended_message` will be an empty string.
  * Identifier: `UseRecommended`

There is an additional strategy - `DropMessage` which will ensure nothing gets sent upstream to the Discord Social SDK. Use this if you do not want any message to be sent to Discord Social if GGWP determines the message to be toxic.

Following code snippet shows setting up message replacement strategy and filter strengths:

```csharp
using GGWP.Chat;

GGWPSDK.Instance?.ChatAPI.SetSanitizationConfig(new SanitizationConfigUpdate
{
    client = new GGWP.Chat.Impl.SanitizationClientConfig
    {
        // Choose which message version to use when calling ReplaceMessage
        messageReplacementConfig = ReplacementStrategy.UseFiltered  // Default: uses filtered_message
        // messageReplacementConfig = ReplacementStrategy.UseReplaced  // Uses replaced_message
        // messageReplacementConfig = ReplacementStrategy.UseRecommended  // Uses recommended_message
        // messageReplacementConfig = ReplacementStrategy.UseOriginal  // Uses original_message
        // messageReplacementConfig = ReplacementStrategy.DropMessage  // Drops the message altogether
    },
    service = new GGWP.Chat.Impl.SanitizationServiceConfig
    {
        violence = ConfigOptionSetting.High,
        sexualContent = ConfigOptionSetting.High,
        verbalAbuse = ConfigOptionSetting.High,
        identityHate = ConfigOptionSetting.High,
        profanity = ConfigOptionSetting.High,
        linkSharing = ConfigOptionSetting.High,
        drugs = ConfigOptionToggle.On,
        spam = ConfigOptionToggle.On,
        selfHarm = ConfigOptionToggle.Off,
        pii = ConfigOptionToggle.On,
        replace = ConfigOptionToggle.On,
    }
});
```

**How Replacement Strategies Affect Message Handling**

The replacement strategy you choose affects what message text is returned when you call `ReplaceMessage`. Here's a practical example showing how the same offensive input would be handled differently based on your chosen strategy:

```csharp
// Original user input
string userMessage = "you stupid idiot go away";

// When you call ReplaceMessage, the returned message depends on your strategy:
var replaced = await GGWPSDK.Instance?.ChatAPI.ReplaceMessage(
    new ChatMessage { message = userMessage }
);

// With UseFiltered (default): "you ****** ***** go away"
// With UseReplaced: "you silly person go away"  
// With UseRecommended: The service's AI chooses the best version for this specific message
// With UseOriginal: "you stupid idiot go away"
```

The strategy you select applies automatically whenever you use `ReplaceMessage`, so you don't need to specify it each time. You can change the strategy at any point by calling `SetSanitizationConfig` again with a new `messageReplacementConfig` value.

**Retrieving Current Configuration**

Get the current sanitization configuration, including your selected replacement strategy and service defaults:

```csharp
var config = await GGWPSDK.Instance?.ChatAPI.GetSanitizationConfig();

// Access client-side config to see which replacement strategy is active
var replacementStrategy = config.client.messageReplacementConfig;
Debug.Log($"Current strategy: {replacementStrategy}");

// Access service-side config
if (config.service != null)
{
    var spamSetting = config.service.Value.spam;
    var selfHarmSetting = config.service.Value.selfHarm;
}
```

#### Calling GGWP's Chat Service from Client

**Sanitizing Messages**

Sanitize a message to check for toxic content. This method always returns all versions of the message regardless of your replacement strategy setting:

```csharp
using GGWP.Chat;

string userMessage = "User's message here";

var sanitized = await GGWPSDK.Instance?.ChatAPI.SanitizeMessage(
    new ChatMessage { message = userMessage }
);

// You can access all versions to see how the message was processed
string original = sanitized.messageDetails.originalMessage;
string filtered = sanitized.messageDetails.filteredMessage;
string replaced = sanitized.messageDetails.replacedMessage;

Debug.Log($"Original: {original}");
Debug.Log($"Filtered: {filtered}");
Debug.Log($"Replaced: {replaced}");
```

**Subscribing to Sanitized Messages**

Monitor all sanitized messages in real-time. This subscription provides all message versions for each sanitized message, giving you complete visibility into the sanitization process:

```csharp
using GGWP.Utils.Async;
using GGWP.Chat;

var sanitizedSubscriber = new Subscriber<SanitizationResponse>(
    GGWPSDK.Instance?.ChatAPI.SubscribeSanitizedMessages()
);

await foreach (var sanitized in sanitizedSubscriber.GetStream())
{
    string original = sanitized.messageDetails.originalMessage;
    string filtered = sanitized.messageDetails.filteredMessage;
    string replaced = sanitized.messageDetails.replacedMessage;
    
    // Process the sanitized message
    Debug.Log($"Sanitized message received: {filtered}");
}
```

**Replacing Messages**

Replace a message with its sanitized version. The specific version used depends on your configured `messageReplacementConfig` strategy. This is typically the method you'll use most frequently when building your chat system, as it automatically applies your chosen filtering approach:

```csharp
string userMessage = "User's message here";

var replaced = await GGWPSDK.Instance?.ChatAPI.ReplaceMessage(
    new ChatMessage { message = userMessage }
);

if (replaced != null)
{
    // Message was successfully replaced according to your strategy
    // The messageDetails will contain the message version selected by your ReplacementStrategy
    string messageToDisplay = replaced.messageDetails.replacedMessage;
    Debug.Log($"Message to display: {messageToDisplay}");
}
else
{
    // Message was dropped (possibly due to sanctions)
    Debug.Log("Message was dropped");
}
```

<details>

<summary><strong>IMPORTANT: Message Dropping Due to Sanctions</strong></summary>

When a user is sanctioned (muted), their messages will be dropped regardless of the replacement strategy. The `ReplaceMessage` method returns null to indicate that the message should not be sent:

```csharp
// If user is muted in chat channel
var replaced = await GGWPSDK.Instance?.ChatAPI.ReplaceMessage(
    new ChatMessage { message = "any message" }
);

if (replaced == null)
{
    Debug.Log("User is muted, message was dropped");
}
```

</details>

## References

1. [GGWP Global Content Filter Configurations](https://docs.ggwp.com/dashboard-user-guide/admin-features#content-filter-configuration)
2. [Available filter settings in GGWP](https://docs.ggwp.com/api-docs-chat/standard-package/chat-api#headers)
3. [Moderation with Discord Social SDK](https://discord.com/developers/docs/discord-social-sdk/how-to/integrate-moderation)
4. [Enabling Discord Webhooks in your Social SDK app](https://discord.com/developers/docs/events/webhook-events)
