示例#1
0
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_id = event.guild_id
        game_id = event.command.options['game-id']
        member = int(event.command.options['member'])

        game = self.games_repository.get(guild_id=guild_id, game_id=game_id)

        if game is None:
            error_game_not_found = self.message_provider.error_game_not_found(
                game_id)
            return DiscordResponse.ephemeral_channel_message(
                error_game_not_found)

        if member not in game.guesses:
            error_guess_not_found = self.message_provider.error_guess_not_found(
                game_id, member)
            return DiscordResponse.ephemeral_channel_message(
                error_guess_not_found)

        del game.guesses[member]

        self.games_repository.save(game)

        await self.game_post_manager.update(game)

        guess_deleted_message = self.message_provider.guess_deleted()
        return DiscordResponse.ephemeral_channel_message(guess_deleted_message)
示例#2
0
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_id = event.guild_id
        game_id = event.command.options.get('game-id')
        title = event.command.options.get('title')
        description = event.command.options.get('description')
        min_guess = event.command.options.get('min')
        max_guess = event.command.options.get('max')

        if game_id is None:
            game_id = self._generate_game_id(guild_id)
        else:
            existing_game = self.games_repository.get(guild_id, game_id)
            if existing_game is not None:
                message = self.message_provider.duplicate_game_id(game_id)

                return DiscordResponse.ephemeral_channel_message(message)

        game = Game(
            guild_id=guild_id,
            game_id=game_id,
            title=title,
            description=description,
            min_guess=min_guess,
            max_guess=max_guess,
            created_by=event.member.user_id,
            create_datetime=datetime.now(),
            close_datetime=None,
            closed=False,
        )
        self.games_repository.save(game)

        game_created_message = self.message_provider.game_created(game)
        return DiscordResponse.ephemeral_channel_message(game_created_message)
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_config = self.configs_repository.get(guild_id=event.guild_id)

        role = int(event.command.options['role'])
        if role not in guild_config.management_roles:
            text = self.message_provider.remove_invalid_management_role(role)
            return DiscordResponse.ephemeral_channel_message(text)

        guild_config.management_roles.remove(role)
        self.configs_repository.save(guild_config)

        text = self.message_provider.admin_removed_management_role(role=role)
        return DiscordResponse.ephemeral_channel_message(text)
示例#4
0
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_config = self.configs_repository.get(guild_id=event.guild_id)

        channel = int(event.command.options['channel'])
        if channel not in guild_config.management_channels:
            text = self.message_provider.remove_invalid_management_channel(
                channel)
            return DiscordResponse.ephemeral_channel_message(text)

        guild_config.management_channels.remove(channel)
        self.configs_repository.save(guild_config)

        text = self.message_provider.removed_management_channel(channel)
        return DiscordResponse.ephemeral_channel_message(text)
示例#5
0
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_config = self.configs_repository.get(guild_id=event.guild_id)

        channel = int(event.command.options['channel'])
        if channel in guild_config.management_channels:
            text = self.message_provider.error_duplicate_management_channel(
                channel)
            return DiscordResponse.ephemeral_channel_message(text)

        guild_config.management_channels.append(channel)
        self.configs_repository.save(guild_config)

        text = self.message_provider.added_management_channel(channel)
        return DiscordResponse.ephemeral_channel_message(text)
    def _disallowed_admin_call(self,
                               command: DiscordCommand) -> DiscordResponse:
        response = DiscordResponse.channel_message()
        response.is_ephemeral = True
        response.content = self.message_provider.disallowed_admin_call(command)

        return response
示例#7
0
async def test_handle():
    # Given
    command_name = "admin"
    subcommand_name = "info"

    event = DiscordEvent(command=DiscordCommand(
        command_name=command_name,
        subcommand_name=subcommand_name,
    ),
                         member=DiscordMember())

    mock_response = DiscordResponse.ephemeral_channel_message(
        "Well handled brother!")

    mock_route_handler = AsyncMock(RouteHandler)
    mock_route_handler.handle.return_value = mock_response

    router = _router(route_handler=mock_route_handler, )

    # When
    response = await router.route(event)

    # Then
    mock_route_handler.handle.assert_called()
    arguments = mock_route_handler.handle.call_args[0]
    assert arguments[0] == event
    assert arguments[1].command == command_name
    if subcommand_name:
        assert arguments[1].subcommand == subcommand_name

    assert response.status_code == 200
    assert json.loads(response.body) == mock_response.json()
