From dfb026b3edfe72f8110360809b9337f09d6db545 Mon Sep 17 00:00:00 2001 From: Robson Date: Tue, 6 Feb 2024 10:47:24 -0300 Subject: [PATCH] feat(issue-29): create APOD command --- requirements.txt | 2 ++ src/main.py | 33 +++++++++++++++++++++++++++++++++ tests/test_bot_commands.py | 18 ++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/requirements.txt b/requirements.txt index ac82f78..21857ff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,8 @@ colorlog==6.8.2 discord==2.3.2 +googletrans==3.1.0a0 loadotenv==1.0.1 mkdocs==1.5.3 mkdocs-material==9.5.6 pre-commit==3.6.0 +requests==2.31.0 diff --git a/src/main.py b/src/main.py index 5eafdd8..804984c 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,12 @@ from typing import Optional import discord +import requests from discord import app_commands from discord.ext import commands from discord.ui import Button from discord.ui import View +from googletrans import Translator from src import settings from src.logger import setup_logger @@ -80,5 +82,36 @@ async def help(interaction: discord.Interaction, command: Optional[str] = None): await interaction.response.send_message(embed=embed, view=view, ephemeral=True) +@bot.tree.command(name='apod', guild=server_id, description='foto astronômica do dia!') +async def apod(interaction: discord.Interaction): + response = requests.get( + f'https://api.nasa.gov/planetary/apod?api_key={settings.NASA_TOKEN}', + ).json() + img_url = response['url'] + img_author = response['copyright'] + + translator = Translator(service_urls=['translate.googleapis.com']) + img_explanation = translator.translate( + response['explanation'], dest='pt', + ).text + img_name = translator.translate(response['title'], dest='pt').text + + translate_append = 'Tradução automática feita utilizando Google Tradutor' + + embed = discord.Embed( + title=img_name, + description=f'{img_explanation}\n\n{translate_append}', + color=discord.Color.dark_magenta(), + ) + + embed.set_thumbnail( + url='https://gpm.nasa.gov/sites/default/files/NASA-Logo-Large.jpg', + ) # nasa logo + embed.set_image(url=img_url) + embed.set_footer(text=f'Autor(es):{img_author}') + + await interaction.response.send_message(embed=embed) + + if __name__ == '__main__': bot.run(settings.DISCORD_TOKEN) diff --git a/tests/test_bot_commands.py b/tests/test_bot_commands.py index 7bebd7a..b21aaba 100644 --- a/tests/test_bot_commands.py +++ b/tests/test_bot_commands.py @@ -36,3 +36,21 @@ def test_help_command(): assert test_command is not None, f"command '{command_name}' not found" assert test_command.callback.__name__ == command_name, 'function name is different from the command name' assert test_command.description == command_description, 'command description is different from the expected' + + +def test_apod_command(): + command_name = 'apod' + command_description = 'foto astronômica do dia!' + + test_command = next( + ( + c for c in bot.tree.walk_commands( + guild=server_id, + ) if c.name == command_name + ), + None, + ) + + assert test_command is not None, f"command '{command_name}' not found" + assert test_command.callback.__name__ == command_name, 'function name is different from the command name' + assert test_command.description == command_description, 'command description is different from the expected'