aiden829219201

Make me a discord giveaway and ticket bot claim ticket ticket panel close ticket all via buttons and also giveaway!

Kodari AI
KodariAug 24 at 8:48 am 9
GiveawayTicketBot
A Discord bot managing giveaways and ticket-based support. Features interactive ticket panels with buttons for claiming, closing, and managing tickets. Includes giveaway creation with automatic winner selection, entry tracking, and role-based permissions. Uses discord.py with slash commands and button components for user-friendly interaction. Handles ticket claiming, reopening, and closing workflows with audit logging.
  1. Build persistent button-based ticket panels with create, claim, and close controls.

  2. Add slash-command giveaways with join buttons, timed endings, and SQLite-backed entries.

Implemented the Discord giveaway and ticket bot.

  • Button ticket panel via /ticketpanel

  • Private ticket creation, staff claim, and close buttons

  • Optional staff role, ticket category, and log channel configuration through .env

  • Button-based giveaways via /giveaway start

  • Persistent SQLite giveaway entries and automatic winner selection

  • Manual early ending through /giveaway end

  • Dependencies and .env.example included

Copy .env.example to .env, add your bot token, install dependencies with pip install -r requirements.txt, then run python bot.py.

Kodari can make mistakes. Review important code before using.
Files
bot.py
giveaways.py
requirements.txt
tickets.py
Code
4 552
bot.py
import logging
import os

import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv

from giveaways import GiveawayEntryView, GiveawayManager, GiveawayStore, parse_duration
from tickets import TicketControlsView, TicketManager, TicketPanelView, TicketSettings


load_dotenv()
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
LOGGER = logging.getLogger(__name__)


def optional_int(name: str) -> int | None:
    value = os.getenv(name, "").strip()
    return int(value) if value else None


class SupportBot(commands.Bot):
    def __init__(self) -> None:
        intents = discord.Intents.default()
        intents.members = True
        super().__init__(command_prefix="!", intents=intents)
        self.ticket_manager = TicketManager(
            self,
            TicketSettings(
                category_id=optional_int("TICKET_CATEGORY_ID"),
                staff_role_id=optional_int("STAFF_ROLE_ID"),
                log_channel_id=optional_int("TICKET_LOG_CHANNEL_ID"),
            ),
        )
        self.giveaway_store = GiveawayStore()
        self.giveaway_manager = GiveawayManager(self, self.giveaway_store)

    async def setup_hook(self) -> None:
        await self.giveaway_store.initialize()
        self.add_view(TicketPanelView(self.ticket_manager))
        self.add_view(TicketControlsView(self.ticket_manager))
        self.add_view(GiveawayEntryView(self.giveaway_manager))
        self.tree.add_command(giveaway_group)
        self.tree.add_command(ticket_panel)
        await self.giveaway_manager.restore()
        await self.tree.sync()

    async def close(self) -> None:
        await self.giveaway_manager.shutdown()
        await super().close()


bot = SupportBot()


@bot.event
async def on_ready() -> None:
    LOGGER.info("Logged in as %s (%s)", bot.user, bot.user.id if bot.user else "unknown")


giveaway_group = app_commands.Group(name="giveaway", description="Create and manage giveaways")


@giveaway_group.command(name="start", description="Start a button-based giveaway")
@app_commands.describe(duration="Examples: 10m, 1h, 1h30m", winners="Number of winners", prize="Prize to give away")
async def giveaway_start(interaction: discord.Interaction, duration: str, winners: app_commands.Range[int, 1, 50], prize: str) -> None:
    if interaction.guild is None or not isinstance(interaction.channel, discord.TextChannel):
        await interaction.response.send_message("Giveaways can only be started in a server text channel.", ephemeral=True)
        return
    if not isinstance(interaction.user, discord.Member) or not interaction.user.guild_permissions.manage_guild:
        await interaction.response.send_message("You need the Manage Server permission to start a giveaway.", ephemeral=True)
        return
    try:
        seconds = parse_duration(duration)
    except ValueError as error:
        await interaction.response.send_message(str(error), ephemeral=True)
        return

    await interaction.response.defer(ephemeral=True)
    giveaway = await bot.giveaway_manager.create(interaction.channel, winners, prize, seconds)
    await interaction.followup.send(f"Giveaway #{giveaway.id} started.", ephemeral=True)


@giveaway_group.command(name="end", description="End a giveaway early using its giveaway ID")
@app_commands.describe(giveaway_id="The ID shown after starting the giveaway")
async def giveaway_end(interaction: discord.Interaction, giveaway_id: int) -> None:
    if interaction.guild is None or not isinstance(interaction.user, discord.Member):
        await interaction.response.send_message("This command can only be used in a server.", ephemeral=True)
        return
    if not interaction.user.guild_permissions.manage_guild:
        await interaction.response.send_message("You need the Manage Server permission to end a giveaway.", ephemeral=True)
        return
    giveaway = bot.giveaway_store.get(giveaway_id)
    if giveaway is None or giveaway.guild_id != interaction.guild.id:
        await interaction.response.send_message("No active giveaway with that ID was found in this server.", ephemeral=True)
        return

    await interaction.response.defer(ephemeral=True)
    result = await bot.giveaway_manager.end(giveaway_id)
    await interaction.followup.send("Giveaway ended." if result else "That giveaway has already ended.", ephemeral=True)


@app_commands.command(name="ticketpanel", description="Post the ticket creation panel")
async def ticket_panel_callback(interaction: discord.Interaction) -> None:
    if interaction.guild is None or not isinstance(interaction.user, discord.Member):
        await interaction.response.send_message("This command can only be used in a server.", ephemeral=True)
        return
    if not bot.ticket_manager.is_staff(interaction.user):
        await interaction.response.send_message("Only ticket staff can post the ticket panel.", ephemeral=True)
        return
    if not isinstance(interaction.channel, discord.TextChannel):
        await interaction.response.send_message("Use this command in a server text channel.", ephemeral=True)
        return

    await bot.ticket_manager.send_panel(interaction.channel)
    await interaction.response.send_message("Ticket panel posted.", ephemeral=True)


if __name__ == "__main__":
    token = os.getenv("DISCORD_TOKEN")
    if not token:
        raise RuntimeError("DISCORD_TOKEN is missing. Copy .env.example to .env and add your bot token.")
    bot.run(token)