示例#8
0
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        logger.debug("Getting info")

        guild_config = self.configs_repository.get(guild_id=event.guild_id)

        message = self.message_provider.channel_admin_info(
            guild_config=guild_config)
        return DiscordResponse.ephemeral_channel_message(message)
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_id = event.guild_id
        user_id = event.member.user_id
        user_nickname = event.member.nickname
        game_id = event.command.options['game-id']
        guess = event.command.options['guess']

        game = self.games_repository.get(guild_id, game_id)
        if game is None:
            error_message = self.message_provider.error_game_not_found(game_id)
            return DiscordResponse.ephemeral_channel_message(content=error_message)

        if game.guesses.get(user_id) is not None:
            error_message = self.message_provider.error_duplicate_guess(game_id)
            return DiscordResponse.ephemeral_channel_message(error_message)

        if game.closed:
            error_message = self.message_provider.error_guess_on_closed_game(game_id)
            return DiscordResponse.ephemeral_channel_message(error_message)

        if game.is_numeric():
            if not self.validate_guess(game=game, guess=guess):
                error_message = self.message_provider.invalid_guess(game)
                return DiscordResponse.ephemeral_channel_message(error_message)

        game_guess = GameGuess()
        game_guess.user_id = user_id
        game_guess.user_nickname = user_nickname
        game_guess.timestamp = datetime.now()
        game_guess.guess = guess

        game.guesses[int(user_id)] = game_guess
        self.games_repository.save(game)

        await self.game_post_manager.update(game)

        guess_added_message = self.message_provider.guess_added(game_id, guess)
        return DiscordResponse.ephemeral_channel_message(content=guess_added_message)
示例#10
0
def test_ephemeral_message():
    # Given
    discord_response = DiscordResponse.channel_message()

    # When
    discord_response.is_ephemeral = True

    # Then
    assert discord_response.flags & 64 == 64
    assert discord_response.is_ephemeral

    # When
    discord_response.is_ephemeral = False

    # Then
    assert discord_response.flags & 64 == 0
    assert not discord_response.is_ephemeral
示例#11
0
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_id = event.guild_id

        all_games = self.games_repository.get_all(guild_id)

        if 'closed' in event.command.options:
            if event.command.options['closed']:
                closed_games = list(filter(lambda g: g.closed, all_games))
                message = self.message_provider.channel_manage_list_closed_games(
                    closed_games)
            else:
                open_games = list(filter(lambda g: not g.closed, all_games))
                message = self.message_provider.channel_manage_list_open_games(
                    open_games)
        else:
            message = self.message_provider.channel_manage_list_all_games(
                all_games)

        return DiscordResponse.ephemeral_channel_message(message)
    async def call(self, event: DiscordEvent) -> DiscordResponse:
        guild_id = event.guild_id
        game_id = event.command.options['game-id']

        game = self.games_repository.get(guild_id, game_id)
        game.closed = True
        self.games_repository.save(game)

        await self.discord_messaging.send_channel_message(
            text=f"Game '{game_id} has now been closed for new guesses.",
            channel_id=event.channel_id,
        )

        for message in game.channel_messages:
            embed = self.message_provider.game_post_embed(game)
            await self.discord_messaging.update_channel_message(
                message.channel_id, message.message_id, embed=embed)

        game_closed_message = self.message_provider.game_closed(game)
        return DiscordResponse.ephemeral_channel_message(game_closed_message)
async def test_handle():
    # Given
    route_handler = _route_handler()

    event = DiscordEvent(command=DiscordCommand(command_name="ping", ))

    response = DiscordResponse.ephemeral_channel_message(
        "You handled it very well!")

    mock_route = AsyncMock(Route)
    mock_route.call.return_value = response

    route_definition = RouteDefinition(
        route=mock_route,
        command="ping",
    )

    # When
    response = await route_handler.handle(event, route_definition)

    # Then
    assert response == response
    def _bot_disallowed(self):
        response = DiscordResponse.channel_message()
        response.is_ephemeral = True
        response.content = self.message_provider.bot_missing_access()

        return response
示例#15
0
 async def call(self, event: DiscordEvent) -> DiscordResponse:
     return DiscordResponse.pong()