Skip to content

Commit

Permalink
Merge pull request #31 from Robso-creator/issue-29
Browse files Browse the repository at this point in the history
feat(issue-29): create APOD command
  • Loading branch information
Robso-creator authored Feb 6, 2024
2 parents 3da7d93 + dfb026b commit 43251bf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
18 changes: 18 additions & 0 deletions tests/test_bot_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 43251bf

Please sign in to comment.