VisionApp/features/links.py

61 lines
2.2 KiB
Python
Raw 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")
DEFAULT_LINKS = {
"Site web": "https://example.com",
"Discord": "https://discord.gg/example",
"YouTube": "https://youtube.com/@example",
}
class Links(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@app_commands.command(name="links", description="Voir les liens du serveur")
async def links(self, interaction: discord.Interaction):
cfg = shared.section(interaction.guild_id, "links")
links = cfg.get("links", DEFAULT_LINKS)
container = discord.ui.Container()
lines = [f"[{name}]({url})" for name, url in links.items()]
container.add_item(discord.ui.TextDisplay("## Liens du serveur\n" + "\n".join(lines)))
view = discord.ui.LayoutView()
view.add_item(container)
await interaction.response.send_message(view=view)
@app_commands.command(name="links_setup", description="Configurer les liens")
@app_commands.describe(name="Nom du lien", url="URL du lien")
@app_commands.checks.has_permissions(manage_guild=True)
async def links_setup(self, interaction: discord.Interaction, name: str, url: str):
cfg = shared.get(interaction.guild_id, "links")
links = cfg.get("links", {})
links[name] = url
cfg["links"] = links
shared.set(interaction.guild_id, "links", cfg)
await interaction.response.send_message(f"Lien **{name}** ajouté.", ephemeral=True)
@app_commands.command(name="links_remove", description="Supprimer un lien")
@app_commands.describe(name="Nom du lien")
@app_commands.checks.has_permissions(manage_guild=True)
async def links_remove(self, interaction: discord.Interaction, name: str):
cfg = shared.get(interaction.guild_id, "links")
links = cfg.get("links", {})
links.pop(name, None)
cfg["links"] = links
shared.set(interaction.guild_id, "links", cfg)
await interaction.response.send_message(f"Lien **{name}** supprimé.", ephemeral=True)
async def setup(bot: commands.Bot):
await bot.add_cog(Links(bot))