-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Robso-creator/issue-8
feat(issue-8): use slash commands
- Loading branch information
Showing
7 changed files
with
65 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file was deleted.
Oops, something went wrong.