Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(issue-29): create APOD command #31

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Loading