Skip to content

Commit

Permalink
feat(issue-18): create help command
Browse files Browse the repository at this point in the history
  • Loading branch information
Robso-creator committed Jan 30, 2024
1 parent 325cd2f commit a4907c9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
47 changes: 47 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from typing import Optional

import discord
from discord import app_commands
from discord.ext import commands
from discord.ui import Button
from discord.ui import View

from src import settings
from src.logger import setup_logger
Expand Down Expand Up @@ -28,5 +33,47 @@ async def on_ready():
async def hello(interaction: discord.Interaction):
await interaction.response.send_message('Hello World!!')


@bot.tree.command(name='help', guild=server_id, description='teste de descrição')
@app_commands.describe(command='comando que precisa de mais detalhes')
async def help(interaction: discord.Interaction, command: Optional[str] = None):
if command is None:
embed = discord.Embed(
title='Ajuda com comandos',
description='Para melhores detalhes sobre comandos, digite: `/help <nome_comando>`',
color=discord.Color.dark_magenta(),
)
else:
desired_command = next(
(
_command for _command in bot.tree.walk_commands(
guild=server_id,
) if _command.name == command
), None,
)

if desired_command is None:
_log.error('invalid command')
await interaction.response.send_message(content=f'Comando {command} é inválido.', ephemeral=True)
return

else:
_log.info(f"user asked help with command '{desired_command.name}'")
embed = discord.Embed(
title=f"Informações sobre comando '{desired_command.name}'",
description=f'_Nome_: `{desired_command.name}`\n_Descrição_: `{desired_command.description}`',
color=discord.Color.dark_magenta(),
)

button = Button(
label='Visitar repositório',
url='https://github.com/Robso-creator/discord_bot',
)
view = View()
view.add_item(button)

await interaction.response.send_message(embed=embed, view=view, ephemeral=True)


if __name__ == '__main__':
bot.run(settings.DISCORD_TOKEN)
26 changes: 22 additions & 4 deletions tests/test_bot_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_hello_command():
command_name = 'hello'
command_description = 'teste de descrição'

hello_command = next(
test_command = next(
(
c for c in bot.tree.walk_commands(
guild=server_id,
Expand All @@ -15,6 +15,24 @@ def test_hello_command():
None,
)

assert hello_command is not None, f"command '{command_name}' not found"
assert hello_command.callback.__name__ == command_name, 'function name is different from the command name'
assert hello_command.description == command_description, 'command description is different from the expected'
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_help_command():
command_name = 'help'
command_description = 'teste de descrição'

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 a4907c9

Please sign in to comment.