Per creare un bot Discord in Python che gestisce la creazione di ticket con pulsanti, puoi utilizzare la libreria discord.py, in particolare la sua estensione per i componenti come pulsanti e menu (discord.ui). Assicurati di avere discord.py aggiornato e di aver impostato correttamente il tuo bot su Discord Developer Portal.
Ecco un esempio di codice che mostra come implementare il bot:
import discord
from discord.ext import commands
from discord.ui import Button, View
# Imposta il tuo token
TOKEN = 'YOUR_BOT_TOKEN'
# ID dei canali e categorie specificati
SERVER_ID = 1276849263644508181 # Server ID
CATEGORY_ID = 1276844114708795444 # Categoria per i ticket
TARGET_CHANNEL_ID = 1276849263644508181 # Canale dove vengono inviati i messaggi iniziali
# Crea un'istanza del bot
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
class TicketView(View):
def __init__(self, member):
super().__init__(timeout=None)
self.member = member
@discord.ui.button(label="Supporto", style=discord.ButtonStyle.green)
async def supporto_button(self, interaction: discord.Interaction, button: Button):
await self.create_ticket(interaction, "supporto")
@discord.ui.button(label="Richiesta", style=discord.ButtonStyle.blurple)
async def richiesta_button(self, interaction: discord.Interaction, button: Button):
await self.create_ticket(interaction, "richiesta")
@discord.ui.button(label="Collaborazione", style=discord.ButtonStyle.gray)
async def collaborazione_button(self, interaction: discord.Interaction, button: Button):
await self.create_ticket(interaction, "collaborazione")
async def create_ticket(self, interaction: discord.Interaction, tipo: str):
guild = interaction.guild
category = discord.utils.get(guild.categories, id=CATEGORY_ID)
channel_name = f'{self.member.name}-{tipo}'
# Crea il canale nella categoria specificata
ticket_channel = await guild.create_text_channel(channel_name, category=category)
# Manda un messaggio nel canale creato
await ticket_channel.send(
f'{self.member.mention} ha aperto un ticket di tipo {tipo}.',
view=CloseTicketView(ticket_channel)
)
await interaction.response.send_message(
f"Il tuo ticket di tipo {tipo} è stato creato: {ticket_channel.mention}",
ephemeral=True
)
class CloseTicketView(View):
def __init__(self, channel):
super().__init__(timeout=None)
self.channel = channel
@discord.ui.button(label="Chiudi Ticket", style=discord.ButtonStyle.red)
async def chiudi_ticket(self, interaction: discord.Interaction, button: Button):
await self.channel.delete()
await interaction.response.send_message("Il ticket è stato chiuso.", ephemeral=True)
@bot.event
async def on_ready():
print(f'Bot connesso come {bot.user}')
# Recupera il canale target e manda il messaggio iniziale
target_channel = bot.get_channel(TARGET_CHANNEL_ID)
if target_channel:
await target_channel.send(
"Quale tipologia di ticket vuoi aprire? Seleziona un'opzione sotto.",
view=TicketView(member=None)
)
@bot.slash_command(guild_ids=[SERVER_ID])
async def ticket(ctx):
"""Crea un messaggio con opzioni di creazione ticket."""
await ctx.send("Quale tipologia di ticket vuoi aprire? Seleziona un'opzione sotto.", view=TicketView(member=ctx.author))
# Avvia il bot
bot.run(TOKEN)
Note importanti:
- Permessi e Intent: Assicurati che il bot abbia i permessi necessari per creare e gestire canali e che gli intent per i membri siano abilitati.
- Variabili da sostituire: Ricorda di sostituire
YOUR_BOT_TOKEN, SERVER_ID, CATEGORY_ID e TARGET_CHANNEL_ID con i valori corretti del tuo server Discord.
- Discord Developer Portal: Nel Discord Developer Portal, nel pannello del bot, assicurati di aver abilitato i componenti di messaggio.
Questo codice gestirà la creazione dei ticket con pulsanti e permetterà di chiuderli, mantenendo l'interazione fluida.