> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dodopayments.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Discord エンタイトルメント

> 購入時に顧客にDiscordサーバーの役割を付与し、サブスクリプション終了時に自動的に取り消します。

<Info>
  Discordエンタイトルメントは、支払い顧客をサーバーに追加し（オプションで役割を割り当て）、サブスクリプションがアクティブになるか一括購入がクリアされるとすぐに追加します。キャンセル、払い戻し、プラン変更により役割は自動的に取り消されます。
</Info>

## 配信される内容

* 顧客は受け取りメールやカスタマーポータルのOAuthリンクを通してDiscordアカウントを接続します。
* 認証すると、Dodo Paymentsはあなたのサーバーに追加し（または既存のメンバーシップを見つけ）設定した役割を割り当てます。
* 特定の役割を選ばなかった場合、サーバーのメンバーシップのみが付与されます。

この統合は有料コミュニティ、サポーターパーク、階層型アクセスチャンネルに最適です。

## Discordを接続する

<Steps>
  <Step title="Open Entitlements">
    Dodo Paymentsダッシュボードで、**Entitlements** に移動し、**+** をクリックして新しいエンタイトルメントを開始します。
  </Step>

  <Step title="Pick Discord">
    **Discord Access** を統合として選択します。まだDiscordをビジネスに接続していない場合は、**Connect Discord** を促されます。

    <Frame caption="Connect Discord prompt before the OAuth handoff.">
      <img src="https://mintcdn.com/dodopayments/do-W-dMDGVB_xzr_/images/entitlements/discord/connect-prompt.png?fit=max&auto=format&n=do-W-dMDGVB_xzr_&q=85&s=8a34c436dd7510942db2a9221f623938" alt="New entitlement panel prompting the merchant to connect Discord" style={{ maxHeight: '500px', width: 'auto' }} width="2844" height="1622" data-path="images/entitlements/discord/connect-prompt.png" />
    </Frame>

    クリックすると、新しいタブでDiscordが開きます。ログインし、ゲートしたいサーバーを選び、ボットの権限（Manage Roles, Kick Members, Create Invite）をそのサーバーで確認します。

    <Frame caption="Discord OAuth: pick the server, then confirm the bot's permissions on it.">
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '12px', alignItems: 'center', justifyItems: 'center' }}>
        <img src="https://mintcdn.com/dodopayments/do-W-dMDGVB_xzr_/images/entitlements/discord/oauth-add-bot.png?fit=max&auto=format&n=do-W-dMDGVB_xzr_&q=85&s=b57bf306fa15aa3f49b528f803cc625e" alt="Discord OAuth screen asking which server to add the Dodo Payments bot to" style={{ width: '100%', maxHeight: '420px', objectFit: 'contain' }} width="1026" height="1362" data-path="images/entitlements/discord/oauth-add-bot.png" />

        <img src="https://mintcdn.com/dodopayments/do-W-dMDGVB_xzr_/images/entitlements/discord/oauth-permissions.png?fit=max&auto=format&n=do-W-dMDGVB_xzr_&q=85&s=438c0b04177e3a4863fb90986f5b95e0" alt="Discord bot permission confirmation screen" style={{ width: '100%', maxHeight: '420px', objectFit: 'contain' }} width="1038" height="1472" data-path="images/entitlements/discord/oauth-permissions.png" />
      </div>
    </Frame>

    Discordがリダイレクトした後、サーバーが接続されていることを確認できます。

    <Frame caption="Server connected — return to the Dodo Payments dashboard to continue.">
      <img src="https://mintcdn.com/dodopayments/do-W-dMDGVB_xzr_/images/entitlements/discord/connected.png?fit=max&auto=format&n=do-W-dMDGVB_xzr_&q=85&s=bd1cbb3f9960e4eb1881d7a818dc6f16" alt="Discord Access connected successfully confirmation page" style={{ maxHeight: '500px', width: 'auto' }} width="2000" height="1140" data-path="images/entitlements/discord/connected.png" />
    </Frame>
  </Step>

  <Step title="Pick a server and role">
    ダッシュボードに戻ったら、接続した **サーバー**（ギルド）を選びます。配信時に割り当てる**役割**を任意に選択するか、空白のままでサーバーのメンバーシップのみを付与します。エンタイトルメントに**名前**を付けて、**Create Entitlement**をクリックします。

    <Frame>
      <img src="https://mintcdn.com/dodopayments/do-W-dMDGVB_xzr_/images/entitlements/discord/create.png?fit=max&auto=format&n=do-W-dMDGVB_xzr_&q=85&s=79e95041881d2925e58fc752280d508e" alt="New Entitlement - Discord Access form with connected server, server picker, role dropdown, and name field" style={{ maxHeight: '500px', width: 'auto' }} width="1196" height="1564" data-path="images/entitlements/discord/create.png" />
    </Frame>
  </Step>

  <Step title="Save the entitlement">
    エンタイトルメントを保存します。これで、製品に添付することが可能です。
  </Step>
