EXAMPLE CODE

Exchange Bot

Provider Module方式の換金サンプル

ENV

.env.example

ENV
DISCORD_TOKEN=
DWALLET_API_KEY=
DWALLET_BASE_URL=https://dwallet-api.nikoroyal.com
DWALLET_CREATE_URL=https://dwallet.nikoroyal.com/wallet
TEXT

requirements.txt

TEXT
discord.py>=2.5
dwsdk>=2.0.0
python-dotenv>=1.0
PYTHON

bot.py

PYTHON
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
from dwallet import DWalletClient, DWalletNotFoundError

load_dotenv()
TOKEN = os.environ["DISCORD_TOKEN"]
CREATE_URL = os.getenv("DWALLET_CREATE_URL", "https://dwallet.nikoroyal.com/wallet")

class WalletCreate(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=120)
        self.add_item(discord.ui.Button(label="D-Walletを作成する", url=CREATE_URL))

class ExchangeView(discord.ui.View):
    def __init__(self, bot, owner_id: int):
        super().__init__(timeout=None)
        self.bot = bot
        self.owner_id = owner_id

    @discord.ui.button(label="外部決済 → LTC", style=discord.ButtonStyle.green)
    async def inbound(self, interaction: discord.Interaction, button: discord.ui.Button):
        try:
            await self.bot.dw.for_user(interaction.user.id).wallet()
        except DWalletNotFoundError:
            await interaction.response.send_message(
                "LTC受取にはD-Walletが必要です。", view=WalletCreate(), ephemeral=True
            )
            return
        await interaction.response.send_message(
            "外部決済Providerの入金確認後に、D-Wallet側の交換処理へ接続してください。",
            ephemeral=True,
        )

    @discord.ui.button(label="LTC → 外部決済", style=discord.ButtonStyle.blurple)
    async def outbound(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message(
            "受取金額・送金先の入力UIと、利用する決済Providerの処理を実装してください。",
            ephemeral=True,
        )

class Bot(commands.Bot):
    def __init__(self):
        super().__init__(command_prefix="!", intents=discord.Intents.default())
        self.dw = DWalletClient.from_env()

    async def setup_hook(self):
        await self.dw.__aenter__()
        await self.tree.sync()

    async def close(self):
        await self.dw.close()
        await super().close()

bot = Bot()

@bot.tree.command(name="exchange_panel", description="換金パネルを設置します")
async def exchange_panel(interaction: discord.Interaction):
    if interaction.guild is None:
        await interaction.response.send_message("サーバー内で実行してください。", ephemeral=True)
        return
    if not interaction.user.guild_permissions.administrator:
        await interaction.response.send_message("Administratorのみ設置できます。", ephemeral=True)
        return
    try:
        await bot.dw.for_user(interaction.user.id).wallet()
    except DWalletNotFoundError:
        await interaction.response.send_message(
            "パネル作成者にもD-Walletが必要です。", view=WalletCreate(), ephemeral=True
        )
        return
    await interaction.response.send_message(
        f"D-Wallet Exchange\nOwner: `{interaction.user.id}`",
        view=ExchangeView(bot, interaction.user.id),
    )

bot.run(TOKEN)