Skip to content

Commit

Permalink
Merge pull request #11 from Robso-creator/issue-8
Browse files Browse the repository at this point in the history
feat(issue-8): use slash commands
  • Loading branch information
Robso-creator authored Jan 30, 2024
2 parents 9d12321 + 7b325e6 commit 4779e35
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Create .env
run: |
touch .env
echo DISCORD_SERVER_ID= ${{ secrets.DISCORD_SERVER_ID }} >> .env
- name: Run Tests
run: |
python -m pytest
36 changes: 18 additions & 18 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import random

import discord
from discord.ext import commands

from src import settings
from src.logger import setup_logger

_log = setup_logger('src.main')

intents = discord.Intents.default()
intents.typing = False
intents.presences = False
intents.members = True
intents.message_content = True

_log = setup_logger('src.main')
bot = commands.Bot(command_prefix='/', intents=intents)
server_id = discord.Object(settings.DISCORD_SERVER_ID)


class MyClient(discord.Client):
async def on_ready(self):
_log.info(f'Logged on as {self.user}!')
@bot.event
async def on_ready():
_log.info('Logged')
try:
synced = await bot.tree.sync(guild=server_id)
_log.info(f'Synced {len(synced)} command(s)')
except Exception as e:
_log.error(e)

async def on_message(self, message):
_log.info('message from {0.author}: {0.content}'.format(message))
if message.content == '!roleta':
await message.channel.send(
f'O sorteado foi: {self.users.copy()[random.randint(0, len(message.guild.members) - 1)]}',
)

@bot.tree.command(name='hello', guild=server_id, description='teste de descrição')
async def hello(interaction: discord.Interaction):
await interaction.response.send_message('Hello World!!')

if __name__ == '__main__':
from src import settings

client = MyClient(intents=intents)
client.run(settings.DISCORD_TOKEN)
bot.run(settings.DISCORD_TOKEN)
1 change: 1 addition & 0 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
load_env()

DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
DISCORD_SERVER_ID = os.getenv('DISCORD_SERVER_ID')
NASA_TOKEN = os.getenv('NASA_TOKEN')
Empty file added tests/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions tests/test_all_commands_have_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import inspect

from src.main import bot
from src.main import server_id
from tests import test_bot_commands


def _get_functions(module):
return [name for name, obj in inspect.getmembers(module) if inspect.isfunction(obj) and name.startswith('test_') and name.endswith('_command')]


def test_all_commands_have_tests():
list_commands = [
f'test_{command.name}_command' for command in bot.tree.walk_commands(guild=server_id)
]
list_test_functions = _get_functions(test_bot_commands)

list_missing_on_test = list(set(list_commands) - set(list_test_functions))

assert list_missing_on_test == [
], f'Test not found for command(s): {list_missing_on_test}'
20 changes: 20 additions & 0 deletions tests/test_bot_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from src.main import bot
from src.main import server_id


def test_hello_command():
command_name = 'hello'
command_description = 'teste de descrição'

hello_command = next(
(
c for c in bot.tree.walk_commands(
guild=server_id,
) if c.name == command_name
),
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'
30 changes: 0 additions & 30 deletions tests/test_bot_funcs.py

This file was deleted.

0 comments on commit 4779e35

Please sign in to comment.