VisionApp/features/avis.py

77 lines
2.8 KiB
Python
Raw Permalink Normal View History

from __future__ import annotations
import logging
import discord
from discord import app_commands
from discord.ext import commands
from features import shared
logger = logging.getLogger("vision")
class AvisModal(discord.ui.Modal, title="Donner mon avis"):
note = discord.ui.TextInput(label="Note (1-5)", max_length=1, placeholder="5")
commentaire = discord.ui.TextInput(
label="Commentaire",
style=discord.TextStyle.paragraph,
required=False,
max_length=1000,
)
def __init__(self, config: dict):
super().__init__()
self.config = config
async def on_submit(self, interaction: discord.Interaction):
try:
note = max(1, min(5, int(self.note.value)))
except ValueError:
await interaction.response.send_message("Note invalide (1-5).", ephemeral=True)
return
guild = interaction.guild or self.bot.get_guild(self.config["guild_id"])
if not guild:
await interaction.response.send_message("Serveur introuvable.", ephemeral=True)
return
channel = guild.get_channel(self.config["channel_id"])
if not channel:
await interaction.response.send_message("Salon d'avis introuvable.", ephemeral=True)
return
stars = "⭐" * note
embed = discord.Embed(
title="Avis",
description=f"**Note :** {stars} ({note}/5)\n**Auteur :** {interaction.user.mention}\n\n{self.commentaire.value or 'Aucun commentaire'}",
color=0x2F3136,
)
embed.set_thumbnail(url=interaction.user.display_avatar.url)
await channel.send(embed=embed)
await interaction.response.send_message("Merci pour ton avis !", ephemeral=True)
class Avis(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@app_commands.command(name="avis_setup", description="Configurer le système d'avis")
@app_commands.describe(channel="Salon où envoyer les avis")
@app_commands.checks.has_permissions(manage_guild=True)
async def avis_setup(self, interaction: discord.Interaction, channel: discord.TextChannel):
shared.set(interaction.guild_id, "avis", {"channel_id": channel.id, "enabled": True})
await interaction.response.send_message(f"Les avis seront envoyés dans {channel.mention}", ephemeral=True)
@app_commands.command(name="avis", description="Donner un avis sur le serveur")
async def avis(self, interaction: discord.Interaction):
cfg = shared.section(interaction.guild_id, "avis")
if not cfg.get("enabled"):
await interaction.response.send_message("Le système d'avis n'est pas activé.", ephemeral=True)
return
await interaction.response.send_modal(AvisModal(cfg))
async def setup(bot: commands.Bot):
await bot.add_cog(Avis(bot))