EXAMPLE CODE

Wallet Bot

残高・Receive・Historyを扱う最小構成

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 import app_commands
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 WalletCreateView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=120)
        self.add_item(discord.ui.Button(label="D-Walletを作成する", url=CREATE_URL))

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="dwallet", description="D-Wallet残高を表示します")
async def dwallet(interaction: discord.Interaction):
    user = bot.dw.for_user(interaction.user.id)
    try:
        wallet = await user.wallet()
        balance = await user.balance()
        receive = await user.receive()
    except DWalletNotFoundError:
        await interaction.response.send_message(
            "D-Walletが見つかりません。先にWalletを作成してください。",
            view=WalletCreateView(), ephemeral=True
        )
        return

    embed = discord.Embed(title="D-Wallet")
    embed.add_field(name="状態", value=f"`{wallet['status']}`", inline=False)
    embed.add_field(name="LTC残高", value=f"`{balance['available']} LTC`", inline=False)
    embed.add_field(name="入金アドレス", value=f"`{receive['address']}`", inline=False)
    await interaction.response.send_message(embed=embed, ephemeral=True)

@bot.tree.command(name="dwallet_history", description="D-Wallet最新履歴を表示します")
async def history(interaction: discord.Interaction):
    user = bot.dw.for_user(interaction.user.id)
    try:
        rows = await user.history(limit=10)
    except DWalletNotFoundError:
        await interaction.response.send_message(
            "D-Walletが見つかりません。", view=WalletCreateView(), ephemeral=True
        )
        return
    text = "\n".join(
        f"`{r['type']}` {r['amount']} LTC" for r in rows
    ) or "履歴はありません。"
    await interaction.response.send_message(text[:1900], ephemeral=True)

bot.run(TOKEN)