discord-lnbits-bot/discord_lnbits_bot.py

308 lines
12 KiB
Python
Raw Normal View History

2025-05-11 00:05:59 +00:00
#!/usr/bin/env python3
import os
import asyncio
import json
import io
import requests
import logging
from datetime import datetime
2025-05-14 14:59:35 +00:00
from dateutil.relativedelta import relativedelta
2025-05-14 15:12:23 +00:00
import qrcode
2025-05-11 00:05:59 +00:00
import discord
from discord import File, Embed
from discord.ext import commands
import websockets
from sqlalchemy import (
2025-05-14 14:59:35 +00:00
create_engine, Column, String, Integer, DateTime, Text, Enum, ForeignKey
2025-05-11 00:05:59 +00:00
)
2025-05-14 15:12:23 +00:00
from sqlalchemy.dialects.postgresql import JSONB
2025-05-11 00:05:59 +00:00
from sqlalchemy.ext.declarative import declarative_base
2025-05-14 14:59:35 +00:00
from sqlalchemy.orm import sessionmaker, relationship
2025-05-11 00:05:59 +00:00
2025-05-14 14:59:35 +00:00
from apscheduler.schedulers.asyncio import AsyncIOScheduler
2025-05-11 00:05:59 +00:00
2025-05-14 14:59:35 +00:00
# ─── Logging ──────────────────────────────────────────────────────────────────
2025-05-11 00:05:59 +00:00
logging.basicConfig(
level=logging.INFO,
2025-05-14 14:59:35 +00:00
format="%(asctime)s %(levelname)s %(name)s | %(message)s"
2025-05-11 00:05:59 +00:00
)
logger = logging.getLogger("discord_lnbits_bot")
2025-05-14 15:12:23 +00:00
# ─── DB Setup & Config Seeding ─────────────────────────────────────────────────
2025-05-14 14:59:35 +00:00
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
2025-05-14 15:12:23 +00:00
logger.critical("Missing DATABASE_URL in .env")
2025-05-14 14:59:35 +00:00
exit(1)
engine = create_engine(DATABASE_URL, future=True)
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
Base = declarative_base()
class Config(Base):
__tablename__ = "config"
key = Column(String, primary_key=True)
value = Column(Text, nullable=False)
class Plan(Base):
__tablename__ = "plans"
id = Column(Integer, primary_key=True)
name = Column(Text, nullable=False, unique=True)
command_name = Column(Text, nullable=False, unique=True)
role_id = Column(String(64), nullable=False)
channel_id = Column(String(64), nullable=False)
price_sats = Column(Integer, nullable=False)
invoice_message = Column(Text, nullable=False)
2025-05-14 15:12:23 +00:00
expiry_type = Column(
Enum("fixed_date", "rolling_days", "calendar_month", name="expiry_type"),
nullable=False
)
2025-05-14 14:59:35 +00:00
duration_days = Column(Integer, default=30)
subs = relationship("Subscription", back_populates="plan")
class Subscription(Base):
__tablename__ = "subscriptions"
id = Column(Integer, primary_key=True)
plan_id = Column(Integer, ForeignKey("plans.id"), nullable=False)
user_id = Column(String(64), nullable=False)
invoice_hash = Column(String(256), unique=True, nullable=False)
invoice_request = Column(Text, nullable=False)
2025-05-14 15:12:23 +00:00
status = Column(
Enum("pending","active","expired","cancelled", name="sub_status"),
nullable=False, default="pending"
)
2025-05-14 14:59:35 +00:00
created_at = Column(DateTime, default=datetime.utcnow)
paid_at = Column(DateTime)
expires_at = Column(DateTime)
role_assigned_at = Column(DateTime)
role_removed_at = Column(DateTime)
2025-05-14 15:12:23 +00:00
raw_invoice_json = Column(JSONB)
raw_payment_json = Column(JSONB)
2025-05-14 14:59:35 +00:00
plan = relationship("Plan", back_populates="subs")
2025-05-14 15:12:23 +00:00
# create tables & seed Config keys
2025-05-14 14:59:35 +00:00
Base.metadata.create_all(engine)
logger.info("✅ Database schema ready.")
2025-05-14 15:12:23 +00:00
session = SessionLocal()
for key in ("discord_token", "guild_id", "lnbits_url", "lnbits_api_key"):
if not session.get(Config, key):
session.add(Config(key=key, value=""))
session.commit()
session.close()
2025-05-14 14:59:35 +00:00
# ─── Config helper ────────────────────────────────────────────────────────────
def get_cfg(key: str) -> str:
2025-05-14 15:12:23 +00:00
db = SessionLocal()
row = db.get(Config, key)
db.close()
return row.value if row else ""
2025-05-14 14:59:35 +00:00
# Read core settings from DB
DISCORD_TOKEN = get_cfg("discord_token")
GUILD_ID = int(get_cfg("guild_id") or 0)
LNBITS_URL = get_cfg("lnbits_url")
LNBITS_API_KEY = get_cfg("lnbits_api_key")
2025-05-14 15:12:23 +00:00
for name, val in (
2025-05-14 14:59:35 +00:00
("discord_token", DISCORD_TOKEN),
("guild_id", GUILD_ID),
("lnbits_url", LNBITS_URL),
("lnbits_api_key", LNBITS_API_KEY),
2025-05-14 15:12:23 +00:00
):
2025-05-14 14:59:35 +00:00
if not val:
2025-05-14 15:12:23 +00:00
logger.critical(f"Config '{name}' is empty in DB. Fill it via the UI first.")
2025-05-11 00:05:59 +00:00
exit(1)
2025-05-14 14:59:35 +00:00
# Build WebSocket URL
2025-05-11 00:05:59 +00:00
if LNBITS_URL.startswith("https://"):
2025-05-14 14:59:35 +00:00
ws_base = LNBITS_URL.replace("https://", "wss://", 1)
2025-05-11 00:05:59 +00:00
elif LNBITS_URL.startswith("http://"):
2025-05-14 14:59:35 +00:00
ws_base = LNBITS_URL.replace("http://", "ws://", 1)
2025-05-11 00:05:59 +00:00
else:
logger.critical("LNBITS_URL must start with http:// or https://")
exit(1)
2025-05-14 14:59:35 +00:00
LNBITS_WS_URL = f"{ws_base}/api/v1/ws/{LNBITS_API_KEY}"
2025-05-11 00:05:59 +00:00
# ─── Discord Bot Setup ────────────────────────────────────────────────────────
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
2025-05-14 15:12:23 +00:00
# ─── Expiry computation ────────────────────────────────────────────────────────
2025-05-14 14:59:35 +00:00
def compute_expiry(paid_at: datetime, plan: Plan) -> datetime:
if plan.expiry_type == "calendar_month":
first = paid_at.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
return first + relativedelta(months=1)
2025-05-14 15:12:23 +00:00
if plan.expiry_type == "rolling_days":
2025-05-14 14:59:35 +00:00
return paid_at + relativedelta(days=plan.duration_days)
2025-05-14 15:12:23 +00:00
return paid_at # fixed_date handled via UI
2025-05-14 14:59:35 +00:00
2025-05-14 15:12:23 +00:00
# ─── Handle plan purchase ─────────────────────────────────────────────────────
2025-05-14 14:59:35 +00:00
async def handle_plan_purchase(interaction: discord.Interaction, plan: Plan):
invoice_ch = bot.get_channel(int(plan.channel_id))
if not invoice_ch:
return await interaction.response.send_message(
"❌ Invoice channel not found.", ephemeral=True
2025-05-11 00:05:59 +00:00
)
2025-05-14 13:39:58 +00:00
invoice_data = {
"out": False,
2025-05-14 14:59:35 +00:00
"amount": plan.price_sats,
"memo": plan.invoice_message
2025-05-14 13:39:58 +00:00
}
headers = {
"X-Api-Key": LNBITS_API_KEY,
"Content-Type": "application/json"
}
2025-05-11 00:05:59 +00:00
2025-05-14 14:59:35 +00:00
loop = asyncio.get_running_loop()
2025-05-11 00:05:59 +00:00
def create_invoice():
2025-05-14 15:12:23 +00:00
r = requests.post(
2025-05-11 00:05:59 +00:00
f"{LNBITS_URL}/api/v1/payments",
2025-05-14 15:12:23 +00:00
json=invoice_data, headers=headers, timeout=10
2025-05-11 00:05:59 +00:00
)
2025-05-14 15:12:23 +00:00
r.raise_for_status()
return r.json()
2025-05-11 00:05:59 +00:00
try:
2025-05-14 14:59:35 +00:00
inv = await loop.run_in_executor(None, create_invoice)
2025-05-14 13:39:58 +00:00
except Exception:
2025-05-14 14:59:35 +00:00
logger.exception("Invoice creation failed")
return await interaction.response.send_message(
2025-05-14 15:12:23 +00:00
"❌ Failed to generate invoice.", ephemeral=True
2025-05-11 00:05:59 +00:00
)
2025-05-14 15:12:23 +00:00
pay_req = inv["bolt11"]
pay_hash = inv["payment_hash"]
2025-05-14 14:59:35 +00:00
db = SessionLocal()
sub = Subscription(
plan_id = plan.id,
user_id = str(interaction.user.id),
invoice_hash = pay_hash,
invoice_request = pay_req,
2025-05-14 15:12:23 +00:00
raw_invoice_json= inv
2025-05-14 14:59:35 +00:00
)
2025-05-14 15:12:23 +00:00
db.add(sub); db.commit(); db.close()
logger.info(f"Stored pending {pay_hash} for user {interaction.user.id}")
2025-05-11 00:05:59 +00:00
2025-05-14 14:59:35 +00:00
def make_qr_buf():
2025-05-14 15:12:23 +00:00
buf = io.BytesIO()
qrcode.make(pay_req).save(buf, format="PNG")
buf.seek(0)
return buf
2025-05-14 13:39:58 +00:00
2025-05-14 14:59:35 +00:00
qr_buf = await loop.run_in_executor(None, make_qr_buf)
qr_file = File(qr_buf, filename="invoice.png")
2025-05-11 00:05:59 +00:00
embed = Embed(
2025-05-14 14:59:35 +00:00
title=f"⚡ Pay {plan.price_sats} sats for {plan.name}",
description=plan.invoice_message,
2025-05-11 00:05:59 +00:00
color=discord.Color.gold()
)
2025-05-14 14:59:35 +00:00
embed.add_field(name="Invoice", value=f"```{pay_req}```", inline=False)
2025-05-11 00:05:59 +00:00
embed.set_image(url="attachment://invoice.png")
2025-05-14 14:59:35 +00:00
embed.set_footer(text=f"Hash: {pay_hash}")
2025-05-11 00:05:59 +00:00
2025-05-14 14:59:35 +00:00
await invoice_ch.send(content=interaction.user.mention, embed=embed, file=qr_file)
2025-05-11 00:05:59 +00:00
await interaction.response.send_message(
f"✅ Invoice posted in {invoice_ch.mention}", ephemeral=True
)
2025-05-14 15:12:23 +00:00
# ─── WS listener ───────────────────────────────────────────────────────────────
2025-05-14 14:59:35 +00:00
async def lnbits_ws_listener():
await bot.wait_until_ready()
logger.info(f"Listening to LNbits WS at {LNBITS_WS_URL}")
while True:
try:
async with websockets.connect(LNBITS_WS_URL) as ws:
async for msg in ws:
2025-05-14 15:12:23 +00:00
data = json.loads(msg)
pay = data.get("payment", {})
hsh = pay.get("checking_id") or pay.get("payment_hash")
if not (hsh and pay.get("status")=="success"):
continue
db = SessionLocal()
sub = db.query(Subscription)\
.filter_by(invoice_hash=hsh, status="pending")\
.first()
if not sub:
db.close(); continue
paid_at = datetime.utcnow()
expires_at = compute_expiry(paid_at, sub.plan)
sub.status = "active"
sub.paid_at = paid_at
sub.expires_at = expires_at
sub.raw_payment_json = pay
db.commit(); db.close()
guild = bot.get_guild(GUILD_ID)
member = guild.get_member(int(sub.user_id)) or await guild.fetch_member(int(sub.user_id))
role = guild.get_role(int(sub.plan.role_id))
chan = bot.get_channel(int(sub.plan.channel_id))
await member.add_roles(role, reason="Subscription paid")
await chan.send(
f"🎉 {member.mention} paid **{sub.plan.price_sats} sats** "
f"for **{sub.plan.name}**, expires {expires_at:%Y-%m-%d}."
)
2025-05-14 14:59:35 +00:00
except Exception:
2025-05-14 15:12:23 +00:00
logger.exception("WS error; reconnecting in 10s")
2025-05-14 14:59:35 +00:00
await asyncio.sleep(10)
2025-05-14 15:12:23 +00:00
# ─── Cleanup expired ───────────────────────────────────────────────────────────
2025-05-14 14:59:35 +00:00
async def cleanup_expired():
now = datetime.utcnow()
db = SessionLocal()
2025-05-14 15:12:23 +00:00
rows = db.query(Subscription)\
.filter(Subscription.status=="active", Subscription.expires_at <= now)\
.all()
for sub in rows:
2025-05-14 14:59:35 +00:00
guild = bot.get_guild(GUILD_ID)
member = guild.get_member(int(sub.user_id))
role = guild.get_role(int(sub.plan.role_id))
if member and role in member.roles:
try:
await member.remove_roles(role, reason="Subscription expired")
sub.status = "expired"
sub.role_removed_at = now
db.commit()
2025-05-14 15:12:23 +00:00
logger.info(f"Removed expired {role.name} from {member.display_name}")
2025-05-14 14:59:35 +00:00
except Exception:
2025-05-14 15:12:23 +00:00
logger.exception("Error removing expired role")
2025-05-14 14:59:35 +00:00
db.close()
2025-05-14 15:12:23 +00:00
# ─── on_ready & scheduler ─────────────────────────────────────────────────────
2025-05-14 14:59:35 +00:00
@bot.event
async def on_ready():
2025-05-14 15:12:23 +00:00
logger.info(f"Bot logged in as {bot.user} (ID {bot.user.id})")
2025-05-14 14:59:35 +00:00
2025-05-14 15:12:23 +00:00
# dynamic command registration
2025-05-14 14:59:35 +00:00
db = SessionLocal()
plans = db.query(Plan).all()
for plan in plans:
2025-05-14 15:12:23 +00:00
@bot.tree.command(name=plan.command_name,
description=f"Buy {plan.name} for {plan.price_sats} sats")
2025-05-14 14:59:35 +00:00
async def _cmd(interaction: discord.Interaction, plan=plan):
await handle_plan_purchase(interaction, plan)
db.close()
await bot.tree.sync()
2025-05-14 15:12:23 +00:00
# start WS listener
2025-05-14 14:59:35 +00:00
bot.loop.create_task(lnbits_ws_listener())
2025-05-14 15:12:23 +00:00
# schedule midnightUTC expiry check
sched = AsyncIOScheduler(timezone="UTC")
sched.add_job(cleanup_expired, "cron", hour=0, minute=0)
sched.start()
2025-05-14 14:59:35 +00:00
2025-05-14 15:12:23 +00:00
# ─── Run ───────────────────────────────────────────────────────────────────────
2025-05-11 00:05:59 +00:00
if __name__ == "__main__":
bot.run(DISCORD_TOKEN)