Files
bot.py
config.py
nitro_checker.py
requirements.txt
Code
4 425
bot.py
import os
import asyncio
from dotenv import load_dotenv
# Load .env BEFORE anything else so config.py reads DEVELOPER_ID properly
load_dotenv()
import discord
from discord.ext import commands
from discord import Embed, Color
from config import DEVELOPER_ID, load_config, save_config
from nitro_checker import generate_code, check_code, extract_code
# ── Helpers ────────────────────────────────────────────────
async def send_to_all_channels(embed):
"""Send an embed to every text channel the bot can see across all guilds."""
for guild in bot.guilds:
for channel in guild.text_channels:
perms = channel.permissions_for(guild.me)
if perms.send_messages and perms.embed_links:
try:
await channel.send(embed=embed)
except discord.Forbidden:
pass
# ── Bot setup ──────────────────────────────────────────────
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents=intents)
# ── State ──────────────────────────────────────────────────
generating = False
generation_task: asyncio.Task | None = None
stats = {"checked": 0, "found": 0, "started_at": None}
# ── Permission guard ───────────────────────────────────────
def is_dev():
async def predicate(ctx):
if ctx.author.id != DEVELOPER_ID:
await ctx.send("⛔ Only the bot developer can use this command.")
return False
return True
return commands.check(predicate)
# ── Events ─────────────────────────────────────────────────
@bot.event
async def on_ready():
print(f"✅ Logged in as {bot.user} (ID: {bot.user.id})")
print(f" Developer ID: {DEVELOPER_ID}")
global generating, generation_task, stats
if not generating:
generating = True
stats = {"checked": 0, "found": 0, "started_at": discord.utils.utcnow()}
generation_task = asyncio.create_task(_run_generation(3))
print(" 🚀 Auto-started code generation (3 threads) — sending to ALL channels")
# ── Commands ───────────────────────────────────────────────
@bot.command(name="setchannel")
@is_dev()
async def set_channel(ctx):
"""(DEPRECATED) The bot now sends working codes to ALL channels automatically."""
embed = Embed(
title="ℹ️ No Channel Setup Needed",
description="The bot now sends working nitro codes to **every text channel** automatically. Just use `!start` and codes will appear everywhere!",
color=Color.blue(),
)
await ctx.send(embed=embed)
@bot.command(name="start")
@is_dev()
async def start_generation(ctx, threads: int = 3):
"""Manually start/restart code generation (dev only)."""
global generating, generation_task, stats
# Cancel existing generation if running
if generating and generation_task:
generating = False
generation_task.cancel()
threads = max(1, min(threads, 20)) # clamp 1–20
generating = True
stats = {"checked": 0, "found": 0, "started_at": discord.utils.utcnow()}
generation_task = asyncio.create_task(_run_generation(threads))
embed = Embed(
title="🚀 Generation Started/Restarted",
description=f"Checking **{threads}** random codes at a time — sending winners to **ALL channels**…",
color=Color.blue(),
)
embed.set_footer(text="Use !stop to stop | !stats for statistics")
await ctx.send(embed=embed)
async def _run_generation(threads: int):
"""Background task — spawns N workers that each generate + check codes."""
semaphore = asyncio.Semaphore(threads)
async def worker():
while generating:
await semaphore.acquire()
try:
code = generate_code()
result = await check_code(code)
finally:
semaphore.release()
if result is None:
continue # rate-limited; the function already slept
stats["checked"] += 1
if result.get("valid"):
stats["found"] += 1
embed = Embed(
title="🎁 Working Nitro Code Found!",
color=Color.green(),
timestamp=discord.utils.utcnow(),
)
embed.add_field(
name="Gift Code", value=f"`{code}`", inline=False
)
embed.add_field(
name="Link",
value=f"https://discord.gift/{code}",
inline=False,
)
embed.add_field(
name="Nitro Duration",
value=result.get("duration", "Unknown"),
)
embed.add_field(
name="Code Expires",
value=result.get("expires_at", "Unknown"),
)
embed.add_field(
name="Uses", value=result.get("uses", "N/A")
)
await send_to_all_channels(embed)
workers = [asyncio.create_task(worker()) for _ in range(threads)]
try:
await asyncio.gather(*workers)
except asyncio.CancelledError:
for w in workers:
w.cancel()
@bot.command(name="stop")
@is_dev()
async def stop_generation(ctx):
"""Stop the code generation (dev only)."""
global generating, generation_task
if not generating:
await ctx.send("⚠ Not currently generating codes.")
return
generating = False
if generation_task:
generation_task.cancel()
generation_task = None
elapsed = (
discord.utils.format_dt(stats["started_at"], style="R")
if stats["started_at"]
else "N/A"
)
embed = Embed(
title="🛑 Generation Stopped",
color=Color.red(),
timestamp=discord.utils.utcnow(),
)
embed.add_field(name="Codes Checked", value=str(stats["checked"]))
embed.add_field(name="Working Codes Found", value=str(stats["found"]))
embed.add_field(name="Started", value=elapsed)
await ctx.send(embed=embed)
@bot.command(name="check")
async def check_single(ctx, *, code_or_link: str):
"""Check if a specific nitro code or discord.gift link is valid."""
code = extract_code(code_or_link)
if code is None:
await ctx.send(