Payment Bot
D-Wallet Paymentを作成するBot
ENV
.env.example
DISCORD_TOKEN=
DWALLET_API_KEY=
DWALLET_BASE_URL=https://dwallet-api.nikoroyal.com
DWALLET_CREATE_URL=https://dwallet.nikoroyal.com/wallet
TEXT
requirements.txt
discord.py>=2.5
dwsdk>=2.0.0
python-dotenv>=1.0
PYTHON
bot.py
import os, uuid
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 CreateWallet(discord.ui.View):
def __init__(self):
super().__init__(timeout=120)
self.add_item(discord.ui.Button(label="D-Walletを作成する", url=CREATE_URL))
class PaymentModal(discord.ui.Modal, title="D-Wallet Payment"):
amount = discord.ui.TextInput(label="支払額", placeholder="0.01000000")
asset = discord.ui.TextInput(label="LTC または DLTC", placeholder="LTC", max_length=4)
def __init__(self, bot):
super().__init__()
self.bot = bot
async def on_submit(self, interaction: discord.Interaction):
try:
await self.bot.dw.for_user(interaction.user.id).wallet()
except DWalletNotFoundError:
await interaction.response.send_message(
"D-Walletが必要です。", view=CreateWallet(), ephemeral=True
)
return
payment = await self.bot.dw.create_payment(
payer_discord_user_id=interaction.user.id,
asset=str(self.asset).strip().upper(),
amount=str(self.amount).strip(),
idempotency_key=f"sample-{interaction.id}-{uuid.uuid4().hex[:12]}",
merchant_reference=f"discord:{interaction.id}",
description="D-Wallet sample payment",
ttl_seconds=300,
)
await interaction.response.send_message(
"Paymentを作成しました。\n"
f"Payment ID: `{payment['payment_id']}`\n"
f"状態: `{payment['status']}`\n\n"
"D-Wallet公式Botから届く確認DMを操作してください。",
ephemeral=True,
)
class PayView(discord.ui.View):
def __init__(self, bot):
super().__init__(timeout=None)
self.bot = bot
@discord.ui.button(label="購入 / 支払い", style=discord.ButtonStyle.green, custom_id="dwallet:sample:pay")
async def pay(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_modal(PaymentModal(self.bot))
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__()
self.add_view(PayView(self))
await self.tree.sync()
async def close(self):
await self.dw.close()
await super().close()
bot = Bot()
@bot.tree.command(name="payment_panel", description="Paymentサンプルパネルを設置します")
@app_commands.checks.has_permissions(administrator=True)
async def panel(interaction: discord.Interaction):
await interaction.response.send_message(
"D-Wallet Payment Sample\n下のボタンからPaymentを作成できます。",
view=PayView(bot),
)
bot.run(TOKEN)
