Ajout d'une commande fact
This commit is contained in:
parent
4abc4e20b5
commit
c8eb921d8b
|
@ -0,0 +1,53 @@
|
||||||
|
import discord
|
||||||
|
import random
|
||||||
|
import aiohttp
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from discord import app_commands
|
||||||
|
from discord import Embed
|
||||||
|
#from discord.app_commands import Choice
|
||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
|
from classes.my_cog import MyCog
|
||||||
|
|
||||||
|
|
||||||
|
class Fact(MyCog):
|
||||||
|
def __init__(self, bot):
|
||||||
|
super().__init__(bot)
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
|
@app_commands.command(name="fact")
|
||||||
|
async def fact(self, interaction: discord.Interaction, private=False):
|
||||||
|
"""
|
||||||
|
Vous reprendriez bien un petit shot de culture G ?
|
||||||
|
:param interaction: discord interaction
|
||||||
|
:param private: Garder ce petit bijou pour soi
|
||||||
|
"""
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'Authorization': f'Bearer {getenv("WIKIMEDIA_ACCESS_TOKEN")}',
|
||||||
|
'User-Agent': 'Staticky (https://mp2i-vms.fr)'
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
await response = session.get("https://fr.wikipedia.org/api/rest_v1/page/random/summary", headers=headers)
|
||||||
|
|
||||||
|
embed = Embed(
|
||||||
|
title=response.json.get("title", "Titre inconnu"),
|
||||||
|
url=response.json.get("content_urls").get("desktop").get("page"),
|
||||||
|
description=response.json.get('extract')
|
||||||
|
)
|
||||||
|
|
||||||
|
embed.set_thumbnail(response.json.get('thumbnail').get('source'))
|
||||||
|
|
||||||
|
await interaction.response.send_message("", embed=embed
|
||||||
|
ephemeral=private)
|
||||||
|
except Exception as e:
|
||||||
|
await interaction.response.send_message(f"Le message n'a pas pu être envoyé...\n`Erreur : {e}`",
|
||||||
|
ephemeral=True)
|
||||||
|
|
||||||
|
|
||||||
|
async def setup(bot):
|
||||||
|
await bot.add_cog(Fact(bot))
|
Loading…
Reference in New Issue