</Steps>

## 顧客フロー

1. 顧客がチェックアウトを完了します。
2. Dodo Paymentsが`pending`ステータスで、Discordを指す`oauth_url`を作成します。
3. 顧客には「Join the Discord」ボタン（OAuthリンク）を含むメールが送信されます。このリンクはカスタマーポータルにも表示されます。
4. 顧客が認証すると、ボットがサーバーに追加し、設定された役割を割り当てます。グラントは`delivered`に移動します。
5. サブスクリプションがキャンセル、停止、または終了するか、商人が手動で取り消すと、ボットは役割を削除し、グラントは`revoked`に移動します。

<Tip>
  Dodo Paymentsボットの役割が付与する役割の**上に**配置されていることを確認してください。Discordは自身より高いランクの役割を割り当てることを防ぎます。
</Tip>

## 必須設定

| フィールド      | 必須  | 説明                                       |
| ---------- | --- | ---------------------------------------- |
| `guild_id` | はい  | DiscordサーバーID。ダッシュボードのピッカーが自動的に入力します。    |
| `role_id`  | いいえ | 配信時に割り当てる役割。サーバーメンバーシップのみのアクセスの場合は省略します。 |

## APIを使用した作成

<CodeGroup>
  ```typescript TypeScript theme={null} theme={null}
  import DodoPayments from 'dodopayments';

  const client = new DodoPayments({
    bearerToken: process.env['DODO_PAYMENTS_API_KEY'],
    environment: 'test_mode',
  });

  const entitlement = await client.entitlements.create({
    name: 'Patrons Discord Role',
    integration_type: 'discord',
    integration_config: {
      guild_id: '123456789012345678',
      role_id: '987654321098765432',
    },
  });
  ```

  ```python Python theme={null} theme={null}
  client.entitlements.create(
      name="Patrons Discord Role",
      integration_type="discord",
      integration_config={
          "guild_id": "123456789012345678",
          "role_id": "987654321098765432",
      },
  )
  ```

  ```go Go theme={null} theme={null}
  client.Entitlements.New(ctx, dodopayments.EntitlementNewParams{
    Name:            dodopayments.F("Patrons Discord Role"),
    IntegrationType: dodopayments.F(dodopayments.EntitlementIntegrationTypeDiscord),
    IntegrationConfig: dodopayments.F[dodopayments.IntegrationConfigUnionParam](
      dodopayments.IntegrationConfigDiscordConfigParam{
        GuildID: dodopayments.F("123456789012345678"),
        RoleID:  dodopayments.F("987654321098765432"),
      },
    ),
  })
  ```
</CodeGroup>

## Webhooks

Discordのグラントを追跡するために、[`entitlement_grant.*` webhook events](/developer-resources/webhooks/intents/entitlement-grant)を購読します：

* `entitlement_grant.created`が`status: "pending"`および顧客が認可するための`oauth_url`で発火します。
* 役割が割り当てられたときに`entitlement_grant.delivered`が発火します。
* 役割が削除されたときに`entitlement_grant.revoked`が発火します。

## トラブルシューティング

<AccordionGroup>
  <Accordion title="Customer never sees the role assigned">
    顧客がOAuthフローを完了するまで、グラントは`pending`にあります。エンタイトルメントのグラントリストをチェックし、OAuthリンクをクリックしたかどうかを確認してください。必要なら配送メールを再送します。
  </Accordion>

  <Accordion title="Grant moves to failed with permission errors">
    Dodo Paymentsボットがサーバーに残り、`Manage Roles`の権限を持ち、割り当てられている役割の上に配置されていることを確認してください。エンタイトルメントを再保存すると、検証が再実行されます。
  </Accordion>

  <Accordion title="Customer cancelled but still has the role">
    Discordは役割の削除を即座に伝播しますが、顧客のローカルDiscordクライアントがキャッシュすることがあります。再接続またはリフレッシュが必要です。サーバー側の状態は正しいです。
  </Accordion>
</AccordionGroup>
